我可以在具有特定值(如 const)的单例中初始化 NSArray 吗? [英] Can I init an NSArray in a Singleton with specific values (like a const)?

查看:27
本文介绍了我可以在具有特定值(如 const)的单例中初始化 NSArray 吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个单例,我想声明一个 NSArray,但我想用 5 个(非连续)整数初始化它.

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

现在它在 .h>Interface、.h>@property 和 .m>@synthesize 中声明.

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

我该怎么做?谢谢.

推荐答案

在单例的 -init 方法中,只需在那里创建和初始化它:

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;
}

这篇关于我可以在具有特定值(如 const)的单例中初始化 NSArray 吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆