NSArray count = 1 but I'm getting "index 0 beyond bounds for empty array"
error
I am confused why I am getting an "NSArray count = 1 but I'm getting
"index 0 beyond bounds for empty array" error. I have logged the array and
array count, but when I execute the line to set the highscore_ I get the
error.
Here is the relevant code:
//Query Players Table
AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
NSManagedObjectContext *context = [appDelegate managedObjectContext];
NSEntityDescription *playerEntity=[NSEntityDescription
entityForName:@"Player" inManagedObjectContext:context];
NSFetchRequest *fetch=[[NSFetchRequest alloc] init];
[fetch setEntity:playerEntity];
NSPredicate *p = [NSPredicate predicateWithFormat:@"playersScore != nil"];
[fetch setPredicate:p];
NSError *fetchError;
NSArray *array=[context executeFetchRequest:fetch error:&fetchError];
NSLog(@"%@",array);
NSLog(@"%i", [array count]);
Player player = (Player)[array objectAtIndex:0];
NSLog(@"%@", player.playersScore);
highscore_ = [player.playersScore longLongValue];
The Log for the array returns this:
[23260:907] ( " (entity: Player; id: 0x1f073420 ; data: {\n
playerHomeStateLat = \"38.82523\";\n playerHomeStateLong =
\"-76.74451000000001\";\n playersFarthestPlate = 0;\n
playersHomeStateVanity = RAYVNFN;\n playersLastDateFound = nil;\n
playersLastPlateFound = nil;\n playersName = nil;\n playersScore = 0;\n
playersState = Maryland;\n playersTotalPlatesFound = 0;\n playersZipcode =
0;\n})" )
The Log for Array Count returns: 1
[23260:907] 1
and the Log for player.playersScore returns: 0
[23260:907] 0
Here is the entire error message the I get when I execute the *highscore_
= [player.playersScore longLongValue];* line:
[23260:907] * Terminating app due to uncaught exception
'NSRangeException', reason: '* -[__NSArrayI objectAtIndex:]: index 0
beyond bounds for empty array' * First throw call stack: (0x333d53e7
0x3b0d0963 0x3332021d 0xe952d 0x352cf087 0x352cf03b 0x352cf015 0x352ce8cb
0x352cedb9 0x351f75f9 0x351e48e1 0x351e41ef 0x36efc5f7 0x36efc227
0x333aa3e7 0x333aa38b 0x333a920f 0x3331c23d 0x3331c0c9 0x36efb33b
0x352382b9 0xaaa29 0xaa9b0) libc++abi.dylib: terminate called throwing an
exception
This code will run fine if the player.playersScore value is greater than 0.
Any idea what is wrong. Thanks.
No comments:
Post a Comment