0SX
0SX

Reputation: 1292

How to select one object from a NSArray?

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.190 TestProject[48914:298] SomeContent19

I need to be able to pick out one object so I can put that object into a string of its own. How can this be done?

My existing code:

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

Upvotes: 4

Views: 12532

Answers (3)

Jacob Relkin
Jacob Relkin

Reputation: 163268

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.

Upvotes: 15

mralex
mralex

Reputation: 106

Element *snippet = [results objectAtIndex:5];

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

Upvotes: 1

NSResponder
NSResponder

Reputation: 16861

Check the documentation for -objectAtIndex:

Upvotes: 3

Related Questions