n.e*_*ind 2 iphone cocoa-touch objective-c nsmutablearray
对不起,如果这是基本的,但我无法理解这一点.我有一个数组,想要遍历它中的每个对象,看看它是否是等于:@"某事".如果试过这个,但它会崩溃:
for (int i = 0; i < ([myNSMutableArray count]); i++) {
NSLog(@"i = %i", i);
if ([[myNSMutableArray objectAtIndex:i] isEqualToString:@"something"]) {
...
} else {
...
}
}
Run Code Online (Sandbox Code Playgroud)
我去拿:
2011-07-14 13:38:40.983 MNs[21416:207] i = 0
2011-07-14 13:38:40.985 MNs[21416:207] -[__NSArrayM isEqualToString:]: unrecognized selector sent to instance 0x4c976f0
2011-07-14 13:38:40.987 MNs[21416:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayM isEqualToString:]: unrecognized selector sent to instance 0x4c976f0'
Run Code Online (Sandbox Code Playgroud)
任何帮助将非常感谢.提前致谢!
编辑:
对不起我忘记了.这就是我创建数组的方式:
NSMutableArray *myNSMutableArray = [[NSMutableArray alloc] init];
for (int x = 0; x < 30; x++) {
[myNSMutableArray addObject:@""];
[myNSMutableArray addObject:@"something"];
[myNSMutableArray addObject:@""];
}
Run Code Online (Sandbox Code Playgroud)
EDIT2:
很抱歉,问题是我试图复制一个可变阵列...所以你的所有答案都是正确的,我现在只需要选一个,我想.
小智 6
试试这个,
for (int i = 0; i < ([myNSMutableArray count]); i++) {
NSLog(@"i = %i", i);
NSString *stringToCheck = (NSString *)[myNSMutableArray objectAtIndex:i];
if ([stringToCheck isEqualToString:@"something"]) {
...
} else {
...
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3934 次 |
| 最近记录: |