开发者

How to select one object from a NSArray?

开发者 https://www.devze.com 2023-02-01 17:31 出处:网络
First of all Merry Christmas to everyone!!! Currently I have a NSArray that has parsed content in it. When I do a NSLog of the array it prints out 20 objects with the parsed content that I needed. Lik

First of all Merry Christmas to everyone!!! Currently I have a NSArray that has parsed content in it. When I do a NSLog of the array it prints out 20 objects with the parsed content that I needed. Like so:

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

However, I don't need every obje开发者_运维百科ct at once. I need to be able to pick out one object at a time so I can put each object into a string of its own. If anyone knows of an easier way to do this then what I'm trying to please let me know. Anyways, I need to be able to choose just one object depending on what object I need. For example, let's say I only need object #5 and not all the array, how can this be done so I can put it into a string? I'm thinking I might have to use the index feature but I'm not sure how to set it up properly. Here is my NSArray that I'm working with:

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);

I've already spent several hours trying to achieve this but my knowledge with array's is limited even with me reading the documentation. :-( Any help is much appreciated. Thanks


To retrieve the first object in an array:

id obj = [array objectAtIndex:0];

To retrieve a random object in an array:

id obj = [array objectAtIndex:arc4random_uniform(array.count)];

See NSArray and the arc4random_uniform man page.


Check the documentation for -objectAtIndex:


Element *snippet = [results objectAtIndex:5];

See NSArray programming guide for details on how to use NSArrays.

0

精彩评论

暂无评论...
验证码 换一张
取 消