开发者

Problem adding data to NSMutableArray

开发者 https://www.devze.com 2023-01-02 09:42 出处:网络
I\'m trying to add the value playerhight, among other values to NSMutableArray, but it won\'t let me build and I can\'t figure out why.

I'm trying to add the value playerhight, among other values to NSMutableArray, but it won't let me build and I can't figure out why.

#import "iGameViewController.h"

@implementation iGameViewController

@synthesize player;

    int playerheight;

NSMutableArray *location = [NSMutableArray arrayWithCapacity:3];

-(IBAction)up;{
    player.center = CGPointMake(player.center.x, player.center.y - 5);
    location = [NSMutableArray new];
[location addObject:player.center.x];
[location addObject:player.center.y];
[location addObject开发者_JS百科:playerheight];

}


You can only add objects to arrays in Objective-C. player.center is a struct, and player.center.x is a CGFloat, neither of which are objects. If you want to add the point as a single object, you can box it in an NSValue object. If you want to add the floats directly, they have to be boxed in NSNumber objects.


What do you need this line for?

 location = [NSMutableArray new];

The rest is already explained by Dave.


playerheight and location should be instance variables, declared in your .h file and then set it in the init method. You can't initialize a global Objective-C object in this way. If you really want it to be global, you can initialize it as nil, and then each time to need it, check to make sure it's been created and it not create it.

0

精彩评论

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