First, the immutable dictionary can be setvalue:forkey:, but not the actual operation, which depends on the immutable dictionary cannot be added or deleted features.
Then take a look at Setobject:forkey:, Setvalue:forkey:, Setvalue:forkeypath: The standard invocation statement:
[Mudict setvalue:<# (NullableID) #> forkey:<# (nonnull nsstring *) #>];
[Mudict setvalue:<# (NullableID) #> forkeypath:<# (nonnull nsstring *) #>];
[Mudict setobject:<# (nonnullID) #> forkey:<# (nonnullID<NSCopying>) #>]; can clearly see:
(1) Setvalue:forkey: The value can be nil (but when value is nil, the Removeobject:forkey method is automatically called);
Setobject:forkey: The value cannot be nil.
(2) Setvalue:forkey: The key must be a string type that is not nil;
Setobject:forkey: The key can be all types that are not nil.
//Demo:
Nsmutabledictionary *dict = [Nsmutabledictionary dictionarywithdictionary:@{@"CS":@"Test"}];
Nsdictionary *testdict = @{@"name":@"test_name",@" Age":@" A",@"Dict":D ICT};
Nsmutabledictionary *mudict = [Nsmutabledictionary dictionarywithdictionary:testdict];
[Mudict SetObject:@"Object"Forkey:@"Key"];
[Mudict SetValue:@"value"Forkey:@"Key2"];//The results are printed at this time:
{
Age = A;
Dict = {
CS = test;
};
Key =Object;
Key2 = value;
Name ="test_name";
}
Here Setobject:forkey: With the Setvalue:forkey: The role is the same, first detect whether there is a key in the Mudict value pairs, existing directly replace the original value, no one inserted a new key value pair.
[Mudict SetValue:@"Ceshi"Forkeypath:@"Dict.cs"];
NSLog (@"%@", mudict);//The results are printed at this time:
{
Age = A;
Dict = {
CS = Ceshi;
};
Key =Object;
Key2 = value;
Name ="test_name";
}
Here is the operation of the Mudict, Processing compound path Dict.cs, first detect mudict in the presence of dict corresponding key value pairs, there is to detect whether there is a CS corresponding key value pairs, there is a replacement value, there is no key value to create a pair, if the mudict does not detect the presence of dict, then stop the operation.
Setvalue:forkey: With Setvalue:forkeypath:
Dynamic settings: SetValue: Property value Forkey: Property name (for simple Path), SetValue: Property value Forkeypath: Property path (for compound path, for example, person has an account type property, Then Person.account is a composite property)
Dynamic read: Valueforkey: Property name, Valueforkeypath: property name (for compound path)
Setobject:forkey: The difference from Setvalue:forkey: