![](/img/trans.png)
[英]Objective-C / iOS: Converting an array of objects to JSON string
[英]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.