简体   繁体   English

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

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

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:)谢谢您的帮助:)

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 NSArray or 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

You can also add the elements to the array when you init (and optionally add them later only if you are using the Mutable version of a collection class:您还可以在初始化时将元素添加到数组中(并且仅当您使用集合 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];

You can use malloc to create a C-style array.您可以使用 malloc 创建 C 样式数组。 something like this should work:像这样的东西应该工作:

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.请注意,与 NSMutable 数组不同,c arrays 不会保留,因此您必须在需要时对其进行管理。 And don't forget the free不要忘记免费

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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