博客已经两个月没有更新了,心里塞塞的。国庆节,把Objective-C高级编程:iOS与OS X多线程和内存管理看了一遍,其实这本书去年就买了,但是一直都放在柜子里冷藏中,这本书里面分析苹果源码的写的很好,里面很多东西都是从苹果官方网站摘要的,第一章”自动引用计数”大部分内容摘自Transitioning to ARC Release Notes。对于只有一点C、C++基础而直接接触iOS开发的我,开发中大部分都是从网上找资料,学的东西也是很零散,项目中需要什么就赶紧补什么,能够实现这个功能并且不出bug就行,原理方面的知道的很少,其实苹果官方网站和Xcode真的是学习的好工具,在苹果官方网站,你能系统的学到该知识点,用的时候能够说出个所以然来。在看文档的时候,对比自己项目中的代码,有违背apple开发文档的就改回来,这样对项目和自身都是有好处。所以,我打算多看看apple开发文档,记录一下那些快速消化技术点而遗漏的知识点。一、Don’t Use Accessor Methods in Initializer Methods and dealloc- @interface Counter : NSObject2 W; J0 H, Y) l A- W, z3 k ]4 e
- / [- \& L% Y* ?" W
- @property (nonatomic, retain) NSNumber *count;4 z+ F. ]5 b" V! I* F
- Y8 v7 M- q. o- u% t- @end;0 T6 [! {/ [" b: S( Y" w4 ^) a4 o
' A3 ^5 N, I* W& Q6 U' h
5 v* s6 J# n! d, Y" F; r- - init {0 O" Q ?3 Y) @6 d5 h+ J& h9 |
- self = [super init];
, l% K/ W; L% Y - if (self) {& _, o; z" X3 a3 u
- _count = [[NSNumber alloc] initWithInteger:0];
6 o- M. l5 F& V$ u5 W4 H% N - }
2 o' d" S8 ~) ~' k$ M - return self;, b9 G* q( R$ R6 g! T) {; n
- }
复制代码结果我在项目中都是self.count = [[NSNumber alloc] initWithInteger:0];(相当于[self setCount:[[NSNumber alloc] initWithInteger:0]])用点语法操作对象的,所以以后在init和dealloc方法里面得调整过来。
, @: O/ j6 Z% Z# F* `) k但是文档没有说为什么不要用访问器方法。
_! [' w# b5 `/ p喵神的理解是你无法确定 self 到底是不是确实调用的是你想要的实例。
4 ]( k7 n& H* t) g, T) W9 ~' w在so上面也搜了相关问题,大家比较赞同的是:该对象不确定是否存在,很有可能会引起内存泄露(虽然我没在项目中遇到过)。 二、A Block Should Always Be the Last Argument to a Method三、Enumeration Macros 枚举- enum {
2 e" J$ \! |- [. `$ V. t5 } - 9 x* @, k7 z2 g* o
- UITableViewCellStyleDefault,
7 C! q) \( m$ S9 q5 h1 l: h9 [ - ! n; Q% r K/ l' l1 X$ p
- UITableViewCellStyleValue1," [' ]$ R9 D5 V* o g
5 D. d4 a. y( {& V& C/ X: b- q/ ]. U- UITableViewCellStyleValue2,5 ~7 {8 `$ X) l0 e) T5 L
" M, d9 B9 H v P: s- UITableViewCellStyleSubtitle
^$ _" T3 W& s! c - - O# g n5 u/ p
- };4 x3 q$ m5 C, e$ L7 I/ Y
a0 k( H: |8 F- f9 ?* n- typedef NSInteger UITableViewCellStyle;
复制代码now:
) M- Q. A/ a. z* v - typedef NS_ENUM(NSInteger, UITableViewCellStyle) {6 ~: K! l: k: i2 y+ L$ l
- 5 C% B' q: l; I! C3 J' d3 ^0 b$ o
- UITableViewCellStyleDefault,
/ J+ a; A3 [3 x7 ^
, r) I- v' M) D- UITableViewCellStyleValue1,. @- i' {' L/ w2 K7 D6 }" f5 |
- 8 V' E% Y5 h. d1 e9 g5 Z
- UITableViewCellStyleValue2,% T8 S. d9 g2 L8 U+ D, l. }
- " I' U u! C5 m `5 Q- M
- UITableViewCellStyleSubtitle
, z w% Q1 i9 R Q: q+ Z! L0 g3 e; R
) {; G) [0 Y( D w- };
复制代码 四、NS_DESIGNATED_INITIALIZER 宏2015/10/184 ^0 J* ^" M9 h; c) q
链接与第三点相同。9 D. o! D# y6 q5 J: i9 d
NS_DESIGNATED_INITIALIZER可译为指定初始化方法。我们在写初始化方法的时候,很有可能不会只有一个初始化方法(参数不同)。你是会每一个都调用一次父类的初始化方法么?还是只有一个初始化方法(即指定初始化方法)来调用父类的初始化方法,而其他的初始化方法(可理解为方便型初始化方法)就类似于金字塔一样根据不同的参数来调用该指定初始化方法? why ?The designated initializer pattern helps ensure that inherited initializers properly initialize all instance variables.指定初始化模式有助于确保继承初始化时能够正确初始化所有的实例变量。 示例代码如下(来自AFHTTPSessionManager文件):
1 R) l4 |" v2 I5 {8 F/ zh文件:
! t% d7 U* t# Q - - (instancetype)initWithBaseURL:(NSURL *)url) e) |9 |; D. i* ~' e
- sessionConfiguration:(NSURLSessionConfiguration *)configuration NS_DESIGNATED_INITIALIZER;
复制代码m文件:
, G$ c0 L( Y+ T - - (instancetype)init {; z% v# A ?) b2 L5 _' L
- return [self initWithBaseURL:nil];
# j2 `" b& `7 u2 x K - }! `& p8 i) W' L9 b( V
- 3 x. X2 Q3 g- f" P
- - (instancetype)initWithBaseURL:(NSURL *)url {( S1 X/ J/ G+ \* W
- return [self initWithBaseURL:url sessionConfiguration:nil];
% _5 `$ L/ r! `( c/ F - }( w9 x+ N* E I, S
- {' R/ L: S0 u
- - (instancetype)initWithSessionConfiguration:(NSURLSessionConfiguration *)configuration {8 [. }5 l" o# y8 ^' H
- return [self initWithBaseURL:nil sessionConfiguration:configuration];
0 X/ z- H m2 `# }1 _/ a - }; j/ c) i: g9 B
0 Y3 u; z9 G. d/ n3 l9 L4 ?- - (instancetype)initWithBaseURL:(NSURL *)url# ~6 F3 m) X/ o+ o
- sessionConfiguration:(NSURLSessionConfiguration *)configuration
/ \7 N7 G1 E* o, d2 E$ H - {% }$ [6 l# ?% k* c7 d; j! ~9 e0 L4 {
- self = [super initWithSessionConfiguration:configuration];" I. O% z+ q% P! S4 Q
- if (!self) {
+ [& Q0 o* [; o& ^# L& x - return nil;0 F$ F8 p& T( ~( d3 Z% _
- }
; d4 x M7 N2 L2 i" n9 G - 3 l0 v. I/ X+ Z* Y
- // Ensure terminal slash for baseURL path, so that NSURL +URLWithString:relativeToURL: works as expected. f) M- w5 g: T7 l
- if ([[url path] length] > 0 && ![[url absoluteString] hasSuffix:@"/"]) {7 x: w: _) B" Q% w& K( w: u
- url = [url URLByAppendingPathComponent:@""];
& t3 f& v D' M7 m - }+ G% M) \' C7 w' N# U
. B- N' r" W W/ m3 T0 H, C- self.baseURL = url;1 u/ f* A t% _# P
* H- w h& o/ s- self.requestSerializer = [AFHTTPRequestSerializer serializer];
$ M, o$ Y$ i& c: V) r$ V - self.responseSerializer = [AFJSONResponseSerializer serializer];
: N- J$ q q% F0 d/ m - 7 U! C$ |0 D' I$ E K: F
- return self;& w6 T5 K6 a: ~, a& y
- }
复制代码 五、instancetypeUsing instancetype instead of id in appropriate places improves type safety in your Objective-C code. 摘自苹果文档
, T0 ^! W* S4 q: `5 z& [ z这样写更安全,苹果官方文档也有举例,id返回的是任意类型,而instancetype返回的本类的对象,这样编译器能检测消息是否发错。 To make sure instancetype factory methods have the right subclassing behavior, be sure to use [self class] when allocating the class rather than referring directly to the class name.& z0 Z& n; H' N) }6 g% A
创建对象的时候尽量用[self class],而不是直接用类名。 * l& l7 a* m9 Z$ D$ x* S/ W
原文链接:http://joakimliu.github.io/2015/10/14/new-skill-in-this-week-two/ + m3 `" r. L$ @( z! j( m* M9 e
" M3 S2 [! _3 y' F1 g- V- ]9 a1 B( T( l; y
* Q" F# t# A6 Q3 R8 O4 M
|