0

objC newbie question...thanks in advance for any help...

it is my understanding that when i declare for a class a property of type NSMutable Array, i must always declare it with the property attribute (copy) as in:

@property myArray (copy,nonatomic)

however, suppose i have an instance, myObject, of a class MyClass, whose properties are as follows:

@interface MyClass : NSObject

@property (nonatomic, copy) NSString *name;
@property (nonatomic, copy) NSString *kind;  
@property (nonatomic, copy) NSMutableArray *timeSeries;

if i declare a property of class MyClass in some other class, does that property also have to be declared as (copy), or can it be (weak) or (strong), and why (or why not?)

for example, can i declare the following property in another class as follows:

@property (nonatomic, weak) MyClass *thing;

dave adelson
  • 853
  • 9
  • 15
  • Why do you believe your mutable array property should have the `copy` attribute? – trojanfoe Jun 20 '13 at 20:41
  • i was just reading through documentation to try to figure out if i misunderstood that point. i see code examples with NSMutableArrays declared with strong attributes as well... – dave adelson Jun 20 '13 at 21:02
  • I think it would depend on how you want the property to behave. If you want to expose a list that cannot be modified then you could make it a `NSArray` (i.e. non-mutable) or use the `copy` attribute so that changes don't inadvertently affect the object. However if you want it to be modified externally then you would use `strong`. – trojanfoe Jun 20 '13 at 21:05
  • If you don't want it to be modified, making it `NSArray` won't help as a mutable instance can still be passed... – Wain Jun 20 '13 at 21:05
  • @Wain Sure, you can simply return the mutable array as non-mutable, however it requires casting back to `NSMutableArray` before it can be modified, which I would consider outside normal use of the property (protection of variables is only advisory in computer languages and can be perverted if someone really wants to mess with the data). – trojanfoe Jun 20 '13 at 21:08
  • okay, i understand where i got the notion that properties that are NSMutableArrays should have the copy attribute...according to one description (http://rypress.com/tutorials/objective-c/properties.html) "If you’re working with mutable values, this has the added perk of freezing the object at whatever value it had when it was assigned." so i guess my question more generically concerns what happens when other classes define properties as weak, when the class of the property includes properties that are not weak...if anyone can guide me to a good primer on this i would appreciate it – dave adelson Jun 20 '13 at 21:15
  • @trojanfoe, I mean that someone can pass in a mutable array to you immutable property, then they can change the contents behind your back. That's what `copy` protects against. – Wain Jun 20 '13 at 21:15
  • @daveadelson, have you looked at http://stackoverflow.com/questions/9859719/xcode-property-attributes-nonatomic-copy-strong-weak – Wain Jun 20 '13 at 21:18
  • just looked at it, thank you...still have some questions but they are abstract in nature and just reflect ignorance and inexperience...i will stew over it on my own...thanks for the help – dave adelson Jun 20 '13 at 21:50

0 Answers0