开发者

How to convert NSNumbers into integers from an NSArray(iPhone)

I have a a game for the iphone where the tags of 32 buttons are vital. I am trying to make a feature where it would save these 32 integers into an array so you can play a different game on the board with the same integers(tags) and then later come back to your game with the integers in the same way.(Not asking about multit开发者_高级运维asking or anything like that) There are some questions like this but none of their answers seem to work.

Right now I have:

savedGame = [NSArray arrayWithObjects: [NSNumber numberWithInt:a2.tag], 
                 [NSNumber numberWithInt:a4.tag], 
                 [NSNumber numberWithInt:a6.tag], 
                 [NSNumber numberWithInt:a8.tag], 
                 [NSNumber numberWithInt:b1.tag], 
                 [NSNumber numberWithInt:b3.tag], 
                 [NSNumber numberWithInt:b5.tag], 
                 [NSNumber numberWithInt:b7.tag],
                 [NSNumber numberWithInt:c2.tag], 
                 [NSNumber numberWithInt:c4.tag],

all the way to:

[NSNumber numberWithInt:h7.tag],nil];

for the part where you save the game

and:

a2.tag = [((NSNumber*)[savedGame objectAtIndex:0]) intValue];

all the way up to:

h7.tag = [((NSNumber*)[savedGame objectAtIndex:32]) intValue];

at the part where you resume your game. However, this code is apparently SO BAD that whenever it gets to this code in the ios simulator it crashes XCODE TOO(which gets very annoying because I need to force quit and start the program again)(its xcode 4)

Saved game is created in the .h like:

NSArray *savedGame;

I guess I will have to add something where it checks if there are 32 numbers in that second part--so if you could add that in your answer that would be great--and thanks in advance! I think the problem is with the second part of that code-- the g2.tag = [((NSNumber*)[savedGame objectAtIndex:25]) intValue];

I have also heard of NS Mutable Arrays, so maybe that could solve my problem?

Thanks for any and all help!! And I am also open to new ways--maybe not arrays--that could solve this problem!! Thankyou in advance!!!


Use an NSMutableArray instead and you can do things like

[savedGame replaceObjectAtIndex:4 withObject:[NSNumber numberWithInt:a8.tag]];

You can also save these in NSUserDefaults:

NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
[prefs setInteger:a4.tag forKey@"a4.tag"];
a4.tag = [prefs integerForKey:@"a4.tag"];

This latter approach will also allow you to use a for loop to set and retrieve these values.


You should really use an NSDictionary for this job. http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSDictionary_Class/Reference/Reference.html This will allow you to query your data using keys.


If you are suspecting that you could be accessing your array beyond its size, then you could check if the array has got the number of objects you expect by inspecting [savedGames count]. Don't know if it helps.

As a general suggestion, I would first run your unmodified program in debugging mode (if you are not doing that already), and possibly step-by-step in the region where it fails. So you will know exactly which is the offending statement.

In order to further try and understand what is happening, I would split your assignments in multiple lines:

NSNumber* number = [savedGame objectAtIndex:0];
a2.tag = [number intValue];

if you set a breakpoint on the first statement you can see what objectAtIndex is returning. Of course I understand it is big work to make all those changes.

As an alternative approach, since it seems to me that what matters to you are the ints (not the NSNumbers, you are using those only to store the ints somewhere), you could simply resort to using a C array of longs. This would avoid all the conversions back and forth. Of course, I am not sure whether you are really only interested in the ints.


Ummmm, based on the way your tags are situated, i guess its some kind of checkers like game? As in you have a 64 square grid but only half of the spaces are playable.

As for your comment about a mutable array, a mutable array just allows you to dynamically add or remove objects to the array. A regular NSArray has a fixed size and components (you cant add to it or remove from it). So you dont need to do the initWithObjects: part with a mutable array.

But, I really think you should restructure the way you are doing this. In this situation, you would want an object that has a location. Then you can just do

@interface GamePiece : NSObject
{
   CGPoint location; //an object with an x and y
}

@property (assign, readwrite) CGPoint location;

-(void)saveTheGame {
   [self setSavedGame:[[[NSMutableArray alloc] init] autorelease]];
   for(GamePiece *piece in allPieces)
   {
      [savedGame addObject:piece]
   }
}

Then in loadGame

if([savedGame count] == 32)
{
   [self setAllPieces:nil]; //allPieces is an array object of a "Game" that contains 
                            //references to all the pieces on the board

   for(GamePiece *piece in savedGame)
   {
       [allPieces addObject:piece];
   }
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜