Ios Objective-C NSMutableArray仅返回一项

Ios Objective-C NSMutableArray仅返回一项,ios,objective-c,iphone,uitableview,Ios,Objective C,Iphone,Uitableview,我正试图通过NSLog打印出我的NSMutableArray中的对象列表,但由于某种原因,它似乎为空。基本上,我有一个待办事项列表,当用户输入要添加到tableview的新字符串时,它也会将该项添加到NSArray中,以便我可以将其保存到设备中 AddToDoItemViewController.m #import "AddToDoItemViewController.h" #import "ToDoItem.h" @interface AddToDoItemViewController ()

我正试图通过
NSLog
打印出我的
NSMutableArray
中的对象列表,但由于某种原因,它似乎为空。基本上,我有一个待办事项列表,当用户输入要添加到tableview的新字符串时,它也会将该项添加到NSArray中,以便我可以将其保存到设备中

AddToDoItemViewController.m

#import "AddToDoItemViewController.h"
#import "ToDoItem.h"

@interface AddToDoItemViewController ()
@property (weak, nonatomic) IBOutlet UITextField *textField;
@property (weak, nonatomic) IBOutlet UIBarButtonItem *saveButton;
@end

@implementation AddToDoItemViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.toDoItem.itemList = [[NSMutableArray alloc] init];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a     little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender     {
    // Get the new view controller using [segue     destinationViewController].
    // Pass the selected object to the new view controller.
    if (sender != self.saveButton) return;

    if (self.textField.text.length > 0) {
        self.toDoItem = [[ToDoItem alloc] init];
        self.toDoItem.itemName = self.textField.text;
        self.toDoItem.completed = NO;

        NSLog(@"Trying to add to array: %@", self.toDoItem.itemName);
        [self.toDoItem.itemList addObject:self.toDoItem.itemName];
        NSLog(@"Array contents: %@", self.toDoItem.itemList);
    }
}

@end
AddToDoItemViewController.h

#import <UIKit/UIKit.h>
#import "ToDoItem.h"

@interface AddToDoItemViewController : UIViewController

@property ToDoItem *toDoItem;

@end
#import <Foundation/Foundation.h>

@interface ToDoItem : NSObject

@property NSString *itemName;
@property BOOL completed;
@property (readonly) NSDate *creationDate;
@property NSMutableArray *itemList;

@end

****“ok”是我输入的文本******

如果在添加数组之前没有初始化数组,请添加
self.toDoItem.itemList=[NSMutableArray new]

编辑:


哦,我看到您添加了
self.toDoItem.itemList=[[NSMutableArray alloc]init]在viewDidLoad中,但这不是放置它的正确位置,它应该位于
self.toDoItem=[[toDoItem alloc]init]之后
或在
ToDoItem

的init方法中,如果在添加数组之前未初始化数组,请添加
self.ToDoItem.itemList=[NSMutableArray new]

编辑:


哦,我看到您添加了
self.toDoItem.itemList=[[NSMutableArray alloc]init]在viewDidLoad中,但这不是放置它的正确位置,它应该位于
self.toDoItem=[[toDoItem alloc]init]之后
或在
ToDoItem

的init方法中,首先需要在ToDoItem.m创建一个init方法

- (id)init {
    self = [super init];
    if (self) {
        // Any custom setup work goes here
        self.itemList =  [[NSMutableArray alloc] init];
    }
    return self;
}

然后再次运行您的项目。

首先,您需要在ToDoItem.m创建一个init方法

- (id)init {
    self = [super init];
    if (self) {
        // Any custom setup work goes here
        self.itemList =  [[NSMutableArray alloc] init];
    }
    return self;
}

然后再次运行项目。

在初始化ToDoItem之前,您正在初始化数组(itemList),因此初始化的数组保持为零。因此,它不能存储任何对象

修改代码如下:

self.toDoItem = [[ToDoItem alloc] init];
self.toDoItem.itemList = [[NSMutableArray alloc] init];
您可以在viewDidLoad或Segue方法中添加以上代码行


希望对您有所帮助。

您在初始化ToDoItem之前正在初始化数组(itemList),因此初始化的数组仍然为零。因此,它不能存储任何对象

修改代码如下:

self.toDoItem = [[ToDoItem alloc] init];
self.toDoItem.itemList = [[NSMutableArray alloc] init];
您可以在viewDidLoad或Segue方法中添加以上代码行



希望它能对您有所帮助。

fonix我有一个疑问,如果他在下面初始化并准备segue,您将如何获得数组值,最后一个值仅获得na BourAnks,以便快速响应。我尝试在self.toDoItem=[[toDoItem alloc]init]之后添加它,但最终要做的是重新初始化数组,从而清除其中的所有项。我想,如果我在onDidLoad中初始化它,它将保持不变。那么,您将不得不对您的逻辑进行一些非常重要的更改,因为您正在重新分配
self.toDoItem
,因此在这一点上,您已经丢失了以前的任何一个。也许你应该把它拿出来?然后,您将从ViewDidLoad中获得先前初始化的数组。它是否应该将该项添加到直接位于类对象中的
NSMutableArray
?它应该,但事实上您拥有
self.toDoItem=[[toDoItem alloc]init]将使您的数组重置为空,因为您正在为
self.toDoItem
分配一个全新的对象。fonix我有一个疑问,如果他使用prepare for segue初始化,您将如何获得数组值,最后一个值仅获得na,以便快速响应。我尝试在self.toDoItem=[[toDoItem alloc]init]之后添加它,但最终要做的是重新初始化数组,从而清除其中的所有项。我想,如果我在onDidLoad中初始化它,它将保持不变。那么,您将不得不对您的逻辑进行一些非常重要的更改,因为您正在重新分配
self.toDoItem
,因此在这一点上,您已经丢失了以前的任何一个。也许你应该把它拿出来?然后,您将从ViewDidLoad中获得先前初始化的数组。它是否应该将该项添加到直接位于类对象中的
NSMutableArray
?它应该,但事实上您拥有
self.toDoItem=[[toDoItem alloc]init]在它将使数组重置为空之前,因为您正在将一个全新的对象分配给
self.toDoItem
。是否可以显示您的toDoItem.mca是否可以显示您的toDoItem.m?出于某种原因,执行此操作仍将返回空数组give me one case return null。出于某种原因,执行此操作仍将返回空数组give me one case return null。请取消对self.toDoItem.itemList的注释=[[NSMutableArray alloc]init];viewDidLoad中的这一行代码。请取消注释self.toDoItem.itemList=[[NSMutableArray alloc]init];viewDidLoad中的这一行代码。