iOS - NSMutableArray 在设置属性时显示对象超出范围
我已经实现了以下代码来将 NSMutableArray 分配给属性 -
NSMutableArray * anArray = [responseDictionary valueForKeyPath:@"tags"];
NSLog(@"The array length is=%d",[anArray count]);
for (NSString *s in anArray) {
NSLog(@"you are %@", s);
}
[self setActiveTagArray:anArray];
它可以很好地打印出字符串值。但是在 setter 函数中,如果我放置一个断点,我会看到它显示有两个对象,但它们“超出范围”。这意味着什么?我做错了什么?我的 getter 也没有获取任何值。属性功能 -
-(void)setActiveTagArray:(NSMutableArray *)tags
{
activeTagArray = [[NSMutableArray alloc] init];
activeTagArray = tags;
//NSLog(@"%@",[activeTagArray count]);
}
-(NSMutableArray *)getActiveTagArray
{
return activeTagArray;
}
I have implemented the following code to assign NSMutableArray to a property -
NSMutableArray * anArray = [responseDictionary valueForKeyPath:@"tags"];
NSLog(@"The array length is=%d",[anArray count]);
for (NSString *s in anArray) {
NSLog(@"you are %@", s);
}
[self setActiveTagArray:anArray];
It prints out the string values fine. But in the setter function, if I place a breakpoint I see that it shows there are two objects but they are "Out of Scope". What does this mean? What am I doing wrong? My getter also does not fetch any values. The property functions -
-(void)setActiveTagArray:(NSMutableArray *)tags
{
activeTagArray = [[NSMutableArray alloc] init];
activeTagArray = tags;
//NSLog(@"%@",[activeTagArray count]);
}
-(NSMutableArray *)getActiveTagArray
{
return activeTagArray;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
activeTagArray 是一个类变量也是一个属性。考虑使用 _activeTagArray 作为类变量名。然后在 .m 文件中只需使用 @synthesize activeTagArray = _activeTagArray; 即可完全获取后两个方法。
对评论的回应:
你说“我已经实现了以下代码来将 NSMutableArray 分配给属性”。我认为这意味着您的 .h 文件中有“
@property(nonatomic,retain)NSMutableArray *activeTagArray;
”。如果是这种情况,那么您可以通过otherObject'sNameForYourClassHere.activeTagArray
访问它。@synthesize 创建访问器 &给你突变者。
Is activeTagArray a class variable as well as a property. Consider using _activeTagArray as the class variable name. And then in the .m file just use
@synthesize activeTagArray = _activeTagArray;
, and for get the second two methods completely.Response to comment:
You said "I have implemented the following code to assign NSMutableArray to a property". I took this to mean you have "
@property (nonatomic, retain) NSMutableArray *activeTagArray;
" in your .h file. If this is the case then you would access it thruotherObject'sNameForYourClassHere.activeTagArray
.@synthesize create accessors & mutators for you.