繁体   English   中英

将字符串对象数组化为字符串Objective-C

[英]Array string objects to strings objective-c

我有这个数组Array *array = [@"string1", @"string2", @"string3", @"string4"];

我想获取每个字符串对象并分配给动态创建的NSString对象。

可能吗?

我不太确定您要什么。

你可以有这样的事情:

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

您可以这样使用:

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

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

destroy(a);

编辑:看来我读错了你的问题。

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

暂无
暂无

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

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