如何在 NSMutableArray 中添加负浮点数?
How to add negative float in NSMutableArray?
好的。我看不出有什么方法可以做到。但也许有人知道?
我不需要使用 NSString 将其放入数组中,因为它会占用大量内存。
NSMutableArray *array = @[@(-16.f)].mutableCopy;
for (NSNumber *number in array) {
NSLog(@"%f", number.floatValue);
}
首先,您需要使用 @(floatValue) 将您的float
值转换为NSNumber
。
NSMutableArray *arr = [[NSMutableArray alloc]init]; // here you can add value using initWithObject method.
[arr addObject:@(-16)]; // here you can add your float value using @(float value).
NSLog(@" array = %@",arr);
当您从数组中检索值时。
float yourValue = [[arr objectAtIndex:(your index)] floatValue];
希望对您有所帮助。
好的。我看不出有什么方法可以做到。但也许有人知道? 我不需要使用 NSString 将其放入数组中,因为它会占用大量内存。
NSMutableArray *array = @[@(-16.f)].mutableCopy;
for (NSNumber *number in array) {
NSLog(@"%f", number.floatValue);
}
首先,您需要使用 @(floatValue) 将您的float
值转换为NSNumber
。
NSMutableArray *arr = [[NSMutableArray alloc]init]; // here you can add value using initWithObject method.
[arr addObject:@(-16)]; // here you can add your float value using @(float value).
NSLog(@" array = %@",arr);
当您从数组中检索值时。
float yourValue = [[arr objectAtIndex:(your index)] floatValue];
希望对您有所帮助。