如何从NSArray中选择一个对象?

0SX*_*0SX 3 c arrays iphone objective-c

首先祝大家圣诞快乐!!! 目前我有一个NSArray已经解析了其中的内容.当我对数组执行NSLog时,它会打印出20个对象,其中包含我需要的已解析内容.像这样:

2010-12-24 20:27:32.170 TestProject[48914:298] SomeContent
2010-12-24 20:27:32.172 TestProject[48914:298] SomeContent1
2010-12-24 20:27:32.172 TestProject[48914:298] SomeContent2
2010-12-24 20:27:32.173 TestProject[48914:298] SomeContent3
2010-12-24 20:27:32.173 TestProject[48914:298] SomeContent4
2010-12-24 20:27:32.173 TestProject[48914:298] SomeContent5
2010-12-24 20:27:32.174 TestProject[48914:298] SomeContent6
2010-12-24 20:27:32.175 TestProject[48914:298] SomeContent7
2010-12-24 20:27:32.176 TestProject[48914:298] SomeContent8
2010-12-24 20:27:32.176 TestProject[48914:298] SomeContent9
2010-12-24 20:27:32.177 TestProject[48914:298] SomeContent10
2010-12-24 20:27:32.177 TestProject[48914:298] SomeContent11
2010-12-24 20:27:32.177 TestProject[48914:298] SomeContent12
2010-12-24 20:27:32.179 TestProject[48914:298] SomeContent13
2010-12-24 20:27:32.179 TestProject[48914:298] SomeContent14
2010-12-24 20:27:32.180 TestProject[48914:298] SomeContent15
2010-12-24 20:27:32.180 TestProject[48914:298] SomeContent16
2010-12-24 20:27:32.181 TestProject[48914:298] SomeContent17
2010-12-24 20:27:32.181 TestProject[48914:298] SomeContent18
2010-12-24 20:27:32.190 TestProject[48914:298] SomeContent19
Run Code Online (Sandbox Code Playgroud)

但是,我不需要同时使用每个对象.我需要能够一次挑出一个对象,这样我就可以将每个对象放入一个自己的字符串中.如果有人知道更简单的方法,那么我想告诉我的是什么.无论如何,我需要能够根据我需要的对象选择一个对象.例如,假设我只需要对象#5而不是所有数组,如何才能将其放入字符串?我想我可能不得不使用索引功能,但我不确定如何正确设置它.这是我正在使用的NSArray:

NSArray* myArray = [document selectElements: @"div.someContent"];
NSMutableArray* results = [NSMutableArray array];
for (Element* element in myArray){
    NSString* snipet = [element contentsSource];
    [results addObject: snipet];
    NSLog(@"%@", snipet);
}
NSLog(@"%i",myArray.count);
Run Code Online (Sandbox Code Playgroud)

我已经花了几个小时试图实现这一点,但即使我阅读文档,我对数组的知识也是有限的.:-(非常感谢任何帮助.谢谢

Jac*_*kin 14

要检索数组中的第一个对象:

id obj = [array objectAtIndex:0];
Run Code Online (Sandbox Code Playgroud)

要检索数组中的随机对象:

id obj = [array objectAtIndex:arc4random_uniform(array.count)];
Run Code Online (Sandbox Code Playgroud)

NSArrayarc4random_uniform手册页.

  • 雅各布再次拯救了这一天!+1 (2认同)