gpt4 book ai didi

iphone - NSArray进行描述,反之亦然

转载 作者:行者123 更新时间:2023-12-01 17:32:16 25 4
gpt4 key购买 nike

我有NSMutableArray像:

NSMutableArray *x=[[NSMutableArray alloc]initWithObjects:@"a",@"b",@"c",@"d", nil];
NSLog(@"%@",x.description);

从描述中我得到以下结果:
(
a,
b,
c,
d
)

我想从描述中重新创建数组,这可能吗?

最佳答案

没有办法在所有情况下都能可靠地工作。例如,您不知道是否

(
123
)

表示包含字符串或数字的数组。

已添加:实际上存在一种将描述转换回数组对象的方法(但由于上述原因,它并不总是返回相同的数组)。 descriptionNSArrayNSDictionary方法
使用可以使用 NSPropertyListSerialization读取的 Old-Style ASCII Property Lists格式:
NSArray *a1 = @[@"123", @456];
NSString *desc = [a1 description];

NSData *d = [desc dataUsingEncoding:NSUTF8StringEncoding];
NSArray *a2 = [NSPropertyListSerialization propertyListWithData:d
options:0
format:NULL
error:NULL];
NSLog(@"%@", a2);
NSLog(@"a1[1] is a %@", [a1[1] class]);
NSLog(@"a2[1] is a %@", [a2[1] class]);

输出:

(
123,
456
)
a1 [1]是__NSCFNumber
a2 [1]是__NSCFString

如您所见,数组 a2看起来像 a1,但是第二个元素 456最初是 NSNumber,已经作为 NSString读回。

因此,您应该仅将 description用作调试工具。其他答案和评论中提到了创建可逆的人类可读描述或存档的更好方法。

关于iphone - NSArray进行描述,反之亦然,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16783635/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com