简体   繁体   English

比较时的奇怪行为

[英]Strange Behavior when making comparisons

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. 当我检查代码时,XCode告诉我下面的“ thing”变量不在范围内。

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;
}

Currently, you don't compare Strings but reference. 当前,您不比较字符串而是引用。 You can try : 你可以试试 :

NSString * thingProperty = [thing performSelector:selector];
 if ([thingProperty isEqualToString:value])
..

But I'm sure it will resolve because you refer to "variable below is out of scope". 但是我确信它会解决,因为您提到“下面的变量超出范围”。 Is it a compile error ? 它是编译错误吗?

  1. @ 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). @ Arnaud del是正确的,无法通过比较指向它们的指针来比较Objective C对象(不同的实例显然具有不同的指针值,即使它们具有相同的值)。 You should use -isEqual: message to compare them by value. 您应该使用-isEqual:消息按值比较它们。

  2. 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. Variable below is out of scope调试器消息通常是由编译器设置中启用的代码优化引起的,因此编译器会隐式清除您的变量,并且调试器没有机会显示其值。

    Make sure you are debugging a Debug configuration and you have optimizations turned off by setting Optimization Level to None. 通过将“ 优化级别”设置为“无”,确保您正在调试调试配置,并且已关闭优化

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM