Objective-C 101 (save vs assign) NSString

Question 101

Let's say I make a car database and each car object is defined as:

#import <UIKit/UIKit.h> @interface Car:NSObject{ NSString *name; } @property(nonatomic, retain) NSString *name; 

Why is it @property(nonatomic, retain) NSString *name; , not @property(nonatomic, assign) NSString *name; ?

I understand that assign will not increment the reference count, as retain will be executed. But why use retain , since name is a member of the todo object, its scope is for itself.

No other external function will change it.

+54
ios objective-c iphone nsstring
Sep 04 '09 at 16:40
source share
8 answers

Objective-C does not have such a thing as an "object scope". The rules of the region have nothing to do with the lifetime of the object - the conservation account is all.

You typically need to declare ownership of instance variables. See Objective-C Memory Management Rules . With the retain property, your property installer claims ownership of the new value and waives ownership of the old. With the assign property, the surrounding code should do this, which is also a mess in terms of responsibility and separation of concerns. The reason you should use the assign property is when you cannot save the value (such as types without an object, such as BOOL or NSRect), or if saving it will lead to unwanted side effects.

By the way, in the case of NSString, the correct property type is usually copy . Thus, it cannot change from under you if someone goes to NSMutableString (which really is a kind of NSString).

+68
Sep 04 '09 at 17:47
source share

and don't forget to access it through

 self.name = something; 

because

 name = something; 

will not care about the generated setter / getter methods, but instead assign a value directly.

+19
Jan 11 '11 at 16:23
source share

Without retain there is no guarantee that the NSString* that you set name will live longer than the assignment statement itself. Using the retain property for the synthesized setter, you let it tell the memory management system that there is at least one other object interested in maintaining NSString* .

+12
Sep 04 '09 at 17:09
source share

For those looking for it, Apple's documentation on property attributes is here .

+9
Jan 11 2018-11-11T00:
source share

self. in:

 self.name = something; 

important! Without it, you access the variable directly and bypass the setter.

Older style (correct me if I am wrong):

 [self setName:something]; 

Anyway, this notation was (vaguely familiar) advice that I really needed when I was looking for the correct @properties on NSStrings . Thanks Axel.

+9
Jun 21 2018-11-11T00:
source share

After reading so many articles, SO posts, and demos to check Variable property attributes, I decided to combine all the attribute data

  • atomic // default
  • non-atomic
  • strong = save // ​​default
  • weak = unsafe_unretained
  • save
  • set // default
  • unsafe_unretained
  • Copy
  • only for reading
  • readwrite // default

therefore, below is a detailed link to an article in which you can find all the attributes mentioned above that defiantly help you. Many thanks to all the people who give the best answers here!

Modified property attributes or modifiers in iOS

  • save = strong
    • it is saved, the old value is freed and assigned
    • Save indicates that the new value should be sent - receive at destination and the old value sent -release
    • keep the same as strong.
    • apple says that if you write, save, it will be automatically converted / works as strong.
    • methods such as "alloc" include implicit "save"

Example:

 @property (nonatomic, retain) NSString *name; @synthesize name; 
  1. assignee
    • assigns a default value and simply assigns the variable
    • assign is a property attribute that tells the compiler how to synthesize an implementation of a property setter.
    • I would use assignment for primitive C properties and weak for weak references to Objective-C objects.

Example:

 @property (nonatomic, assign) NSString *address; @synthesize address; 
+8
Mar 21 '13 at 7:20
source share

The Google Objective-C Style Guide covers this pretty well:

Setters taking NSString should always copy the string that it takes. Never just keep the string. This avoids the caller changing it without your knowledge. Do not assume that since you accept NSString, it is not NSMutableString.

+3
Mar 12 2018-12-12T00:
source share

Would it be unsatisfactory if your class received this string object and then disappeared from under it? Do you know how the second time your class mentions this object, it was deleted by another object?

This is why you want to use retain setter semantics.

+2
Jan 11 '11 at 16:13
source share



All Articles