1

This is my code:

@interface Object : NSObject {
@private
  NSArray *array;
}

@property NSArray *array;

@end

And the @synthesize in the implementation. I get a compiler warning in the line with the @property:

warning: default assign attribute on property 'array' which implements NSCopying protocol is not appropriate with -fobjc-gc[-only]

If I write the property as @property (assign) NSArray *array it does not show up. What is this about?

lucas clemente
  • 6,255
  • 8
  • 41
  • 61

3 Answers3

2

In your case you are creating a property that is a pointer to an object. Assign, which is the default, is not appropriate for objects, which should be declared as retain or copy.

In your case you should define your property as:

@property (nonatomic, copy) NSArray *array;

You could use retain instead of copy here, but there are good reasons to use copy.

edit

To answer the deeper question you seem to be asking - have a look at this thread from the Cocoa mailing lists.

Are you using the LLVM compiler or gcc?

Community
  • 1
  • 1
Abizern
  • 146,289
  • 39
  • 203
  • 257
  • Okay, so this depends on the mutability of `array`? How is this affected by the garbage collection, as stated in the warning? – lucas clemente Jun 03 '11 at 13:46
  • As jer said - if you're being explicit about it, it lets you do it. – Abizern Jun 03 '11 at 13:54
  • I understand that, but _why_ does it warn me about that only if I'm using garbage collection? I just want to understand why someone put this warning there ;) – lucas clemente Jun 03 '11 at 14:01
  • LLVM. I understand it now. The issue is that `assign` in a GC environment produces a strong reference, whereas it is weak in a non-GC app. Edited your answer. – lucas clemente Jun 03 '11 at 16:38
0

Properties default to assign. Your property is an assign.

jer
  • 20,094
  • 5
  • 45
  • 69
0

Regarding assign vs copy in GC enabled app, I found this via google...

http://www.cocoabuilder.com/archive/cocoa/194064-use-of-assign-vs-copy-for-accessors-in-garbage-collected-app.html

I think we usually use assign, but will use copy if needed, like for example, for NSString object. So to get rid of warning, we just explicitly specify it as assign.

Khomsan
  • 543
  • 5
  • 5