用户
 找回密码
 立即注册

发帖

201510所学新技巧(2),iOS开发技巧

[复制链接]
  • TA的每日心情
    慵懒
    2025-11-24 10:46
  • 27

    主题

    6

    回帖

    669

    积分

    管理员

    积分
    669
    发表于 2015-10-14 00:00:00
    博客已经两个月没有更新了,心里塞塞的。国庆节,把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
    2015/10/14
    % K5 U5 c! N& Q. g' Khttps://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/MemoryMgmt/Articles/mmPractical.html
    $ E% V! a4 F* T& u- E0 w7 d" Y不要在init和dealloc方法里面使用访问器方法。
    : v3 t5 T! I, P7 X示例代码:
    0 b7 F: i6 w$ n  G( e- z4 }) H9 e
    1. @interface Counter : NSObject2 W; J0 H, Y) l  A- W, z3 k  ]4 e
    2. / [- \& L% Y* ?" W
    3. @property (nonatomic, retain) NSNumber *count;4 z+ F. ]5 b" V! I* F

    4. - Y8 v7 M- q. o- u% t
    5. @end;0 T6 [! {/ [" b: S( Y" w4 ^) a4 o

    6. ' A3 ^5 N, I* W& Q6 U' h

    7. 5 v* s6 J# n! d, Y" F; r
    8. - init {0 O" Q  ?3 Y) @6 d5 h+ J& h9 |
    9.     self = [super init];
      , l% K/ W; L% Y
    10.     if (self) {& _, o; z" X3 a3 u
    11.         _count = [[NSNumber alloc] initWithInteger:0];
      6 o- M. l5 F& V$ u5 W4 H% N
    12.     }
      2 o' d" S8 ~) ~' k$ M
    13.     return self;, b9 G* q( R$ R6 g! T) {; n
    14. }
    复制代码
    结果我在项目中都是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 ~' wso上面也搜了相关问题,大家比较赞同的是:该对象不确定是否存在,很有可能会引起内存泄露(虽然我没在项目中遇到过)。
    二、A Block Should Always Be the Last Argument to a Method三、Enumeration Macros 枚举
    2015/10/183 H0 O8 ]2 m% ^- ?$ U- ?$ D
    https://developer.apple.com/library/ios/releasenotes/ObjectiveC/ModernizationObjC/AdoptingModernObjective-C/AdoptingModernObjective-C.html#//apple_ref/doc/uid/TP40014150-CH1-SW1
    % _6 f% X2 M0 K  J在项目中用NS_ENUM、NS_OPTIONS表示枚举。" I: i5 r' ~( Y: J. D
    before:# q" Q9 |- J  n2 t$ M
    1. enum {
      2 e" J$ \! |- [. `$ V. t5 }
    2. 9 x* @, k7 z2 g* o
    3. UITableViewCellStyleDefault,
      7 C! q) \( m$ S9 q5 h1 l: h9 [
    4. ! n; Q% r  K/ l' l1 X$ p
    5. UITableViewCellStyleValue1," [' ]$ R9 D5 V* o  g

    6. 5 D. d4 a. y( {& V& C/ X: b- q/ ]. U
    7. UITableViewCellStyleValue2,5 ~7 {8 `$ X) l0 e) T5 L

    8. " M, d9 B9 H  v  P: s
    9. UITableViewCellStyleSubtitle
        ^$ _" T3 W& s! c
    10. - O# g  n5 u/ p
    11. };4 x3 q$ m5 C, e$ L7 I/ Y

    12.   a0 k( H: |8 F- f9 ?* n
    13. typedef NSInteger UITableViewCellStyle;
    复制代码
    now:
    ) M- Q. A/ a. z* v
    1. typedef NS_ENUM(NSInteger, UITableViewCellStyle) {6 ~: K! l: k: i2 y+ L$ l
    2. 5 C% B' q: l; I! C3 J' d3 ^0 b$ o
    3. UITableViewCellStyleDefault,
      / J+ a; A3 [3 x7 ^

    4. , r) I- v' M) D
    5. UITableViewCellStyleValue1,. @- i' {' L/ w2 K7 D6 }" f5 |
    6. 8 V' E% Y5 h. d1 e9 g5 Z
    7. UITableViewCellStyleValue2,% T8 S. d9 g2 L8 U+ D, l. }
    8. " I' U  u! C5 m  `5 Q- M
    9. UITableViewCellStyleSubtitle
      , z  w% Q1 i9 R  Q: q+ Z! L0 g3 e; R

    10. ) {; G) [0 Y( D  w
    11. };
    复制代码
    四、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
    1. - (instancetype)initWithBaseURL:(NSURL *)url) e) |9 |; D. i* ~' e
    2.            sessionConfiguration:(NSURLSessionConfiguration *)configuration NS_DESIGNATED_INITIALIZER;
    复制代码
    m文件:
    , G$ c0 L( Y+ T
    1. - (instancetype)init {; z% v# A  ?) b2 L5 _' L
    2.     return [self initWithBaseURL:nil];
      # j2 `" b& `7 u2 x  K
    3. }! `& p8 i) W' L9 b( V
    4. 3 x. X2 Q3 g- f" P
    5. - (instancetype)initWithBaseURL:(NSURL *)url {( S1 X/ J/ G+ \* W
    6.     return [self initWithBaseURL:url sessionConfiguration:nil];
      % _5 `$ L/ r! `( c/ F
    7. }( w9 x+ N* E  I, S
    8.   {' R/ L: S0 u
    9. - (instancetype)initWithSessionConfiguration:(NSURLSessionConfiguration *)configuration {8 [. }5 l" o# y8 ^' H
    10.     return [self initWithBaseURL:nil sessionConfiguration:configuration];
      0 X/ z- H  m2 `# }1 _/ a
    11. }; j/ c) i: g9 B

    12. 0 Y3 u; z9 G. d/ n3 l9 L4 ?
    13. - (instancetype)initWithBaseURL:(NSURL *)url# ~6 F3 m) X/ o+ o
    14.            sessionConfiguration:(NSURLSessionConfiguration *)configuration
      / \7 N7 G1 E* o, d2 E$ H
    15. {% }$ [6 l# ?% k* c7 d; j! ~9 e0 L4 {
    16.     self = [super initWithSessionConfiguration:configuration];" I. O% z+ q% P! S4 Q
    17.     if (!self) {
      + [& Q0 o* [; o& ^# L& x
    18.         return nil;0 F$ F8 p& T( ~( d3 Z% _
    19.     }
      ; d4 x  M7 N2 L2 i" n9 G
    20. 3 l0 v. I/ X+ Z* Y
    21.     // Ensure terminal slash for baseURL path, so that NSURL +URLWithString:relativeToURL: works as expected. f) M- w5 g: T7 l
    22.     if ([[url path] length] > 0 && ![[url absoluteString] hasSuffix:@"/"]) {7 x: w: _) B" Q% w& K( w: u
    23.         url = [url URLByAppendingPathComponent:@""];
      & t3 f& v  D' M7 m
    24.     }+ G% M) \' C7 w' N# U

    25. . B- N' r" W  W/ m3 T0 H, C
    26.     self.baseURL = url;1 u/ f* A  t% _# P

    27. * H- w  h& o/ s
    28.     self.requestSerializer = [AFHTTPRequestSerializer serializer];
      $ M, o$ Y$ i& c: V) r$ V
    29.     self.responseSerializer = [AFJSONResponseSerializer serializer];
      : N- J$ q  q% F0 d/ m
    30. 7 U! C$ |0 D' I$ E  K: F
    31.     return self;& w6 T5 K6 a: ~, a& y
    32. }
    复制代码
    五、instancetype
    Using 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
    使用道具 举报 回复
    发表于 2016-6-16 17:00:16

    - l+ J: h8 r. d  d/ w只是赚金币  别见怪
    使用道具 举报 回复 支持 反对
    严禁恶意灌水!!!拒绝伸手党!!!
    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    ض