The dictionary NSDictionary in OC and the dictionary nsdictionary in oc

Source: Internet
Author: User

The dictionary NSDictionary in OC and the dictionary nsdictionary in oc

======================================

Dictionary

======================================

NSDictionary * dict = [[NSDictionary alloc] initWithObjectsAndKeys: @ "One", @ "1", @ "Two", @ "2", @ "Three ", @ "3", @ "One", @ "4", nil];

// The data in the dictionary is stored as a key-value pair.

// @ "One" and @ "1" form a key-Value Pair

// @ "1" is called a key)

// @ "One" is called a value)

// The value is the data to be stored, and the key is the index of the data to be searched.

// The function of the dictionary is to quickly find the value through the key.

// The value can be repeated and the key is unique.

// The key-value pairs in the dictionary are not ordered. There is no so-called first key-value pair, and the second key-Value Pair

// Keys and values are all arbitrary objects, and the address of the objects stored in the dictionary. However, keys often use strings.

NSDictionary * dict2 =@ {@ "4": @ "Four", @ "1": @ "One", @ "2": @ "Two ", @ "3": @ "Three "};

NSLog (@ "% @", dict );

NSLog (@ "% @", dict2 );

// The address of the returned value through the Input key

NSString * value = [dict objectForKey: @ "3"];

Value = dict [@ "3"];

// After Xcode 4.6

// If no corresponding key exists, nil is returned.

NSLog (@ "% @", value );

// Total number of return key-value pairs

NSUInteger count = [dict count];

NSLog (@ "% lu", count );

// Return all keys

NSArray * keys = [dict allKeys];

// Return all values

NSArray * values = [dict allValues];

// Traverse the dictionary

For (NSString * key in dict ){

// Each cycle key points to a key

// Uses the traversal key to indirectly traverse the value

NSLog (@ "% @", dict [key]);

}

NSMutableDictionary * mutableDict = [[NSMutableDictionary alloc] init];

// Reset the dictionary

[MutableDict setDictionary: @ {@ "1": @ "One", @ "2": @ "Two", @ "3": @ "Three"}];

// Add

[MutableDict setObject: @ "Four" forKey: @ "4"];

// Delete

// Delete a value using the key

[MutableDict removeObjectForKey: @ "3"];

// Delete multiple values using multiple keys

[MutableDict removeObjectsForKeys: @ [@ "1", @ "2", @ "3"];

// Delete all key-value pairs

[MutableDict removeAllObjects];

△Oc is a very beautiful language. Its names are basically a combination of words. You can learn about this method or the role of this variable through the name;

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.