Effective Objective-C (1)

  • 自定义的类如果不想通过 [[Class alloc] init] 来初始化一个类,可以如下
-(instancetype)init
{
    @throw [NSException exceptionWithName:NSInternalInconsistencyException reason:@"Must use initWithName: age: gender:" userInfo:nil];
}

-(instancetype)init
{
    return [self initWithName:@"Roy" age:18 gender:GenderMan];
}
  • 通过实现 description 方法对象打印
-(NSString *)description
{
    return [NSString stringWithFormat:@"<%@ : %p, %@, %lu,%lu>",[self class],self,_name,(unsigned long)_age,(unsigned long)_gender];
}

你可能感兴趣的:(Effective Objective-C (1))