iOS property declaration clarification

2019-01-29 19:58发布

This is a two part question in hopes that I can understand more about the topic.

1) It seems to me that you have two popular options for declaring a property for a class in objective c. One is to add the property to the header's class body eg.

@interface MyClass : NSObject {
    NSArray *myArray;
}

Or you can add it after the @interface body and before the @end statement like so.

@interface MyClass : NSObject {
    //
}

@property (nonatomic, retain) NSArray *myArray;

What is the difference between these two "styles" and when do you choose one over the other?

2) after the @property you find options such as (nonatomic, retain). What are those for and why/when do you use different options?

5条回答
男人必须洒脱
2楼-- · 2019-01-29 20:24

1) @property is a special way to define getter- and setter-methods, or as we call them accessors in Objective-C. Your first snippet just declares an array for which you have to declare and write accessors yourself. For example setMyArray: and myArray.
Using @property will declare your accessors for you and is equivalent to declaring setMyArray: and myArray yourself. It is the preferred way to declare accessors since Objective-C 2.0. Note that you still have to declare the property (in your case myArray) yourself.

2) You first need to know about @synthesize. Remember that @property DECLARES the accessors for your property, @synthesize will IMPLEMENT them. When you use an @property in your @interface you mostly likely write an @synthesize in @implementation. Using @synthesize is equivalent to implementing setMyArray: and myArray.
The attributes (nonatomic, retain) tell the compiler, among others, how the memory management should work and therefore how the methods will be implemented. Note that you never actually see these accessors, but be assured that they are there and ready for you to be used.

To read more on that topic I recommend reading Section 9 on Properties from the following Tutorial or buy a Book that covers an Introduction to Objective-C.

Also you should familiarize yourself with at least the following attributes:

  • Access
    • Choose readwrite (default) or readonly. If readonly is set, ONLY the getter methods will be available.
  • Setter Memory Management
    • assign (default), simply assigns the new value. You mostly likely only use this with primitive data types.
    • retain, releases the old value and retains the new. If you use the garbage collector, retain is equivalent to assign. Why? The manual release of the old value will be done by the garbage collector.
    • copy will copy the new value and release the old value. This is often used with strings.
  • Threading
    • atomic (default) will ensure that the setter method is atomic. This means only one thread can access the setter at once.
    • nonatomic, use this when you don't work with threads.

This post gives you a good introduction to memory management and assign, retain and copy.

查看更多
乱世女痞
3楼-- · 2019-01-29 20:34

Here are the only property modifiers that Xcode recognizes:

  • nonatomic (does not enforce thread safety on the property, mainly for use when only one thread shall be used throughout a program)
  • atomic (enforces thread safety on the property, mainly for use when multiple threads shall be used throughout a program) (default)
  • retain / strong (automatically retains / releases values on set, makes sure values do not deallocate unexpectedly) (default if ARC and object type)
  • readonly (cannot set property)
  • readwrite (can both set and get property) (default)
  • assign / unsafe_unretained (no memory management shall be done with this property, it is handled manually by the person assigning the value) (default if not ARC or object type)
  • copy (copies the object before setting it, in cases where the value set must not change due to external factors (strings, arrays, etc).
  • weak (automatically zeroes the reference should the object be deallocated, and does not retain the value passed in)
  • getter=method (sets the selector used for getting the value of this property)
  • setter= method (set the selector used for setting the value of this property)
查看更多
The star\"
4楼-- · 2019-01-29 20:43

Properties are basically the accessor methods. They define the scope of the variable. First case as given above,the variable is not accessible in other classes whereas by declaring a property as in the second case,variable is accessible in other classes also. Also, they are useful for memory management.

查看更多
女痞
5楼-- · 2019-01-29 20:43

First one will be private declaration and will not be accessible by other classes if you do not define the second one. Second is used together with @synthesize in .m module , and setter/getter's are created for you by the compiler. You can still define your own getter or setter with this. In this case all iVars defined in @property can be accessed by other classes.Retain/release operations are done automatically. You should read Apple documentation for more details. please check: What's the difference between the atomic and nonatomic attributes?

查看更多
不美不萌又怎样
6楼-- · 2019-01-29 20:44

Properties are basically the accessor methods. They define the scope of the variable. by default access specifior of variable is protected and properties set its Specifier from protected to Public

查看更多
登录 后发表回答