Geo*_*rge 1 memory-management objective-c ios
例如,我是子类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
Run Code Online (Sandbox Code Playgroud)
该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
Run Code Online (Sandbox Code Playgroud)
然后,我删除了@synthesize myString = _myString;,并还有另外的错误对于这一说法[_myString release];作为Semantic Issue: Use of undeclared identifier '_text'
如果没有必要合成或释放myString上面的弱属性,我应该重写这样的代码:
该MyUIView.h文件中:
@interface MyUIView : UIView
@property (nonatomic, weak) NSString *myString;
@end
Run Code Online (Sandbox Code Playgroud)
该MyUIView.m文件中:
#import "MyUIView.h"
@implementation MyUIView
- (void)dealloc
{
[super dealloc];
}
// Other methods
@end
Run Code Online (Sandbox Code Playgroud)
weak 仅在启用ARC(或GC)时才是有效的属性属性.
您可以切换到ARC(强烈建议)或使用assign或unsafe_unretained属性,代价是失去weak参考的好处(参见委托属性声明中'weak'和'assign'之间的区别)
话虽这么说,我认为无论是assign和weak不会完成任何事情在特定情况下可取的.
除非您真的想避免对该字符串的强引用,否则相应的属性声明将如下所示:
@property (nonatomic, copy) NSString *myString;
Run Code Online (Sandbox Code Playgroud)
关于为什么copy而不是retain(或strong),您可以读取NSString属性:复制还是保留?
| 归档时间: |
|
| 查看次数: |
837 次 |
| 最近记录: |