【问题标题】:Which technique to use for creating private methods in Obj-C?使用哪种技术在 Obj-C 中创建私有方法?
【发布时间】:2010-10-29 02:14:13
【问题描述】:

当我想在 Objective-C 中创建私有方法时,我应该使用什么?
1) 众所周知的分类技术。
2) @private 指令。
(我在做 iOS 开发)。

【问题讨论】:

    标签: objective-c cocoa-touch private-methods


    【解决方案1】:

    @private 用于 ivars,而不用于方法。只需在 .m 文件的顶部创建一个类扩展并将私有方法放在那里。它看起来像

    @interface MyClass () // note the empty parens
    - (void)onePrivateMethod;
    - (void)twoPrivateMethod;
    @end
    
    @implementation MyClass
    // implement everything
    @end
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-01-17
    • 2011-03-26
    • 2010-10-07
    • 2013-10-16
    • 2010-09-12
    • 1970-01-01
    • 1970-01-01
    • 2013-09-18
    相关资源
    最近更新 更多