gpt4 book ai didi

objective-c - Objective-C 中的浮点值数组

转载 作者:太空狗 更新时间:2023-10-30 03:46:18 27 4
gpt4 key购买 nike

如何在 Objective-C 中创建 float 数组?可能吗?

最佳答案

您可以使用不同的方式创建动态数组(大小在运行时决定,而不是编译时决定),具体取决于您希望使用的语言:

Objective-C

NSArray *array = [[NSArray alloc] initWithObjects:
[NSNumber numberWithFloat:1.0f],
[NSNumber numberWithFloat:2.0f],
[NSNumber numberWithFloat:3.0f],
nil];
...
[array release]; // If you aren't using ARC

或者,如果您想在创建后更改它,请使用 NSMutableArray:

NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity:0];
[array addObject:[NSNumber numberWithFloat:1.0f]];
[array addObject:[NSNumber numberWithFloat:2.0f]];
[array addObject:[NSNumber numberWithFloat:3.0f]];
...
[array replaceObjectAtIndex:1 withObject:[NSNumber numberWithFloat:99.9f]];
...
[array release]; // If you aren't using ARC

或者使用新的Objective-C literals syntax :

NSArray *array = @[ @1.0f, @2.0f, @3.0f ];
...
[array release]; // If you aren't using ARC

C

float *array = (float *)malloc(sizeof(float) * 3);
array[0] = 1.0f;
array[1] = 2.0f;
array[2] = 3.0f;
...
free(array);

C++/Objective-C++

std::vector<float> array;
array[0] = 1.0f;
array[1] = 2.0f;
array[2] = 3.0f;

关于objective-c - Objective-C 中的浮点值数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12931221/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com