问题:有没有办法使用现有的objective-c方法来完成NSDictionary或NSArray的完整深层副本,它们本身有嵌套的词典或数组?
那是我已经读过的问题可能是当它遇到嵌套的字典或数组时它只复制指向嵌套项的指针,而不是真正复制该项.
背景:作为一个例子,我正在尝试使用NSUserDefaults加载/保存以下配置,并且当加载需要将不可变副本转换为NSUserDefault之前进行更改之前的可变副本.
我将json解析结果保存在以下dictionary内容中:
{
"statusCode":"200",
"body":[
{
"status":"success",
"remarks":null
}
],
"data":[
"abcd":[
{
"category":"a",
"title":"b",
"id":"24"
},
{
"category":"c",
"title":"crd",
"id":"65"
},
{
"category":"ds",
"title":"sd",
"id":"18"
}
]
},
{
"efgh":[
{
"category":"ds",
"title":"sd",
"id":"18"
},
{
"category":"sd",
"title":"sd",
"id":"1"
}
]
},
{
"ijkl":[
{
"category":"ds",
"title":"sd",
"id":"18"
},
{
"category":"sd",
"title":"sd",
"id":"1"
}
]
}
]
}
Run Code Online (Sandbox Code Playgroud)
key @"data"的数据可以通过使用保存到数组中
NSMutableArray *getdata=[[NSMutableArray alloc]init];
getcat=[results objectForKey:@"data"];
Run Code Online (Sandbox Code Playgroud)
现在我应该为values(category, title, id)第一个索引内部做些什么,即"abcd".
如果有人有任何知识,请看看.
谢谢大家.
我试图显示字典格式的数据.下面,有三次尝试.首次尝试,输出顺序完全改变.第二次尝试,输出顺序与输入相同.但是,在第三次尝试中,我将变量声明为NSDictionary.我收到的确切输出.为什么这会改变字典?请指导我.我搜索了Swift的字典标签.但我找不到.
//First Attempt
var dict : Dictionary = ["name1" : "Loy", "name2" : "Roy"]
println(dict)
//output:
[name2: Roy, name1: Loy]
//Second Attempt
var dict : Dictionary = ["name2" : "Loy", "name1" : "Roy"]
println(dict)
//output:
[name2: Loy, name1: Roy]
-----------------------------------------------------------
//Third Attempt With NSDictionary
var dict : NSDictionary = ["name1" : "Loy", "name2" : "Roy"]
println(dict)
//output:
{
name1 = Loy;
name2 = Roy;
}
Run Code Online (Sandbox Code Playgroud)
另一个问题:我用游戏场进行验证.我的屏幕截图如下:

在这里,在NSDictionary中,我首先给了name5,但是在右边name2显示,然后,在println中,它以升序显示.为什么会这样?

在这里,在字典中,我给了name5作为第一个,但是在右边name2正在显示,然后,在println中,它正在显示它是如何在Dictionary行上拍摄的.为什么会这样?
我觉得这是一个愚蠢的问题,但无论如何我都会问...
我有一组NSDictionary对象,其键/值对对应于我创建的自定义类,调用它MyClass.有一种简单或"最佳实践"的方法让我基本上做MyClass * instance = [地图NSDictionary属性的事情MyClass ];吗?我有一种感觉,我需要做一些事情,NSCoding或者NSKeyedUnarchiver,而不是自己偶然发现,我认为那里的人可能能指出我正确的方向.
我想保存NSMutableArray或NSDictionary作为永久文件.然后,我想稍后重新打开该文件.
这可能吗?如果是这样,我该怎么办?
我想在一个对象中存储一个归零的弱引用NSDictionary.这是为了引用父项NSDictionary,因此我可以在不进行搜索的情况下爬回大型结构.
我不能__weak在这里使用; 即使我的本地引用很弱,它NSDictionary也会存储对弱引用对象的强引用.而且,当然,NSDictionary不能有nil对象.
我在iOS上,而不是Mac,因此NSHashTable无法使用.我只希望一个物体变弱; 其余的应该仍然很强大.
(我将发布我的答案,所以如果没有更好的答案,我有一些标记为可接受.但我希望有人有更好的答案.)
可以使用(或不使用)NSDictionary将iPhone钥匙串存放在钥匙串中KeychainItemWrapper吗?如果不可能,你有另一种解决方案吗?
我正在和怪物一起玩游戏.每个人都有一个统计数据列表,这些数据都将是整数.我可以将每个stat设置为它自己的变量,但我更喜欢将它们保存在NSDictionary中,因为它们都是相关的.当我试图改变每个统计数据的值时,我遇到了问题.
是)我有的:
-(id) init {
self = [super init];
if(self) {
stats = [NSDictionary dictionaryWithObjectsAndKeys:
@"Attack", 0,
@"Defense", 0,
@"Special Attack", 0,
@"Special Defense", 0,
@"HP", 0, nil];
}
return self;
}
Run Code Online (Sandbox Code Playgroud)
我想做的事
-(void) levelUp {
self.level++;
[self.stats objectForKey:@"Attack"] += (level * 5);
[self.stats objectForKey:@"Defense"] += (level * 5);
[self.stats objectForKey:@"Special Attack"] += (level * 5);
[self.stats objectForKey:@"Special Defense"] += (level * 5);
[self.stats objectForKey:@"HP"] += (level * 5);
}
Run Code Online (Sandbox Code Playgroud)
错误我得到了
Arithmetic on pointer to interface 'id', which is not …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用一个类作为一个关键NSDictionary.我看了这个问题的答案,我所拥有的几乎是一样的; 我正在使用setObject: forKey:.然而,XCode抱怨说Incompatible pointer types sending 'Class' to parameter of type 'id<NSCopying>'.我的电话是:
[_bugTypeToSerializerDictionary setObject: bugToStringSerializer
forKey: [bugToStringSerializer serializedObjectType]];
Run Code Online (Sandbox Code Playgroud)
bugToStringSerializer是BugToStringSerializer具体实现的实例serializedObjectType.具体实现的示例如下所示:
- (Class) serializedObjectType {
return [InfectableBug class];
}
Run Code Online (Sandbox Code Playgroud)
我在这做错了什么?
我想创建一个类似这样的NSDictionary:
"zones":
{
{
"zoneId": "1",
"locations":
{
{
"locId": "1",
"locZoneId": "1",
"locLatitude": "33.68506785633641",
"locLongitude": "72.97488212585449"
},
{
"locId": “2”,
"locZoneId": "1",
"locLatitude": "33.68506785633641",
"locLongitude": "72.97488212585449"
},
{
"locId": “3”,
"locZoneId": "1",
"locLatitude": "33.68506785633641",
"locLongitude": "72.97488212585449"
},
}
}
}
Run Code Online (Sandbox Code Playgroud)
但我不知道如何创造.
nsdictionary ×10
ios ×6
objective-c ×6
iphone ×4
class ×1
cocoa-touch ×1
collections ×1
deep-copy ×1
int ×1
keychain ×1
nsdata ×1
swift ×1