Can I init an NSArray in a Singleton with specific

2019-09-08 15:50发布

问题:

I have a Singleton and I'd like to declare an NSArray, but I'd like to init it with 5 (non -consecutive) integers.

Right now it's declared in the .h>Interface, .h>@property, and .m>@synthesize.

How can I do this? Thanks.

回答1:

In your singleton's -init method, simply create and initialize it there:

- (id)init
{
    _array = [[NSArray arrayWithObjects:[NSNumber numberWithInt:5], [NSNumber numberWithInt:50], [NSNumber numberWithInt:3.72], [NSNumber numberWithInt:5], [NSNumber numberWithInt:96], nil] retain];
    return self;
}