integernsarraynsdatasparrow-framework

how to convert NSArray to NSData...or something


I am rather embarrassed to ask this, but I am on a time limit. basically, i need to get an array which can hold integers and then those integers can be used to plot a point on an image.

I am trying to make a program where random objects travel down the screen following certain paths, but at random. So far this is my code:

enemyPaths = [NSMutableArray array];//5 items (0-4) [enemyPaths addObject:[NSNumber numberWithInt:48]]; [enemyPaths addObject:[NSNumber numberWithInt:93]]; [enemyPaths addObject:[NSNumber numberWithInt:138]]; [enemyPaths addObject:[NSNumber numberWithInt:183]]; [enemyPaths addObject:[NSNumber numberWithInt:228]]; [enemyPaths retain];

That is the Array. What i want to do is to be able to make a random number generator and have the x point of an object use whatever value comes out of the array as a starting point:

double i = (arc4random() % 5);


NSData *data = [NSKeyedArchiver archivedDataWithRootObject:[enemyPaths objectAtIndex: i]];
 image.x = (int)data;

I am at a bit of a loss as to what to do, as my knowledge of converting NSarray to NSdata isnt very extensive and this is rather confusing. I would appreciate any help you can give.

Important note: I am using the Sparrow framework with this program, just so you know.


Solution

  • Why are you trying to archive the array in the first place? I think you want to do this:

    image.x = [[enemyPaths objectAtIndex:i] intValue];
    

    Here intValue is a method of the NSNumber class; it is the inverse of the numberWithInt method you used when creating the array.