检查 NSMutableArray 中的值

发布于 2025-01-05 11:20:14 字数 224 浏览 4 评论 0原文

我有一个 NSMutableArray(contactsArray) ,其内容如下。

{({ContactID = ""; RecordID = 45; Number = "";},
{ContactID = 134;RecordID = 47;Number = PNAPYOEMZH;})}

我有一个自变量说x。我需要检查 x 是否等于数组中的 ContactID

I have an NSMutableArray(contactsArray) with contents as below.

{({ContactID = ""; RecordID = 45; Number = "";},
{ContactID = 134;RecordID = 47;Number = PNAPYOEMZH;})}

I have an independent variable say x. I need to check whether x is equal to the ContactID in the array

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

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

发布评论

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

评论(2

彼岸花似海 2025-01-12 11:20:14

您可以使用 isEqual 来检查数组数据之间的内容,尝试一下

if([[yourArray objectAtIndex:theIndex]valueForKey:@"ContactID"] isEqualToNumber [[yourArray objectAtIndex:theIndex]valueForKey:@"RecordID"])
{
NSLog(@"the contact ID matches the Record ID");
}
else
{
NSLog(@"it doesn't match")
}

这里发生的情况是 if 语句将数组内的对象与索引或行“theIndex”

希望 中的数据中的“ContactId”和“RecordID”的值进行比较此帮助

编辑:由于您将问题编辑为具有变量 X,因此您可以将第二个参数比较器更改为 [NSNumber numberWithInt:x]

如果您的 X 是 String 类型,则应该使用 .intValue

you can use isEqual to check things between array data, try this

if([[yourArray objectAtIndex:theIndex]valueForKey:@"ContactID"] isEqualToNumber [[yourArray objectAtIndex:theIndex]valueForKey:@"RecordID"])
{
NSLog(@"the contact ID matches the Record ID");
}
else
{
NSLog(@"it doesn't match")
}

what happened here is the if sentences is comparing the object inside your array with the value of "ContactId" with "RecordID" at the data in the index or row "theIndex"

hope this help

edit : since you edited the question into having a variable X, you can just change the second parameter comparer to [NSNumber numberWithInt:x]

if your X is a String type, you should use .intValue

递刀给你 2025-01-12 11:20:14

您可以执行以下操作:

NSMutableArray *contactsArray = <the_array>;
int x = <the_value_to_check_against>;

[contactsArray filterUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(id obj, NSDictionary *bindings){
    return ([[obj objectForKey:@"ContactID"] intValue] == x);
}]];

或将谓词更改为:

[contactsArray filterUsingPredicate:[NSPredicate predicateWithFormat:@"self.ContactID == %@", [NSNumber numberWithInt:x]]];

You could do something like this:

NSMutableArray *contactsArray = <the_array>;
int x = <the_value_to_check_against>;

[contactsArray filterUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(id obj, NSDictionary *bindings){
    return ([[obj objectForKey:@"ContactID"] intValue] == x);
}]];

or change the predicate to:

[contactsArray filterUsingPredicate:[NSPredicate predicateWithFormat:@"self.ContactID == %@", [NSNumber numberWithInt:x]]];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文