Do I use save or copy in the same singlet?

I read somewhere that with an NSString in an object you need to use a copy instead of saving. Can someone explain if this is correct and why?

For example, I have the following declaration for my singleton:

#import <foundation/Foundation.h> @class FaxRecipient; @interface MyManager : NSObject { NSString *subject; NSString *reference; NSString *coverSheet; FaxRecipient *faxRecipient; } @property (nonatomic, retain) NSString *test1; @property (nonatomic, retain) NSString *test2; @property (nonatomic, retain) NSString *test3; @property (nonatomic,retain) FaxRecipient *faxRecipient; + (id)sharedManager; @end 
+8
ios iphone
source share
5 answers

I think that β€œmust” in the sense of must is a little strong. You can use either copy or retain , but usually you should use copy for your NSString* properties, because:

  • Usually you do not want the string property to change under your nose;
  • NSMutableString is a subclass of NSString , so it’s possible that someone can set your NSString* property to point to the string to be modified, thereby creating the potential to change the string while it is being used;
  • For immutable classes, such as NSString , copy operations end up just saving the original object.

Given these three points, it's hard to think of a good reason to use retain instead of copy for your NSString properties.

+8
source share

prefers copy . it doesn't matter if your class is a single or not.

I wrote a rather long explanation of this, which describes mutable and immutable types in detail: NSMutableString how to save / copy

+4
source share

You can also use a copy instead of saving. I am using copy for NSString. Good discussion on this topic. Here is the message stackoverflow post NSString Copy or save?

+3
source share

You can use either. In both cases, you will be the owner of the objects and must be released in dealloc .

The difference between copy and retain .

save: β†’ This simply increases the storage counter on the existing object.

copy: β†’ it breaks the new object into your property

In both cases: you will have ownership of the objects.

Read Apple's concept of memory management .

@ Klaus Broch: from Apple documentation

You take responsibility for the object if you create it using a method whose name begins with "alloc", "new", "copy" or "mutableCopy" (for example, alloc, newObject or mutableCopy), or if you send it to it save message.

0
source share

You must use the save value in this instance. The reason for this is because 1. you do not want the object to be freed 2. you most likely want to change the NSString value at some point

Using the copy attribute basically means that the value of your NSString should not change without warning. that is, it will stop the NSString value, which will be installed and restored at the same time.

For this implementation, you should use persistence, or at least that's my understanding. See here for more details: http://cocoawithlove.com/2010/06/assign-retain-copy-pitfalls-in-obj-c.html

-2
source share

All Articles