当 for 循环从 NSArray 到达 NSNotFound 时如何中断
我有一个 nsmutablearray,我在 nsarray 的 for 循环中向其中添加对象。我希望它添加对象,直到它到达 NSArray 中的 indexOfObject: 中的 NSNotFound
。 该数组位于 for 循环内,用于从每个索引中提取字符串。
我的代码:
NSInteger untilNotFound = NSNotFound;
NSUInteger index = [array indexOfObject:string];
for (index; index < untilNotFound; index ++)
{
[self.mutablearray addObject:[array objectAtIndex: index]];
}
编辑:
for (string in array)
{
NSComparisonResult *result = [string compare:searchText options:(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch)
range:NSMakeRange(0, [searchText length])];
if (result == NSOrderedSame)
{
NSInteger index;
for (index =[array indexOfObject:string]; (index < [array count] && index < 100 && index!= NSNotFound); index ++)
{
[self.mutablearray addObject:[array objectAtIndex: index]];
}
break;
}
}
编辑。
我以前的代码可以工作,它在 if (result == NSOrderedSame) 内部,我只是想知道是否有更好、更简单、性能友好的解决方案来在到达 NSNotFound 之前提取索引:
NSUInteger new = [array indexOfObject:string];
NSString *last = [array lastObject];
NSUInteger total = [array indexOfObject:last];
NSUInteger newmax = new + 10;
NSUInteger totalminus = total - 11;
if (newmax <= totalminus )
{
[self.mutablearray addObject:string];
[self.mutablearray addObject:[array objectAtIndex:new + 1]];
[self.mutablearray addObject:[array objectAtIndex:new + 2]];
[self.mutablearray addObject:[array objectAtIndex:new + 3]];
[self.mutablearray addObject:[array objectAtIndex:new + 4]];
[self.mutablearray addObject:[array objectAtIndex:new + 5]];
[self.mutablearray addObject:[array objectAtIndex:new + 6]];
[self.mutablearray addObject:[array objectAtIndex:new + 7]];
[self.mutablearray addObject:[array objectAtIndex:new + 8]];
[self.mutablearray addObject:[array objectAtIndex:new + 9]];
[self.mutablearray addObject:[array objectAtIndex:new + 10]];
}
else
{
[self.mutablearray addObject:string];
}
I have a nsmutablearray which I add object to it in a for loop from a nsarray. I want it to add object until it reaches NSNotFound
in indexOfObject: from NSArray.
The array is inside a for loop to extract string from every index.
My code:
NSInteger untilNotFound = NSNotFound;
NSUInteger index = [array indexOfObject:string];
for (index; index < untilNotFound; index ++)
{
[self.mutablearray addObject:[array objectAtIndex: index]];
}
Edit:
for (string in array)
{
NSComparisonResult *result = [string compare:searchText options:(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch)
range:NSMakeRange(0, [searchText length])];
if (result == NSOrderedSame)
{
NSInteger index;
for (index =[array indexOfObject:string]; (index < [array count] && index < 100 && index!= NSNotFound); index ++)
{
[self.mutablearray addObject:[array objectAtIndex: index]];
}
break;
}
}
EDIT.
My previous code that works, it is inside if (result == NSOrderedSame) I just wonder if there is any nicer and easier and performance-friendly solution to extract indexes just before it reaches NSNotFound:
NSUInteger new = [array indexOfObject:string];
NSString *last = [array lastObject];
NSUInteger total = [array indexOfObject:last];
NSUInteger newmax = new + 10;
NSUInteger totalminus = total - 11;
if (newmax <= totalminus )
{
[self.mutablearray addObject:string];
[self.mutablearray addObject:[array objectAtIndex:new + 1]];
[self.mutablearray addObject:[array objectAtIndex:new + 2]];
[self.mutablearray addObject:[array objectAtIndex:new + 3]];
[self.mutablearray addObject:[array objectAtIndex:new + 4]];
[self.mutablearray addObject:[array objectAtIndex:new + 5]];
[self.mutablearray addObject:[array objectAtIndex:new + 6]];
[self.mutablearray addObject:[array objectAtIndex:new + 7]];
[self.mutablearray addObject:[array objectAtIndex:new + 8]];
[self.mutablearray addObject:[array objectAtIndex:new + 9]];
[self.mutablearray addObject:[array objectAtIndex:new + 10]];
}
else
{
[self.mutablearray addObject:string];
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这不是
NSArray
或NSNotFound
的工作原理。尝试访问超过 NSArray 末尾的对象将引发异常。您想这样做有什么具体原因吗?使用快速枚举要容易得多:编辑: 我从您的其他评论中看到,您希望将其限制为添加到可变数组的最多 100 个对象。这很容易:
That's not how
NSArray
orNSNotFound
work. Attempting to access an object past the end of anNSArray
will raise an exception. Is there a specific reason why you want to do it this way? It's much easier to use fast enumeration:Edit: I see from your other comments that you want to limit this to a maximum of 100 objects added to the mutable array. That's easy enough:
看起来你正在尝试做一个累积子集。试试这个:
或者,如果你只是做一个严格的子集,试试这个:
Looks like you're trying to do a cumulative subset. Try this:
Or, if you're just doing a strict subset, try this:
NSNotFound 是这样定义的
,因此您将访问数组中的无效索引。
NSNotFound is defined like this
so you're going to be accessing invalid indeces in your array.
这将运行直到数组有对象。
This will run until array has objects.