A brief introduction to OBJECT-C------nsdictionary

Source: Internet
Author: User

Main.m

////MAIN.M//Nsdictionary////Created by Zhaogang on 16/5/8.//Copyright 2016 Zhaogang. All rights reserved./// * Collection Nsarray ArrayList nsset HashSet nsdictionary Map 1. Nsarray\nsmutablearray * ordered * fast Creation (immutable): @[] * Quick access element: array name [] 2. Nsset\nsmutableset * Unordered 3. Nsdictionary\nsmutabledictionary * unordered * Quick Create (immutable): @{key:value} * Quick access: dictionary name [key] */#import <Foundation/Foundation.h> intMainintargcConst Char* argv[]) {Nsarray*person = @[@{@"Address": @"Beijing", @"Name": @"Jack", @"Age": @"a"},      @{@"Address": @"Shanghai", @"Name": @"Rose", @"Age": @"very"},      @{@"Address": @"Nanjing", @"Name": @"Mike.", @"Age": @"All"},      @{@"Address": @"Hongkong", @"Name": @"Hobe", @"Age": @"very"}      ];//Nsdictionary *jack = person[0];//NSLog (@ "%@", Jack);    NSLog(@"%@", person[1][@"Address"]);return 0;}voidTest3 () {//dictionary does not allow a key to be used, but allows the same value (object)    nsdictionary*dict = @{@"Address": @"Beijing", @"Name": @"Jack", @"Age": @"a"};//Nsarray *keys = [Dict AllKeys];    //for (int i = 0; i < Dict.count; i++)    //    {    ////[keys objectatindex:i];    //NSString *key = keys[i];    //NSString *object = Dict[key];    //    //NSLog (@ "%@-%@", key, object);    //    }[Dict enumeratekeysandobjectsusingblock:^ (ID_nonnull Key,ID_nonnull obj,BOOL* _nonnull stop) {NSLog(@"%@ - %@", key, obj);//*stop = YES;}];}voidTest2 () {//Error    //Nsmutabledictionary *dict = @{@ "name": @ "Jack"};    //    //[Dict setobject:@ "Rose" forkey:@ "name"];    nsmutabledictionary*dict = [nsmutabledictionaryDictionary];//Add key-value pairs, no order[Dict setobject:@"Jack"forkey:@"Name"]; [Dict setobject:@"Beijing"forkey:@"Address"];//This will overwrite the value of the key above[Dict setobject:@"Rose"forkey:@"Name"];//Remove key-value pairs    //[dict removeobjectforkey:<# (nonnull ID) #>];    NSString*str = dict[@"Name"];NSLog(@"%@", str);//nslog (@ "%@", @[@ "Jack", @ "Rose"]);    NSLog(@"%@", dict);}voidTest () {/* Dictionary: Key---->value index----> the contents stored in the text are key-value pairs .    //nsdictionary *dict = [nsdictionary dictionarywithobject:@ "Jack" forkey:@ "name"];    //Nsarray *key = @[@ "name", @ "address"];    //Nsarray *object = @[@ "Jack", @ "Beijing"];    //    //Nsdictionary *dict = [nsdictionary dictionarywithobjects:object forkeys:key];    //Nsdictionary *dict = [nsdictionary dictionarywithobjectsandkeys:    //@ "Jack", @ "name",    //@ "Beijing", @ "address",    //@ "a", @ "age", nil];    nsdictionary*dict = @{@"Name": @"Jack", @"Address": @"Beijing", @"Age": @"a"};IDobj = [Dict objectforkey:@"Age"];NSLog(@"%@", obj);//Returns the number of key-value teams    NSLog(@"%ld", Dict. Count);}

A brief introduction to OBJECT-C------nsdictionary

Related Article

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.