Viral Narshana
Viral Narshana

Reputation: 1877

Problem retrieving data from array iphone?

I declared a nsmutablearray and assign some object but when retrieving object it gives memory location.Here is the code.

NSMutableArray *arrCondiNum = [[NSMutableArray alloc] init];
[arrCondiNum addObject:@"2"];
[arrCondiNum addObject:@"4"];
[arrCondiNum addObject:@"6"];
[arrCondiNum addObject:@"8"];
for(int i = 0;i<[arrCondiNum count];i++){
    NSLog(@"Array number %d",[arrCondiNum objectAtIndex:i]);
}

Output it gives
Array number 20820
Array number 20836
Array number 20852
Array number 20868

Upvotes: 1

Views: 650

Answers (4)

success
success

Reputation: 67

just write like this.. NSLog(@"Array number %@",[arrCondiNum objectAtIndex:i]); u have to print some text..so

Upvotes: 0

Georg Fritzsche
Georg Fritzsche

Reputation: 99092

Note that in case you want an array of numbers, use NSNumber instead:

[array addObject:[NSNumber numberWithInt:1]];

You still have to use the %@ format specifier though (as NSNumber is a class-type) or retrieve the integer value using -intValue.

Upvotes: 2

Beno&#238;t
Beno&#238;t

Reputation: 7427

You add string in your array, then to display it, you have to use %@:

NSLog(@"Array number %@",[arrCondiNum objectAtIndex:i]);

In your code (%d), you display the address of the object.

%@ will display the description of the ObjC object (return of -descriptionWithLocale: or -description)

Upvotes: 3

Toastor
Toastor

Reputation: 8990

Try [[arrCondiNum objectAtIndex:i] floatValue] instead. NSArrays are keeping objects (pointers to objects to be more precise), not values. So the array will return the string object you created earlier. By sending this object the "floatValue" message, it will return its content represented as float value. You could use intValue as well to receive an integer.

Upvotes: 0

Related Questions