在为 iPhone 编程时如何保持 NSArray 对所有方法可用

发布于 2024-08-18 06:21:10 字数 231 浏览 4 评论 0原文

我正在制作一个使用一些 NSArray 的 iPhone 应用程序。现在我必须在每个方法中生成数组。我知道必须有一种更有效的方法来做到这一点,比如在初始化期间创建 NSArray,然后可供所有方法使用。问题是,当我在 ViewDidLoad 方法中创建 NSArrays 时,当我尝试在其他方法中调用它们时,我收到一条错误,指出它们无法识别。也许我试图错误地初始化 NSArray 或者在错误的位置?任何有关这方面的信息将不胜感激。感谢您抽出时间。

I am making an iPhone app that uses a few NSArrays. Right now I have to generate the arrays in each method. I know there has to be an more efficient way of doing this, like having the NSArrays created during initialization and then being available to all methods after that. The problem is, that when I create the NSArrays in the ViewDidLoad method, when I try to call them in other methods, I get an error stating that they are not recognized. Perhaps I am trying to initialize the NSArrays incorrectly or maybe in the wrong spot? Any info on this would be appreciated. Thank you for your time.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

看海 2024-08-25 06:21:10

您应该将数组声明为属性。合成它并在 ViewDidLoad 方法中初始化。

即标头的

@interface AddFriendViewController : UIViewController {
    NSArray *myFriends;
}

@property (nonatomic, retain) NSarray *myFriends;

@end

实现:

@synthesize myFriends;

- (void)viewDidLoad {
    [super viewDidLoad];
    // init and alloc your myFriendsArray here
}

You should declare your array as a property. Synthesize it and initialize in your ViewDidLoad Method.

i.e. the header

@interface AddFriendViewController : UIViewController {
    NSArray *myFriends;
}

@property (nonatomic, retain) NSarray *myFriends;

@end

the implementation:

@synthesize myFriends;

- (void)viewDidLoad {
    [super viewDidLoad];
    // init and alloc your myFriendsArray here
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文