Setobject:forkey: The difference from Setvalue:forkey:

Source: Internet
Author: User

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:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.