码迷,mamicode.com
首页 > 移动开发 > 详细

IOS 初始化方法的总结(initWithxxx)

时间:2015-07-20 19:16:04      阅读:148      评论:0      收藏:0      [点我收藏+]

标签:

初始化方法都是等同:

1. 

[NSString stringWithFormat:<#(NSString *), ...#>];

 [[NSString alloc]initWithFormat:<#(NSString *), ...#>:<#(NSString *)#>]

2.

[NSArray arrayWithObject:<#(id)#>];

 [[NSArray alloc]initWithArray:<#(NSArray *)#>]

3.模型中设计初始化方法:

.h文件中:

3个属性:

@property (nonatomic,copy) NSString *icon;

@property (nonatomic,copy) NSString *desc;

@property (nonatomic,copy) NSString *name;

2个方法+中包含-:

-(id) initWithDict:(NSDictionary *)dict;

+(id) shopWithDict:(NSDictionary *)dict;

.m文件中:

-(id) initWithDict:(NSDictionary *)dict{

    if (self = [super init]) {

        self.icon = dict[@"icon"];

        self.name = dict[@"name"];

        self.desc = dict[@"desc"];

    }

 

    return self;

}

 

+(id) shopWithDict:(NSDictionary *)dict{

    

//    Shop *s = [[Shop alloc]init];

//    s.icon = dict[@"icon"];

//    s.name = dict[@"name"];

//    s.desc = dict[@"desc"];

    return [[self alloc]initWithDict:dict];

    

 

 

IOS 初始化方法的总结(initWithxxx)

标签:

原文地址:http://www.cnblogs.com/yuyu-2012/p/4661978.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!