【发布时间】:2013-11-13 17:41:24
【问题描述】:
例如,我将UIView 子类化,其中定义了一个名为myString 的弱属性。 @synthesize myString = _myString; 语句有错误消息:Semantic Issue: @synthesize of 'weak' property is only allowed in ARC or GC mode。
MyUIView.h 文件:
@interface MyUIView : UIView
@property (nonatomic, weak) NSString *myString;
@end
MyUIView.m 文件:
#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
然后我删除了@synthesize myString = _myString;,这条语句[_myString release]; 出现了另一个错误Semantic Issue: Use of undeclared identifier '_text'
如果不需要像上面myString这样的弱属性合成和释放,我是否应该像这样重写代码:
MyUIView.h 文件:
@interface MyUIView : UIView
@property (nonatomic, weak) NSString *myString;
@end
MyUIView.m 文件:
#import "MyUIView.h"
@implementation MyUIView
- (void)dealloc
{
[super dealloc];
}
// Other methods
@end
【问题讨论】:
-
读取错误。
@synthesize of 'weak' property is only allowed in ARC。该文件未在启用 ARC 的情况下进行编译。 -
是否有特殊原因声明该属性为弱?
-
一个名为 label 但不是 UILabel 的属性:命名不佳。
-
致@MartinR,上面的代码是另一个人写的,我猜是用ARC写的。但我想将其更改为手动保留释放样式。
-
致@vikingosegundo,属性名称已从
label更改为myString:D
标签: ios objective-c memory-management