Shishir.bobby
Shishir.bobby

Reputation: 10984

assign value from array to string

i have an array of 5 objects.

i want to assign object which is at index 1, to an NSSTRING.

nsstring *abc = [array objectAtindex:1];

i know this is wrong syntax, this is returning object , something like this.

how can i get value which is at index 1 and assign it to an string?

regards

Upvotes: 0

Views: 1542

Answers (2)

Jordan
Jordan

Reputation: 21760

Arrays are zero based in Objective-C land. So...

NSArray *array = [NSArray arrayWithObjects:@"one", @"two", nil];
NSString *abc = [array objectAtIndex:1];

Would return the second object in the array. Zero would return the first.

Upvotes: 2

Jack
Jack

Reputation: 133567

Erm.. this is the correct syntax :)

Apart the name of the string class:

NSString *abc = [array objectAtIndex:1];

mind that this won't create a copy of the string, if you need to copy it use

NSString *abc = [NSString stringWithString:[array objectAtIndex:1]];

As Eiko notes you can directly copy the string object if you need to:

NSString  *abc = [[array objectAtIndex:1] copy];

Upvotes: 7

Related Questions