Reputation: 3619
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
Upvotes: 1
Views: 900
Reputation: 9810
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
Upvotes: 0
Reputation: 108101
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?
Upvotes: 7