1

For example, I subclass UIView, in which a weak property called myString is defined. There is an error message for @synthesize myString = _myString; statement: Semantic Issue: @synthesize of 'weak' property is only allowed in ARC or GC mode.

The MyUIView.h file:

@interface MyUIView : UIView

@property (nonatomic, weak) NSString *myString;

@end

The MyUIView.m file:

#import "MyUIView.h"

@implementation MyUIView

@synthesize myString = _myString; // This statement causes an error whose message is Semantic Issue: @synthesize of 'weak' property is only allowed in ARC or GC mode

- (void)dealloc
{
    [_myString release];

    [super dealloc];
}

// Other methods

@end

Then I removed the @synthesize myString = _myString; and there goes another error for this statement [_myString release]; as Semantic Issue: Use of undeclared identifier '_text'

If it's not necessary to synthesize nor release a weak property like myString above, should I re-write the code like this:

The MyUIView.h file:

@interface MyUIView : UIView

@property (nonatomic, weak) NSString *myString;

@end

The MyUIView.m file:

#import "MyUIView.h"

@implementation MyUIView

- (void)dealloc
{        
    [super dealloc];
}

// Other methods

@end
Gabriele Petronella
  • 106,943
  • 21
  • 217
  • 235
George
  • 3,384
  • 5
  • 40
  • 64

2 Answers2

7

weak is a valid property attribute only when ARC (or GC) is enabled.

You can either switch to ARC (strongly advisable) or use an assign or unsafe_unretained attribute instead, at the cost of losing the benefit of a weak reference (see Whats the difference between 'weak' and 'assign' in delegate property declaration)

That being said, I think both assign and weak won't accomplish anything desirable in the specific case.

Unless you really want to avoid a strong reference to that string, the appropriate property declaration would go as follows:

@property (nonatomic, copy) NSString *myString;

On why copy and not retain (or strong), you can read NSString property: copy or retain?

Community
  • 1
  • 1
Gabriele Petronella
  • 106,943
  • 21
  • 217
  • 235
0

NSString (and any other class that is immutable with a mutable subclass) should be synthesized to copy.

@property (copy) NSString *myString;

the first comment: Yes. (nonatomic, copy) is fine.

the second comment: You don't need to, it is assumed as well in modern objective-c syntax. There will be a _myString ivar created either way. Yes you need to release _myString if you use copy

Gabriele Petronella
  • 106,943
  • 21
  • 217
  • 235
Jesse Naugher
  • 9,780
  • 1
  • 41
  • 56
  • 2
    It makes always sense to use "copy" with a NSString property. That is independent of ARC vs. Manual Reference Counting. – Martin R Nov 02 '13 at 18:19
  • To @JesseNaugher, Can I use `nonatomic` in the same time with `copy`, as `@property (nonatomic, copy) NSString *myString;`? – George Nov 02 '13 at 18:21
  • To @JesseNaugher, If I use `copy` attribute for the property `myString`, do I need to `@synthesize myString = _myString` (I know this is default, so I mean will there be an default `_myString` ivar made for the property with copy attribute?) And if there is `_myString` ivar made, do I need to `[_myString release]` in the `dealloc` method? – George Nov 02 '13 at 18:25
  • 3
    *"properties are assumed to be nonatomic unless otherwise specified"* - That is not correct. The default is "atomic" (unfortunately). – Martin R Nov 02 '13 at 18:38
  • To @JesseNaugher, In your first sentence, **"and any other class that is immutable with a mutable subclass"**, do you mean that, for example, if I want a property in the class of `NSArray`, it should be synthesized to `copy`, because there is `NSMutableArray`. Then why it should always be `copy` if there exists "mutable" subclass? – George Nov 02 '13 at 19:04
  • see here: http://stackoverflow.com/questions/387959/nsstring-property-copy-or-retain/388002#388002 – Jesse Naugher Nov 02 '13 at 19:05
  • @congliu the fact that a mutable subclass exist is precisely why you use `copy`. You don't wan the client to be able to modify the collection after it has been passed to you. `copy` solves the case in which you accept a `NSArray` but the client provides a `NSMutableArray`. It's also worth noting that `copy` on a `NSArray` returns the array itself, instead of a copy, so you can have the cake and eat it too. – Gabriele Petronella Nov 02 '13 at 19:20
  • @JesseNaugher by the way I think your post, as currently stands, doesn't answer the question. You are not explaining why weak variables cannot be synthesized in MRC, which is what the OP asked. – Gabriele Petronella Nov 02 '13 at 19:44
  • @JesseNaugher btw please use the comment system to reply to comments instead of editing your answers to include those. – Filip Radelic Nov 02 '13 at 19:56