Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 是否可能或有必要在手动保留释放模式下合成/释放弱属性_Ios_Objective C_Memory Management - Fatal编程技术网

Ios 是否可能或有必要在手动保留释放模式下合成/释放弱属性

Ios 是否可能或有必要在手动保留释放模式下合成/释放弱属性,ios,objective-c,memory-management,Ios,Objective C,Memory Management,例如,I子类UIView,其中定义了名为myString的弱属性。@synthesis myString=\u myString语句:语义问题:@仅在ARC或GC模式下允许合成'weak'属性 MyUIView.h文件: @interface MyUIView : UIView @property (nonatomic, weak) NSString *myString; @end #import "MyUIView.h" @implementation MyUIView @synth

例如,I子类
UIView
,其中定义了名为
myString
的弱属性。
@synthesis myString=\u myString语句:
语义问题:@仅在ARC或GC模式下允许合成'weak'属性

MyUIView.h
文件:

@interface MyUIView : UIView

@property (nonatomic, weak) NSString *myString;

@end
#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
@interface MyUIView : UIView

@property (nonatomic, weak) NSString *myString;

@end
#import "MyUIView.h"

@implementation MyUIView

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

// Other methods

@end
MyUIView.m
文件:

@interface MyUIView : UIView

@property (nonatomic, weak) NSString *myString;

@end
#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
@interface MyUIView : UIView

@property (nonatomic, weak) NSString *myString;

@end
#import "MyUIView.h"

@implementation MyUIView

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

// Other methods

@end
然后我删除了
@synthesis myString=\u myString
这个语句出现了另一个错误
[\u myString release]as
语义问题:使用未声明的标识符“\u text”

如果没有必要合成或释放上面的
myString
这样的弱属性,我是否应该这样重新编写代码:

MyUIView.h
文件:

@interface MyUIView : UIView

@property (nonatomic, weak) NSString *myString;

@end
#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
@interface MyUIView : UIView

@property (nonatomic, weak) NSString *myString;

@end
#import "MyUIView.h"

@implementation MyUIView

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

// Other methods

@end
MyUIView.m
文件:

@interface MyUIView : UIView

@property (nonatomic, weak) NSString *myString;

@end
#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
@interface MyUIView : UIView

@property (nonatomic, weak) NSString *myString;

@end
#import "MyUIView.h"

@implementation MyUIView

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

// Other methods

@end
NSString
(以及任何其他与可变子类不可变的类)应合成为
copy

@property (copy) NSString *myString;
第一条评论:是的。(非原子,复制)可以

第二条评论:不需要,在现代objective-c语法中也假定了这一点。无论哪种方式,都会创建一个
\u myString
ivar。是的,如果使用
copy
NSString
(以及任何其他与可变子类不可变的类),则需要释放
\u myString
,并将其合成为
copy

@property (copy) NSString *myString;
第一条评论:是的。(非原子,复制)可以


第二条评论:不需要,在现代objective-c语法中也假定了这一点。无论哪种方式,都会创建一个
\u myString
ivar。是的,如果您使用
copy

weak
仅在启用ARC(或GC)时才是有效的属性属性,则需要释放
\u myString

您可以切换到ARC(强烈建议)或使用
赋值
不安全_未保留
属性,但代价是失去
引用的好处(请参阅)

话虽如此,我认为
assign
weak
在特定情况下都无法实现任何理想的效果

除非您确实希望避免对该字符串的强引用,否则适当的属性声明如下所示:

@property (nonatomic, copy) NSString *myString;

关于为什么
copy
而不是
retain
(或
strong
)的问题,您可以阅读
只有在启用ARC(或GC)时,弱
才是有效的属性属性

您可以切换到ARC(强烈建议)或使用
赋值
不安全_未保留
属性,但代价是失去
引用的好处(请参阅)

话虽如此,我认为
assign
weak
在特定情况下都无法实现任何理想的效果

除非您确实希望避免对该字符串的强引用,否则适当的属性声明如下所示:

@property (nonatomic, copy) NSString *myString;

关于为什么
copy
而不是
retain
(或
strong
),您可以读取错误<代码>@仅允许在ARC中合成“弱”属性
。该文件未在启用ARC的情况下编译。是否有特殊原因将该属性声明为弱属性?一个名为label但不是UILabel的属性:命名不好。对于@MartinR,上述代码是由另一个人编写的,我猜它是用ARC编写的。但我想将其更改为手动保留发布样式。对于@vikingosegundo,属性名称从
标签更改为
myString
:请担心错误<代码>@仅允许在ARC中合成“弱”属性
。该文件未在启用ARC的情况下编译。是否有特殊原因将该属性声明为弱属性?一个名为label但不是UILabel的属性:命名不好。对于@MartinR,上述代码是由另一个人编写的,我猜它是用ARC编写的。但我想将其更改为手动保留发布样式。对于@vikingosegundo,属性名称从
label
更改为
myString
:DIt总是有意义地将“copy”与NSString属性一起使用。这与ARC和手动引用计数无关。对于@JesseNaugher,我可以将
非原子
复制
同时使用,作为
@property(非原子,复制)NSString*myString
?对于@JesseNaugher,如果我为属性
myString
使用
copy
属性,我是否需要
@synthesis myString=\u myString
(我知道这是默认的,所以我的意思是是否会为具有copy属性的属性生成默认的
\u myString
ivar)以及如果存在
\u myString
ivar,我是否需要在
dealloc
方法中
[\u myString release]
“属性被假定为非原子的,除非另有规定”-这是不正确的。默认值为“原子”(不幸的是)。对于@JesseNaugher,在您的第一句话中,“以及任何其他与可变子类不可变的类”,您的意思是,例如,如果我想要
NSArray
类中的属性,它应该合成为
copy
,因为存在
NSMutableArray
。那么,如果存在“可变”子类,为什么它总是
copy
?将“copy”与NSString属性一起使用总是有意义的。这与ARC和手动引用计数无关。对于@JesseNaugher,我可以将
非原子
复制
同时使用,作为
@property(非原子,复制)NSString*myString
?对于@JesseNaugher,如果我为属性
myString
使用
copy
属性,我是否需要
@synthesis myString=\u myString
(我知道这是默认的,所以我的意思是是否会为具有copy属性的属性生成默认的
\u myString
ivar)以及如果存在
\u myString
ivar,我是否需要在
dealloc
方法中
[\u myString release]