当 for 循环从 NSArray 到达 NSNotFound 时如何中断

发布于 2024-12-12 00:04:40 字数 2326 浏览 0 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

天生の放荡 2024-12-19 00:04:40

这不是 NSArrayNSNotFound 的工作原理。尝试访问超过 NSArray 末尾的对象将引发异常。您想这样做有什么具体原因吗?使用快速枚举要容易得多:

for (id object in array) {
    [self.mutablearray addObject:object];
}

编辑: 我从您的其他评论中看到,您希望将其限制为添加到可变数组的最多 100 个对象。这很容易:

NSUInteger count = 0;
for (id object in array) {
    [self.mutablearray addObject:object];
    count++;
    if (count >= 100) break;
}

That's not how NSArray or NSNotFound work. Attempting to access an object past the end of an NSArray 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:

for (id object in array) {
    [self.mutablearray addObject:object];
}

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:

NSUInteger count = 0;
for (id object in array) {
    [self.mutablearray addObject:object];
    count++;
    if (count >= 100) break;
}
冬天旳寂寞 2024-12-19 00:04:40

看起来你正在尝试做一个累积子集。试试这个:

for(NSInteger index = [array indexOfObject: string]; ((index <  [array count]) &&
                                                      (index <= 100)           &&
                                                      (index != NSNotFound))   ;  index++)
{
    [self.mutablearray addObject: [array objectAtIndex: index]];
}

或者,如果你只是做一个严格的子集,试试这个:

NSInteger startIndex = [array indexOfObject: string];
NSInteger endIndex = MIN([array count], 100);
if(startIndex != NSNotFound)
{
    self.mutablearray = [array subarrayWithRange: NSMakeRange(startIndex, endIndex)] mutableCopy];
}

Looks like you're trying to do a cumulative subset. Try this:

for(NSInteger index = [array indexOfObject: string]; ((index <  [array count]) &&
                                                      (index <= 100)           &&
                                                      (index != NSNotFound))   ;  index++)
{
    [self.mutablearray addObject: [array objectAtIndex: index]];
}

Or, if you're just doing a strict subset, try this:

NSInteger startIndex = [array indexOfObject: string];
NSInteger endIndex = MIN([array count], 100);
if(startIndex != NSNotFound)
{
    self.mutablearray = [array subarrayWithRange: NSMakeRange(startIndex, endIndex)] mutableCopy];
}
说不完的你爱 2024-12-19 00:04:40

NSNotFound 是这样定义的

enum {NSNotFound = NSIntegerMax};

,因此您将访问数组中的无效索引。

NSNotFound is defined like this

enum {NSNotFound = NSIntegerMax};

so you're going to be accessing invalid indeces in your array.

for (id obj in array){
 [self.mutableArray addObject:obj];
}

这将运行直到数组有对象。

for (id obj in array){
 [self.mutableArray addObject:obj];
}

This will run until array has objects.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文