通过比较字符串从Xcode中的NSArray中选择indexOfObject [英] select indexOfObject from NSArray in Xcode by comparing string

查看:974
本文介绍了通过比较字符串从Xcode中的NSArray中选择indexOfObject的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Xcode中有 NSarray 值。我需要通过比较字符串值来获取数组indexOfObject。
我的数组值是

Hi I have NSarray values in Xcode. I need to get array indexOfObject by comparing string values. my array values are

 (
    {
    firstName = lord;
    lastname = krishna;
    },
    {
    firstName = priya;
    lastname = amirtha;
    }
 )

如果我在文本字段中键入名字,单击按钮意味着姓名要显示在另一个文本字段中。

谢谢。

If I type first name in textfield and click button means last name want to display in another textfield.
thank you.

推荐答案

回答问题的标题:

NSString *compareString = @"something";
NSMutableArray *indexesOfMatches = [[NSMutableArray alloc]init];

for (NSString *string in theArray) {

   if ([string isEqualToString:compareString]) {
       NSNumber *index = [[NSNumber numberWithInterger:[theArray indexOfObject:string]];
       [indexOfMatches addObject:index];
    }
}
    //indexOfMatches will now contain NSNumber objects that represent the indexes of each of the matching string objects in the array

我认为使用NSDictionary会更好。然后你可以简单地将名字和姓氏保持为键值对。

I think that using an NSDictionary would be better for you though. Then you can simply keep the first and last names as Key Value pairs.

NSDictionary *names = @{@"lord" : @"krishna", @"priya" : @"amirtha" };

然后,当你得到第一个名字时,你可以为密钥做值:

Then you can just do value for key when you get the first name:

NSString *firstName = @"lord";
NSString *lastName = [names valueForKey:firstName];

这篇关于通过比较字符串从Xcode中的NSArray中选择indexOfObject的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆