我想根据它们的长度过滤出一个字符串数组。一般来说,我完全不熟悉Objective C和OOP。NSMutableArray removeObjectAtIndex用法
wordList=[[stringFile componentsSeparatedByCharactersInSet:[NSCharacterSetnewlineCharacterSet]] mutableCopy];
for (int x=0; x<[wordList count]; x++) {
if ([[wordList objectAtIndex:x] length] != 6) {
[wordList removeObjectAtIndex:x];
}else {
NSLog([wordList objectAtIndex:x]);
}
}
for (int x=0; x<[wordList count]; x++) {
NSLog([wordList objectAtIndex:x]);
}
NSLog在else语句中只输出6个字母的单词,但第二个NSLog输出整个数组。我在这里错过了什么?还有任何通用的指针来清理/改进代码,我们感激不尽。
好吧,这是有道理的。我尝试在if长度!= 6语句中添加一个x--,这也适用。既然我在学习,我会用你的方法,但出于好奇,x有什么问题吗? – user1418214 2012-07-14 19:35:33
@ user1418214添加'x - '没有什么问题,但更典型的解决方案是使用'while'代替for',并将'x ++'增量放入'else'分支。 – dasblinkenlight 2012-07-14 19:40:29
使用谓词方法,'NSDictionary * binding'部分如何影响程序? – user1418214 2012-07-14 19:46:05