简体   繁体   中英

Array string objects to strings objective-c

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?

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];
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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