gpt4 book ai didi

objective-c - NSMutableArray : addObject "nil"?

转载 作者:太空狗 更新时间:2023-10-30 03:30:30 25 4
gpt4 key购买 nike

我在数组末尾添加“nil”时遇到问题,我得到“NSInvalidArgumentException”?

NSMutableArray *k = [NSMutableArray arrayWithCapacity:10];

for (int i=0; i<9; i++){
[k addObject: @"blank"];
}
[k addObject: nil]; //<-- NSInvalidArgumentException

我需要在循环中逐项执行所有这些操作,然后添加“nil”。

谢谢

(然后我使用这个数组并初始化一个二维数组。问题是我无法用没有“nil”的数组成功“replaceObjectAtIndex”。如果我用“initWithObjects: @"blank", @"blank", @"blank", ... nil"构建“k”,这将起作用。然而,写 1000 个空白有点多。这就是循环的目的。)

//////这里是用于初始化和构建二维矩阵的试验和错误代码,用于从“CSV 文件”中读取和存储矩阵//////

///*
NSMutableArray *ppp = [NSMutableArray arrayWithCapacity:2];
NSMutableArray *kkk = [NSMutableArray arrayWithCapacity:20];

NSNull *myNull = [NSNull null];
for (int i=0; i<9; i++) {
//[kkk addObject: [NSMutableString stringWithFormat: @"%d",i]];
[kkk addObject: myNull];
}
//[kkk addObject: nil];

[ppp addObject:kkk];
[ppp addObject:kkk];
//*/


/*
// this is successful --> just uncomment this block and comment out the block above
[ppp addObject:[[NSMutableArray alloc] initWithObjects:
@"z1",
@"z2",
@"z3",
@"z4",
@"z5",
@"z6",
@"z0gg",
@"z0hh",
@"z0ii",
@"z0jj",
nil
]];

[ppp addObject:[[NSMutableArray alloc] initWithObjects:
@"a1",
@"b2",
@"c3",
@"d4",
@"e5",
@"f6",
nil
]];

*/

[[ppp objectAtIndex:0] replaceObjectAtIndex:1 withObject:@"HOTDOG"];
[[ppp objectAtIndex:1] replaceObjectAtIndex:1 withObject:@"HOHO"];
// HOHO will replace HOTDOG as well for the code not using "nil"

最佳答案

您不能将nil 添加到NSMutableArray,并且you will raise an exception if you try to .

虽然有 NSNull:

NSNull *myNull = [NSNull null];
[myMutableArray addObject:myNull];

但是,您可能会问自己为什么要尝试这样做。

关于objective-c - NSMutableArray : addObject "nil"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6714439/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com