简体   繁体   中英

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.

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). You should use -isEqual: message to compare them by value.

  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.

    Make sure you are debugging a Debug configuration and you have optimizations turned off by setting Optimization Level to None.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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