xcode 如何在Objective-C中创建一个字符串或浮点数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7042431/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 21:43:37  来源:igfitidea点击:

how to create an array of string or float in Objective-C

iphoneobjective-ciosxcode

提问by Desmond

i need some help here, i need to know how to create an array of string retrieved from an array. i'm using powerplot for graph and it only accept float or string array.

我需要一些帮助,我需要知道如何创建从数组中检索的字符串数组。我正在使用 powerplot 作为图形,它只接受浮点数或字符串数​​组。

i need to create something something like this dynamically.

我需要动态地创建这样的东西。

NSString * sourceData[7] = {@"2", @"1", @"4", @"8", @"14", @"15", @"10"};

NSString * sourceData[7] = {@"2", @"1", @"4", @"8", @"14", @"15", @"10"};

Below are my code to find out the numbers in strings.

下面是我找出字符串中数字的代码。

NSInteger drunked = [appDelegate.drinksOnDayArray count];
NSMutableArray * dayArray = [[NSMutableArray alloc] init];
NSMutableArray * sdArray = [[NSMutableArray alloc] init];
//float *sdArray[7];


for (int i=0; i<drunked; i++) {
    DayOfDrinks *drinksOnDay = [appDelegate.drinksOnDayArray objectAtIndex:i];
    NSString * dayString= [NSDate stringForDisplayFromDateForChart:drinksOnDay.dateConsumed];
    [dayArray addObject:dayString];
    NSLog(@"%@",[dayArray objectAtIndex:i]);

    drinksOnDay.isDetailViewHydrated = NO;
    [drinksOnDay hydrateDetailViewData];

    NSString * sdString= [NSString stringWithFormat:@"%@", drinksOnDay.standardDrinks];
    [sdArray addObject:sdString];

    NSString *tempstring;
    NSLog(@"%@",[sdArray objectAtIndex:i]);

}

thanks for the help :)

谢谢您的帮助 :)

回答by Faizan S.

Array's in Objectice-C aren't that hard to work with:

Objectice-C 中的数组并不难处理:

NSMutableArray *myArray = [NSMutableArray array];
[myArray addObject:@"first string"]; // same with float values
[myArray addObject:@"second string"];
[myArray addObject:@"third string"];
int i;
int count;
for (i = 0, count = [myArray count]; i < count; i = i + 1)
{
   NSString *element = [myArray objectAtIndex:i];
   NSLog(@"The element at index %d in the array is: %@", i, element); // just replace the %@ by %d
}

You can either use NSArrayor NSMutableArray- depending on your needs, they offer different functionality.

您可以使用NSArrayNSMutableArray- 根据您的需要,它们提供不同的功能。

Following tutorial covers exactly what you are looking after:

以下教程涵盖了您正在关注的内容:

http://www.cocoalab.com/?q=node/19

http://www.cocoalab.com/?q=node/19

回答by larsacus

You can also add the elements to the array when you init (and optionally add them later only if you are using the Mutableversion of a collection class:

您还可以在初始化时将元素添加到数组中(并且仅当您使用Mutable集合类的版本时才可以选择稍后添加它们:

NSMutableArray *myArray = [[NSMutableArray alloc] initWithObjects:@"2", @"1", @"4", @"8", @"14", @"15", @"10", nil];
[myArray addObject:@"22"];
[myArray addObject:@"50"];

//do something

[myArray release];

回答by Luis

You can use malloc to create a C-style array. something like this should work:

您可以使用 malloc 创建一个 C 风格的数组。这样的事情应该工作:

NSString **array = malloc(numElements * sizeof(NSString *))
some code here
free(array)

Be aware that unlike NSMutable array, c arrays won't do a retain, so you have to manage it if needed. And don't forget the free

请注意,与 NSMutable 数组不同,c 数组不会保留,因此您必须在需要时对其进行管理。并且不要忘记免费