NSMutableArray 作为实例变量始终为 null
浪费了很多时间后,我正式向专家寻求帮助!
我的问题在于使用 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
看起来你的 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.
使用 view did load 而不是 init...
Use view did load instead of init...
将
init
更改为viewDidLoad
并看看会发生什么Change that
init
forviewDidLoad
and see what happens您的
init
方法是否被调用过(在complicationsViewController
中)。添加一个 NSLog 来检查这一点,您可能会调用initWithNib:
。在
viewDidUnload
处,您应该删除self.fruitsArr = nil;
,或者,如果您想保留它,则在 viewDidLoad 中初始化fruitsArr
(并且从init
中删除它)。Is your
init
method ever being called (incomplicationsViewController
). Add a NSLog to check this, you might be callinginitWithNib:
maybe.At
viewDidUnload
you should removeself.fruitsArr = nil;
, or, if you want to keep it, then initialize thefruitsArr
in viewDidLoad (and remove it frominit
).因为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.