进行比较时的奇怪行为
我有一个函数,用于通过检查特定属性是否等于大型数据数组中多个对象的特定值来构造数组。数据数组已完全初始化,但我无法从中检索任何对象。当我浏览代码时,XCode 告诉我下面的“thing”变量超出了范围。
这是由于我的函数导致的错误还是数据数组的问题? (我独立检查了数据数组,它有正确的计数和正确的成员)。
- (NSMutableArray *)parseForProperty:(NSString*)property EqualTo:(NSString*)value
{
NSMutableArray *result = [[NSMutableArray alloc] init];
SEL selector = NSSelectorFromString(property);
NSLog(@"parseProp");
for (RCDetailItem *thing in [[self defaultStore] parsedData])
{
NSLog(@"Thing Title: %@", thing.title);
if ([thing performSelector:selector] == value)
{
[result addObject:thing];
}
}
return result;
}
I have a function that I use to construct an array by checking if a particular property is equal to a particular value of an object among many in a large data array. The data array is fully initialized, but I cannot retrieve any objects out of it. When I go through the code, XCode tells me that the "thing" variable below is out of scope.
Is this an error due to my function or is the problem with the data array? (I checked the data array independently and it's got the right count and the right members).
- (NSMutableArray *)parseForProperty:(NSString*)property EqualTo:(NSString*)value
{
NSMutableArray *result = [[NSMutableArray alloc] init];
SEL selector = NSSelectorFromString(property);
NSLog(@"parseProp");
for (RCDetailItem *thing in [[self defaultStore] parsedData])
{
NSLog(@"Thing Title: %@", thing.title);
if ([thing performSelector:selector] == value)
{
[result addObject:thing];
}
}
return result;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
目前,您不比较字符串而是引用。
您可以尝试:
但我确信它会解决,因为您提到“下面的变量超出范围”。是编译错误吗?
Currently, you don't compare Strings but reference.
You can try :
But I'm sure it will resolve because you refer to "variable below is out of scope". Is it a compile error ?
@Arnaud del 是对的,Objective C 对象不能通过比较指针来比较它们(不同的实例显然有不同的指针值,即使它们具有相同的值)。您应该使用
-isEqual:
消息按值比较它们。下面的变量超出范围
调试器消息通常是由编译器设置中启用的代码优化引起的,因此您的变量会被编译器隐式擦除,并且调试器没有机会显示其值。确保您正在调试调试配置,并且通过将优化级别设置为“无”来关闭优化。
@Arnaud del is right, Objective C objects cannot be compared by comparing pointers to them (different instances obviously have different pointer values, even if they have the same value). You should use
-isEqual:
message to compare them by value.Variable below is out of scope
debugger message is often caused by code optimization enabled in compiler settings, so your variable is implicitly wiped out by compiler, and the debugger has no chance to show it's value.Make sure you are debugging a Debug configuration and you have optimizations turned off by setting Optimization Level to None.