简体   繁体   中英

Retrieve NSString from core-data error

The app needs to retrieve data from core-data then compare one of the values with an IF STATEMENT

MyAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
        NSManagedObjectContext *context = [appDelegate managedObjectContext];


        NSEntityDescription *getDetails = [NSEntityDescription entityForName:@"Detail" inManagedObjectContext:context];
        NSFetchRequest *request = [[NSFetchRequest alloc] init];

        [request setEntity:getDetails];
        NSPredicate *pred = [NSPredicate predicateWithFormat:@"(genID = %i)", genID];
        [request setPredicate:pred];

        NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:@"genID" ascending:YES];
        [request setSortDescriptors:[NSArray arrayWithObject:sort]];


        NSError *error;
        NSArray *objects = [context executeFetchRequest:request error:&error];

        for (NSManagedObject *oneObject in objects) {   



            NSString *type = [oneObject valueForKey:@"type"];


            if (type == @"straight") {
                NSLog(@"straight");
                //straight logic removed
            }else if (type == @"Left") {
                NSLog(@"Left");
                //Left logic removed
            }else {
                NSLog(@"Else");
                //Else logic removed

            }


            }

The issue is that it never goes into "straight" or "left" always the ELSE. Stepping through the code I can see that values do match straight and left, even dumped them into a log file shows them fine. What is wrong?

You need to compare strings using isEqual: or isEqualToString: or compare: .

For instance:

NSString *type = [oneObject valueForKey:@"type"];
if ([type isEqualToString:@"straight"])
    // etc.

Search StackOverflow for "NSString equal" or "NSString compare" if you need more explanation. You aren't anywhere near the first person to ask this question, and you definitely won't be the last.

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