开发者

no hit on if-statement

I am trying to get a hit on my if-statement. I fetch qDiff (integer32) from the Core Data db and everything works except get a hit on the if-statements:

NSString *diff;

NSArray *fetchedObjects = [qContext executeFetchRequest:fetchRequest error:&error];
for (NSManagedObject *info in fetchedObjects) {
    NSLog(@"idQ: %@", [info valueForKey:@"idQ"]);
    NSLog(@"qDiff: %@", [info valueForKey:@"qDiff"]); //<<<<<<<<<Print correct number

    diff = [info valueForKey:@"qDiff"];
    NSLog(@"diff: %@", diff);       //<<<<<<<<<Print correct number

    if ([diff isEqual:@"1"]) NSLog(@"EASY");

    //if ([diff isEqualToString: @"2"]) NSLog(@"MEDIUM");
    //if ([[info valueForKey:@"qDiff"] isEqualToString: @"3"]) NSLog(@"HARD"); //<<<<<<tried this first


    NSLog(@"question: %@", [info valueForKey:@"question"]);
}        
[fetchRequest release];      

I have been trying different If-statements but it does not trigger (print the NSLog).

Anyone that can give me a hint?

=========UPDATE==========

I changed to:

if ([diff isEqualToString:@"1"]) NSLog(@"EASY");

And got the following:

2010-12-12 15:39:49.321 XX_v2[2950:207] idQ: 0

2010-12-12 15:39:49.321 XX_v2[2950:207] qDiff: 1

2010-12-12 15:39:49.322 XX_v2[2950:207] diff: 1

2010-12-12 15:39:49.322 XX_v2[2950:207] -[_PFCachedNumber isEqualToString:]: unrecognized selec开发者_JS百科tor sent to instance 0xa0f9008

2010-12-12 15:39:49.324 XX_v2[2950:207] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[_PFCachedNumber isEqualToString:]: unrecognized selector sent to instance 0xa0f9008'

==========UPDATE #2=======

Did try:

if ([diff isEqual:@"1"]) NSLog(@"EASY");

no difference in output:

2010-12-12 15:55:57.381 XX_v2[3151:207] idQ: 0

2010-12-12 15:55:57.382 XX_v2[3151:207] qDiff: 1

2010-12-12 15:55:57.382 XX_v2[3151:207] diff: 1

2010-12-12 15:55:57.382 XX_v2[3151:207] question: Do you want a new?

However, in the debugger i found:

'diff' "Variable not a CFString"

========UPDATE#3========

Updated the code above to show definition of 'diff'.

========UPDATE#4========

I did finally solve the problem with this statement:

if ([[info valueForKey:@"qDiff"] intValue] == 1) NSLog(@"EASY");


You are treating qDiff as an NSString while Core Data saves it as an integer32 and exposes it as an NSNumber. Try this:

if ([diff isEqualToNumber:[NSNumber numberWithInt:1]]) NSLog(@"EASY");

Or this:

if ([diff intValue] == 1) NSLog(@"EASY");


You said qDiff is integer32 in the data model. Then, the class is NSNumber in the Objective-C code, not NSString.

You should understand that in Objective-C, string objects, number objects, and non-object numbers are not converted automatically. And == doesn't work magically for all of the types and the classes.

So, you need to - use the appropriate classes explicitly, and then - convert them explicitly, - and compare them with appropriate ways.

So, use

NSNumber* diff = [info valueForKey:@"qDiff"];

and perform either

if([diff isEqualTo:[NSNumber numberWithInteger:3]]){ ... }

or

if([diff intValue] == 3){ ... }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜