Kap*_*isa 3 objective-c nsmutablearray
这里我试图在数组中添加对象并检查对象是否存在于数组中.因为我正在使用以下代码..
NSInteger ind = [arrActionList indexOfObject:indexPath];
if (ind >= 0 ) {
[arrActionList removeObjectAtIndex:ind];
}
else {
[arrActionList addObject:indexPath];
}
Run Code Online (Sandbox Code Playgroud)
在这里,我想我做得对..首先我要检查索引.如果> = 0我将删除对象,否则添加一个新对象.
我的问题是,如果找不到对象的索引,它会为我的整数变量分配一个垃圾值.我想它应该是-1,但它不是我的下一行,我正在删除对象抛出错误.
ind = 2147483647
任何帮助......
在官方文档可能会有所帮助.
简而言之,如果指定的对象不在数组中,则indexOfObject:返回常量NSNotFound.该NSNotFound常数具有0x7FFFFFFF的,这是在十进制等于2147483647的值.
如果你这样做,你的代码应该正常运行:
NSInteger ind = [arrActionList indexOfObject:indexPath];
if (ind != NSNotFound) {
[arrActionList removeObjectAtIndex:ind];
}
else {
[arrActionList addObject:indexPath];
}
Run Code Online (Sandbox Code Playgroud)
如果你以后不需要ind的值,你可以写;
if ( [arrActionList containsObject:indexPath] ) {
[arrActionList removeObject:indexPath;
}
else {
[arrActionList addObject:indexPath];
}
Run Code Online (Sandbox Code Playgroud)
或者,而不是测试ind> = 0,使用
if (ind != NSNotFound) { ...
Run Code Online (Sandbox Code Playgroud)
因为这就是2147483647实际上的值 - 它根本不是一个"垃圾"值,它告诉你一些有用的东西.
| 归档时间: |
|
| 查看次数: |
8731 次 |
| 最近记录: |