美文网首页
Effective Objective-C (1)

Effective Objective-C (1)

作者: CaptainRoy | 来源:发表于2018-08-01 10:12 被阅读8次
    • 自定义的类如果不想通过 [[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)

          本文链接:https://www.haomeiwen.com/subject/jmdlvftx.html