0

I have this array Array *array = [@"string1", @"string2", @"string3", @"string4"];

I want to get each string object and assign to dynamically created NSString objects.

Is it possible?

Omer Waqas Khan
  • 2,423
  • 4
  • 33
  • 62
  • 5
    The above code isn't even syntactically valid. What is an `Array`? What do the square brackets mean? – JeremyP Mar 12 '12 at 16:26
  • Are you saying that for each NSString in that array, you want a new NSString that is a copy of the string in the array? – UIAdam Mar 12 '12 at 16:30
  • Yes, UIAdam thats what I want so that I would assign those strings to my labels to show on screen – Omer Waqas Khan Mar 12 '12 at 16:59

1 Answers1

0

I'm not quite sure what you are asking for.

You can have something like this:

typedef struct Array_s {
   __strong id *values;
   int count;
} Array;

#define array(values) ({ id _v[] = { values }; int _c = sizeof(_v) * sizeof(*_v); int *_cpy = malloc(sizeof(id) * _c); memcpy(_cpy, _v, sizeof(id) * _c); Array _a; _a.count = _c; _a.values = _cpy; _a; })

#define destroy(array) ({ free(array->values); })

And you can use this like this:

Array a = array(@"This", @"Is", @"A", @"Test");

for (int i = 0; i < a.count; i++)
{
    NSLog(@"%@", a.values[i]);
}

destroy(a);

EDIT: It appears I read your question wrong.

NSArray *deepCopyArray(NSArray *input)
{
    id copiedValues[input.count];

    for (int i = 0; i < input.count; i++)
    {
        copiedValues[i] = [[input objectAtIndex:i] copy];
    }

    return [NSArray arrayWithObjects:copiedValues count:input.count];
}
Richard J. Ross III
  • 55,009
  • 24
  • 135
  • 201
  • Hey Richard thank you, actually the scenario is that, I have saved names in core data, I am getting them in an array, then from that array I want to assign those names to each label on the screen – Omer Waqas Khan Mar 12 '12 at 16:57