5

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.

i need to create something something like this dynamically.

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 :)

2
  • what is exactly the problem your have with your code? Commented Aug 12, 2011 at 15:38
  • i need to dynamically create a list of String or Float like this NSString * sourceData[7] = {@"2", @"1", @"4", @"8", @"14", @"15", @"10"}; what i have now is an array with objects inside, i dun know how to add string in a string. Commented Aug 14, 2011 at 6:42

3 Answers 3

7

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

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.

Following tutorial covers exactly what you are looking after:

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

Sign up to request clarification or add additional context in comments.

3 Comments

hi shaharyar, thanks for the reply, however this is not what i want. i know how an array works, but i need to know how to dynamically create a list of string like this NSString * sourceData[7] = {@"2", @"1", @"4", @"8", @"14", @"15", @"10"}; what i have now is an array with objects inside, i dun know how to add string in a string.
how to add string in a string? your question isn't very clear there. please clarify. your question says : how to create an array of string or float in Objective-C | that's what I answered.
@Shaharyar The float array isn't working for me! when I addObject it says sending 'float' to parameter of incompatible type 'id'
1

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:

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

//do something

[myArray release];

Comments

0

You can use malloc to create a C-style array. 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. And don't forget the free

2 Comments

Hi Luis, Could you give me more info on how to run the loop ?
You just have a C array, a standar for loop should work. For instance, for (int i =0; i<numElements; i++) { NSString *elem = array[i]; }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.