Objective-C class extension

前端 未结 5 739
伪装坚强ぢ
伪装坚强ぢ 2020-12-13 18:07

As a fairly new objective-c programmer (with a 4 years Java experience), I seem to be having a hard time understanding when to use class extensions. From what I understood (

5条回答
  •  鱼传尺愫
    2020-12-13 18:57

    In question 1, example 2: The methods foo and bar implemented in Class are visible to instances of Class. If foo and bar aren't declared in a separate header file AND the implementation file isn't made available, another class will be unaware of the existence of foo and bar. foo and bar are private methods; instances of Class will still respond to foo and bar messages. Also, Xcode will flag a warning if another class that tries to message foo and bar since Xcode is unable to verify (without the declaration in the header file) that foo and bar exist. By contrast, example 1 declaration of bar allows any other class to send message bar to instances of Class. Xcode will also not flag an error for any message to bar if @interface Class is in a header file that is #import by the other class.

    In question 2, example 4: mySortedArray is a local immutable array declared with local scope within Class. The mySortedArray in example 3 is an instance variable of type NSArray accessible to other class that creates an instance of Class.

提交回复
热议问题