Properties and Instance Variables in Objective-C

前端 未结 5 1112
借酒劲吻你
借酒劲吻你 2020-12-02 05:44

I\'m rather confused about properties and instance variables in Objective-C.

I\'m about half-way through Aaron Hillegass\'s \"Cocoa Programming for Mac OS X\" and ev

相关标签:
5条回答
  • 2020-12-02 06:19

    Properties are used to generate accessors for instance variables, there's no magic happening.

    You can implement the same accessors by hand.

    You can find in Aaron Hillegass's book examples of 3 memory management strategies for member variables. They are assign/copy/retain. You select one of those as required for given variable.

    I assume you understand memory management in Objective-c ...

    Accessors hide the complexity and differences of memory management for each variable.

    For example:

    name = @"Test"
    

    is a simple assignment, name now holds reference to NSString @"Test". However you could decide to use copy or retain. No matter which version of memory management you chose accessor hides the complexity and you always access the variable with (or similar):

    [self setName:@"Test"] 
    [self name]
    

    Now setName: might use assign/copy or retain and you don't have to worry about it.

    My guess is that iPhone tutorials use properties to make it easier for new developers to jump through memory management (even though it's handy to generate appropriate accessors with properties rather than implement them by hand every time).

    0 讨论(0)
  • 2020-12-02 06:29

    You can write like this

    //MyClass.h
    
    @class Something;
    
    @interface MyClass : NSObject 
    
    @property (nonatomic, strong) NSString *name;
    @property (nonatomic, strong) NSArray *items;
    
    @end 
    
    //MyClass.m
    @interface MyClass() 
    
    @property (nonatomic, strong) IBOutlet NSTextField *myTextField;
    @property (nonatomic, strong) Something *something;
    
    @end
    
    0 讨论(0)
  • 2020-12-02 06:34

    However, in the iPhone world, things seem to be different. People declare properties for every single instance variable, declare properties for IBOutlets, and use accessors/mutators to interact with instance variables within the class (e.g. they would write [self setName:@"Test"] rather than name = @"Test").

    That's not iPhone-specific. Except in init methods and the dealloc method, it's good practice to always use your accessors. The main benefit, especially on the Mac (with Cocoa Bindings), is that using your accessors means free KVO notifications.

    The reason why people “declare properties for every single instance variable” is most probably that all of their instance variables are things they want to expose as properties. If they had something they would want to keep private, they would not declare a property for it in the header file. (However, they may make a property for it in a class extension in the implementation file, in order to get the aforementioned free KVO notifications.)

    Declaring properties for outlets is overkill, in my opinion. I don't see a point to it. If you don't make a property, the nib loader will set the outlet by direct instance-variable access, which is just fine for that task.

    0 讨论(0)
  • 2020-12-02 06:34

    I would suggest that modern development has made a very strong attempt to identify, define and apply best practices.

    Among these best practices we find continuity and consistency.

    Apart from arguing over use of accessors in init and dealloc methods, accessors should generally be used all the time (inside and outside of a class) for the benefits they offer, including encapsulation, polymorphic var implementations (which both allow for abstracting and refactoring) and to facilitate those best practices of continuity and consistency. The fundamental benefits of an object-orient language come into play when doing things in this way and exploiting the fullness of the language's capabilities. Always being consistent in one's coding is an oft undermentioned benefit, as any senior programmer will usually attest.

    0 讨论(0)
  • 2020-12-02 06:38

    In the iPhone world, there's no garbage collector available. You'll have to carefully manage memory with reference counting. With that in mind, consider the difference between:

    name = @"Test";
    

    and

    self.name = @"Test";
    // which is equivalent to:
    [self setName: @"Test"];
    

    If you directly set the instance variable, without prior consideration, you'll lose the reference to the previous value and you can't adjust its retain count (you should have released it manually). If you access it through a property, it'll be handled automatically for you, along with incrementing the retain count of the newly assigned object.

    The fundamental concept is not iPhone specific but it becomes crucial in an environment without the garbage collector.

    0 讨论(0)
提交回复
热议问题