ios developer tiny share-20160823
来源:互联网 发布:凡古加网络 编辑:程序博客网 时间:2024/06/05 16:56
上节我们讲Objective-C的init方法,这节我们讲initWithXX方法,如initWithInt,实现带有参数的初始化方法。另外,我们讲一下工厂方法实现初始化。
Initializer Methods Can Take Arguments
Some objects need to be initialized with required values. An NSNumber object, for example, must be created with the numeric value it needs to represent.
The NSNumber class defines several initializers, including:
- (id)initWithBool:(BOOL)value;- (id)initWithFloat:(float)value;- (id)initWithInt:(int)value;- (id)initWithLong:(long)value;
Initialization methods with arguments are called in just the same way as plain init methods—an NSNumber object is allocated and initialized like this:
NSNumber *magicNumber = [[NSNumber alloc] initWithInt:42];
As mentioned in the previous chapter, a class can also define factory methods. Factory methods offer an alternative to the traditional alloc] init] process, without the need to nest two methods.
The NSNumber class defines several class factory methods to match its initializers, including:
+ (NSNumber *)numberWithBool:(BOOL)value;+ (NSNumber *)numberWithFloat:(float)value;+ (NSNumber *)numberWithInt:(int)value;+ (NSNumber *)numberWithLong:(long)value;
A factory method is used like this:
NSNumber *magicNumber = [NSNumber numberWithInt:42];
This is effectively the same as the previous example using alloc] initWithInt:]. Class factory methods usually just call straight through to alloc and the relevant init method, and are provided for convenience.
0 0
- ios developer tiny share-20160823
- ios developer tiny share-20160628
- ios developer tiny share-20160629
- ios developer tiny share-20160630
- ios developer tiny share-20160701
- ios developer tiny share-20160704
- ios developer tiny share-20160705
- ios developer tiny share-20160706
- ios developer tiny share-20160707
- ios developer tiny share-20160708
- ios developer tiny share-20160711
- ios developer tiny share-20160712
- ios developer tiny share-20160713
- ios developer tiny share-20160714
- ios developer tiny share-20160715
- ios developer tiny share-20160718
- ios developer tiny share-20160719
- ios developer tiny share-20160720
- Homebrew error: Another active Homebrew process is already in progress
- Android ActivityManagerService(AMS)的启动分析 << 代码讲的比较细致,在了解主要流程后再看这篇
- accept函数详解
- MPC-HC在windows10上VS2013编译
- imageview清空缓存状态
- ios developer tiny share-20160823
- spring data redis 操作redis 单机版和集群
- iOS开发支付集成之微信支付
- 贪吃蛇self1
- springmvc-组件配置-学习笔记
- 在centos6上安装docker1.12
- Android架构(一)MVP全解析
- CE、PE、P
- svn检出maven项目后,工程名字不显示M标识