5

If I have a property like this:

//test.h

@interface test
@property (nonatomic, readonly, weak) NSObject x;
@end

redefined in the implementation file to be read/write:

// test.m
@interface test ()
@property (nonatomic, readwrite) NSObject x;
@end

I used weak in .h, but I said nothing in the extension, will the property keep the 'weak' specifier, or will it change to 'strong'?

Will the keywords strong/assign/weak be overwritten when the property is redefined?

jscs
  • 63,694
  • 13
  • 151
  • 195
Wingzero
  • 9,644
  • 10
  • 39
  • 80
  • As a common practice I always specify the `strong/weak/assign` attribute of the property in the class extension. It's not needed in the read-only public declaration. – rmaddy Jan 06 '15 at 03:37
  • @rmaddy - Declaring just `readonly` in the `.h` and `readwrite, weak` in the class extension in the `.m` gives "Primary property declaration is implicitly strong while redeclaration in class extension is weak" under Xcode 5.1.1. However including `strong` or `assign` in the extension does not raise the ire of the compiler. Has a later compiler changed this? – CRD Jan 06 '15 at 04:11
  • @CRD Hmmm. I guess I haven't done that with `weak` properties. Good to know. – rmaddy Jan 06 '15 at 06:23

1 Answers1

5

A simple test with Xcode 5.1.1 shows the weak attribute is kept. The same is true for the assign and strong attributes - you can specify them in the .h and omit them in the .m, if you do include them in the .m the two must match.

Having said that, I do not know if this is documented anywhere. But then the semantics of Objective-C are not formally defined anywhere either. So use at your own risk.

Recommendation: just repeat it.

CRD
  • 52,522
  • 5
  • 70
  • 86
  • how you check if the attribute is set? I am trying to find a way to check these things. Thanks! – Wingzero Jan 06 '15 at 05:17
  • At compile time just read the `.h`; for runtime lookup the function `property_getAttributes` in the documentation. – CRD Jan 06 '15 at 06:03