NSMutableArray 作为实例变量始终为 null

发布于 2025-01-01 15:24:37 字数 2189 浏览 1 评论 0原文

浪费了很多时间后,我正式向专家寻求帮助!

我的问题在于使用 NSMutableArray 作为实例变量,并尝试在类中的方法中添加对象并返回数组。我显然做了一些根本性错误的事情,并且非常感谢您的帮助...我已经尝试了 stackoverflow 上其他类似问题的所有建议,阅读了苹果文档,以及基本上我能想到的所有尝试和错误编码的组合。可变数组总是返回 (null)。我什至尝试为它们创建属性,但数组仍然返回(null),然后由于设置属性时的保留以及类的 init 方法中的 init,我还遇到了内存管理问题。

这就是我想要做的:

1)循环遍历一系列 UISwitches,如果它们“打开”,则向 NSMutableArray 添加一个字符串

2)将此可变数组分配给另一种方法中的另一个数组

任何帮助非常感谢,

Andy

对于一些代码

fruitsViewController.hfruitsViewController.m

#import <UIKit/UIKit.h>

@interface fruitsViewController : UIViewController
{
    NSMutableArray *fruitsArr;
    UISwitch *appleSwitch;
    UISwitch *orangeSwitch;
}

@property (nonatomic,retain) NSMutableArray *fruitsArr; // ADDED ON EDIT
@property (nonatomic,retain) IBOutlet UISwitch *appleSwitch;
@property (nonatomic,retain) IBOutlet UISwitch *orangeSwitch;

- (IBAction)submitButtonPressed:(id)sender;
@end

...

#import "fruitsViewController.h"

@implementation fruitsViewController

@synthesize fruitsArr; // ADDED ON EDIT
@synthesize appleSwitch, orangeSwitch;

/* COMMENTED OUT ON EDIT
-(id)init
{
    if (self = [super init]) {
        // Allocate memory and initialize the fruits mutable array
        fruitsArr = [[NSMutableArray alloc] init];
    }
    return self;
}
*/

// VIEW DID LOAD ADDED ON EDIT
- (void)viewDidLoad
{
    self.fruitsArr = [[NSMutableArray alloc] init];
}

- (void)viewDidUnload
{
    self.fruitsArr = nil;
    self.appleSwitch = nil;
    self.orangeSwitch = nil;
}

- (void)dealloc
{
    [fruitsArr release];
    [appleSwitch release];
    [orangeSwitch release];
    [super dealloc];
}

- (IBAction)submitButtonPressed:(id)sender
{
    if ([self.appleSwitch isOn]) {
        [self.fruitsArr addObject:@"Apple"; // 'self.' ADDED ON EDIT
    }
    if ([self.orangeSwitch isOn]) {
        [self.fruitsArr addObject:@"Orange"; // 'self.' ADDED ON EDIT
    }
    NSLog(@"%@",self.fruitsArr); // Why is this returning (null) even if the switches are on?!
    [fruitsArr addObject:@"Hello World";
    NSLog(@"%@",self.fruitsArr); // Even trying to add an object outside the if statement returns (null)
}
@end

After many hours wasted, I officially turn to the experts for help!

My problem lies with using a NSMutableArray as an instance variable, and trying to both add objects and return the array in a method in my class. I am obviously doing something fundamentally wrong and would be grateful for help...I have already tried all the suggestions from other similar questions on stackoverflow, read apples documentation, and basically all combinations of trial and error coding I can think of. The mutable array just alway returns (null). I've even tried creating properties for them, but still the array returns (null) and then I also am running into memory management problems due to the retain while setting the property, and the init in the init method for the class.

Here is what I am trying to do:

1) Loop through a series of UISwitches and if they are 'switched on', add a string to the NSMutableArray

2) Assign this mutable array to another array in another method

Any help much appreciated,

Andy

And for some code...

fruitsViewController.h

#import <UIKit/UIKit.h>

@interface fruitsViewController : UIViewController
{
    NSMutableArray *fruitsArr;
    UISwitch *appleSwitch;
    UISwitch *orangeSwitch;
}

@property (nonatomic,retain) NSMutableArray *fruitsArr; // ADDED ON EDIT
@property (nonatomic,retain) IBOutlet UISwitch *appleSwitch;
@property (nonatomic,retain) IBOutlet UISwitch *orangeSwitch;

- (IBAction)submitButtonPressed:(id)sender;
@end

fruitsViewController.m

#import "fruitsViewController.h"

@implementation fruitsViewController

@synthesize fruitsArr; // ADDED ON EDIT
@synthesize appleSwitch, orangeSwitch;

/* COMMENTED OUT ON EDIT
-(id)init
{
    if (self = [super init]) {
        // Allocate memory and initialize the fruits mutable array
        fruitsArr = [[NSMutableArray alloc] init];
    }
    return self;
}
*/

// VIEW DID LOAD ADDED ON EDIT
- (void)viewDidLoad
{
    self.fruitsArr = [[NSMutableArray alloc] init];
}

- (void)viewDidUnload
{
    self.fruitsArr = nil;
    self.appleSwitch = nil;
    self.orangeSwitch = nil;
}

- (void)dealloc
{
    [fruitsArr release];
    [appleSwitch release];
    [orangeSwitch release];
    [super dealloc];
}

- (IBAction)submitButtonPressed:(id)sender
{
    if ([self.appleSwitch isOn]) {
        [self.fruitsArr addObject:@"Apple"; // 'self.' ADDED ON EDIT
    }
    if ([self.orangeSwitch isOn]) {
        [self.fruitsArr addObject:@"Orange"; // 'self.' ADDED ON EDIT
    }
    NSLog(@"%@",self.fruitsArr); // Why is this returning (null) even if the switches are on?!
    [fruitsArr addObject:@"Hello World";
    NSLog(@"%@",self.fruitsArr); // Even trying to add an object outside the if statement returns (null)
}
@end

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

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

发布评论

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

评论(5

開玄 2025-01-08 15:24:37

看起来你的 init 函数从未被调用过。如果您从 NIB 初始化此视图控制器,则需要使用 initWithCoder。如果没有,只需在viewDidLoad中声明你的fruitsArr即可。

It seems like your init function is never called. If you're initializing this view controller from a NIB, you need to use initWithCoder. If not, just declare your fruitsArr in viewDidLoad.

请止步禁区 2025-01-08 15:24:37

使用 view did load 而不是 init...

- (void)viewDidLoad
{
    fruitsArr = [[NSMutableArray alloc] init];
}

Use view did load instead of init...

- (void)viewDidLoad
{
    fruitsArr = [[NSMutableArray alloc] init];
}
盛夏尉蓝 2025-01-08 15:24:37

init 更改为 viewDidLoad 并看看会发生什么

Change that init for viewDidLoad and see what happens

在巴黎塔顶看东京樱花 2025-01-08 15:24:37

您的 init 方法是否被调用过(在 complicationsViewController 中)。添加一个 NSLog 来检查这一点,您可能会调用 initWithNib:

viewDidUnload 处,您应该删除 self.fruitsArr = nil;,或者,如果您想保留它,则在 viewDidLoad 中初始化 fruitsArr(并且从 init 中删除它)。

Is your init method ever being called (in complicationsViewController). Add a NSLog to check this, you might be calling initWithNib: maybe.

At viewDidUnload you should remove self.fruitsArr = nil;, or, if you want to keep it, then initialize the fruitsArr in viewDidLoad (and remove it from init).

雪化雨蝶 2025-01-08 15:24:37

因为fruitsArr 没有被初始化。
你应该首先这样做:
FruitsArr = [[NSMutableArray alloc] init];

所以,我认为你在使用fruitsArr之前不要运行-(id)init。

because fruitsArr don't be init.
you should do this first:
fruitsArr = [[NSMutableArray alloc] init];

so, I think you don't run - (id)init before you use fruitsArr.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文