简体   繁体   中英

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

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 )

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 .

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM