在数组@iphone Sdk中查找选定的对象索引
我将实现一个包含排序功能的应用程序。
这里我有两个数组,一个数组名称是“listOfItems”,另一个数组名称是“copyListOfItems”。
在成功对数组进行排序后,将其添加到“copyListOfItems”,从该数组中我获取选定的字符串值,并在名为“listOfItems”的主数组中找到相同的“STRING”值。
在这里我使用了以下逻辑:-
NSLog(@"My Index %d",[listOfItems indexOfObject:selectedCountry]);
但我无法获得该索引,任何人都可以帮忙。
Im going to implement one application that includes sorting functionality.
Here i have two arrays, one array name is "listOfItems" and another one is that "copyListOfItems".
Here after successfully sorting the array its adding to "copyListOfItems", from this array i'm taking the selected string value and finding that same "STRING" value in the main array called "listOfItems".
Here i have used below logic:-
NSLog(@"My Index %d",[listOfItems indexOfObject:selectedCountry]);
but m not able to get that index, can anyone please help in this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
参考文档中indexOfObject: 的正下方是indexOfObjectIdenticalTo:实际上,我错了。 indexOfObject 应该这样做。显然你还有另一个问题。你确定你的字符串是相同的吗?您确定您确实将正确的字符串传递给indexOfObject吗?你确定你的数组存在吗?
Right below indexOfObject: in the reference doc was indexOfObjectIdenticalTo:Actually, I was wrong. indexOfObject should do it. You've obviously got another problem. Are you sure your strings are identical? Are you sure you're actually passing the right string to indexOfObject? Are you sure your array even exists?
IndexOfObject 对我来说总是工作得很好,尤其是 NSStrings。否则,您可以循环整个数组并使用 [NSString isEqualToString:NSString]。
在比较之前尝试显示循环内的对象,并查看该对象(在本例中为 NSString)是否确实存在。
另外,尝试修剪选定的国家/地区。那里可能有空格或不需要的字符。
IndexOfObject always works fine with me especially with NSStrings. Otherwise, you can loop your entire array and use [NSString isEqualToString:NSString].
Try to display the objects inside the loop before comparing and see if that object (in this case an NSString) is really there.
Also, try trimming the selectedCountry. There might be a space or unwanted character there.