将 NSString 与数组的 NSString 字段进行匹配

发布于 2024-11-17 02:39:40 字数 805 浏览 1 评论 0原文

我有一个名为 myObjectArray 的 NSMutableArray,其中包含名为 myObject 的 NSObjects 数组。 myObject 有两个字段(元素?),它们是 NSString 的。像这样:

@interface myObject : NSObject {
   NSString * string1;
   NSString * string2;
}

我有一个 NSMutableArray ,其中包含大约 50 个这样的对象,所有对象都具有不同的 string1 和 string2 。然后我有一个独立的 NSString 变量,称为 otherString;

有没有一种快速方法可以从 myObjectArray 访问其 string1 与 otherString 匹配的 myObject?

我应该说,这就是我所拥有的,但我想知道是否有更快的方法:

-(void) matchString: {

    NSString * testString = otherString;
    for(int i=0; i<[myObjectArray count];i++){
    myObject * tempobject = [myObjectArray objectAtIndex:i];
        NSString * tempString = tempobject.string1;
        if ([testString isEqualToString:tempString]) {
            // do whatever
        }

    }

}

I have an NSMutableArray called myObjectArray which contains and array of NSObjects called myObject. myObject has two fields (elements?) which are NSString's. like this:

@interface myObject : NSObject {
   NSString * string1;
   NSString * string2;
}

I have an NSMutableArray which contains about 50 of these objects, all with different string1's and string2's. then I have and independent NSString variable, called otherString;

Is there a fast way to access the myObject from myObjectArray whose string1 matches otherString?

i should say, this is what i have, but i wonder if there is a faster way:

-(void) matchString: {

    NSString * testString = otherString;
    for(int i=0; i<[myObjectArray count];i++){
    myObject * tempobject = [myObjectArray objectAtIndex:i];
        NSString * tempString = tempobject.string1;
        if ([testString isEqualToString:tempString]) {
            // do whatever
        }

    }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

枕花眠 2024-11-24 02:39:40

有几种方法可以做到这一点,

使用谓词

NSPredicate * filterPredicate = [NSPredicate predicateWithFormat:@"string1 MATCHES[cd] %@", otherString];
NSArray * filteredArray = [myObjectArray filteredArrayUsingPredicate:filterPredicate];

现在filteredArray拥有所有具有string1myObject实例> 匹配 otherString

使用indexOfObjectPassingTest:

NSUInteger index = [myObjectArray indexOfObjectPassingTest:^(BOOL)(id obj, NSUInteger idx, BOOL *stop){
    myObject anObject = obj;
    return [anObject.string1 isEqualToString:otherString];
}

如果有满足条件的对象, index 会将您指向其索引。否则它将具有值NSNotFound

如果您希望所有对象都满足条件,您还可以查看indexesOfObjectsPassingTest:

There are a few ways you can do this,

Using Predicates

NSPredicate * filterPredicate = [NSPredicate predicateWithFormat:@"string1 MATCHES[cd] %@", otherString];
NSArray * filteredArray = [myObjectArray filteredArrayUsingPredicate:filterPredicate];

Now filteredArray has all the myObject instances that have their string1 matching otherString.

Using indexOfObjectPassingTest:

NSUInteger index = [myObjectArray indexOfObjectPassingTest:^(BOOL)(id obj, NSUInteger idx, BOOL *stop){
    myObject anObject = obj;
    return [anObject.string1 isEqualToString:otherString];
}

If there is an object that satisfies the condition, index will point you to its index. Otherwise it will have the value NSNotFound.

You can also look at indexesOfObjectsPassingTest: if you want all the objects satisfying the condition.

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