0

应用程序需要从核心数据中检索数据,然后将其中一个值与 IF 语句进行比较

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

            }


            }

问题是它永远不会进入“直”或“左”总是 ELSE。单步执行代码,我可以看到值确实匹配直线和左侧,甚至将它们转储到日志文件中也显示它们很好。怎么了?

4

1 回答 1

1

您需要使用isEqual:orisEqualToString:或比较字符串compare:

例如:

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

如果您需要更多解释,请在 StackOverflow 中搜索“NSString equal”或“NSString compare”。你不是第一个问这个问题的人,你肯定不会是最后一个。

于 2012-05-15T19:45:20.423 回答