-3

我想在 NSArray 或 NSMutableArray 中存储浮点值(成员变量:更改继续),所以,想查看 NSLog 形式。我一次又一次地尝试,但失败了。

你能帮帮我吗?

4

3 回答 3

1

这就是为什么NSNumber类在那里:

NSNumber *num = [NSNumber numberWithFloat:3.14f];
// or:
NSNumber *num = @(3.14f);
[mutableArray addObject:num];

NSLog(@"%f", [(NSNumber *)[mutableArray objectAtIndex:0] floatValue]);
于 2012-12-17T00:06:22.200 回答
1

您不能将原始类型存储在 中NSArray,您必须首先将它们装箱(包装)在 NSObject 实例中,例如NSNumber.

float myVal = 5.5f;
NSArray *arr = [NSArray arrayWithObjects:[NSNumber numberWithFloat:myVal], nil];
NSLog("some array: %@", arr);

当然,您可以直接使用NSLog(@"my value: %f", myVal).

于 2012-12-17T00:09:03.627 回答
0

Forgive me for another answer but I couldn't see that old and too long syntax:

NSArray* arr= @[ @1.0 ];
NSLog(@"%@",arr[0] );

Notice that you haven't to call [arr[0] floatValue], the description method will do it's job.

于 2012-12-17T00:27:59.690 回答