addObject替换NSMutableArray中的上一个对象

Tom*_*Tom 2 objective-c nsmutablearray ios

我正在尝试通过for循环将对象添加到NSMutableArray.但似乎每当我添加一个对象时它会替换旧的对象,这样我当时在数组中只有一个对象......

你知道什么可能是错的吗?

- (void)viewDidLoad
{
[super viewDidLoad];
LoginInfo *info = [[LoginInfo alloc] init];
info.startPost = @"0";
info.numberOfPosts = @"10";
info.postType = @"1";
getResults = [backendService getAllPosts:info];

for (NSInteger i = 0; i < [getResults count]; i++) {

    Post *postInfo = [[Post alloc] init];
    postInfo = [getResults objectAtIndex:i];

    dataArray = [[NSMutableArray alloc] init];
    [dataArray addObject:postInfo.noteText];
    NSLog(@"RESULT TEST %@", dataArray);

}
}
Run Code Online (Sandbox Code Playgroud)

这是RESULT TEST日志,它始终只显示输出中最后添加的字符串.

wat*_*n12 10

你正在for循环中初始化dataArray,所以每次再创建它(这意味着没有对象)并添加一个新对象

移动

dataArray = [[NSMutableArray alloc] init];
Run Code Online (Sandbox Code Playgroud)

在for循环之前

postInfo当你使用getResults数组中的对象立即覆盖它时,也不需要分配/初始化对象