Overview of the iOS dictionary

Source: Internet
Author: User

Groups the objects in a list, using key-value pairs in groups
The Nsdictionary nsmutabledictionary class creates a list of objects with keys.
If you need to add or remove objects to the dictionary, use Nsmutabledictionary
1 Creation of the dictionary Alloc constructor or Dictionarywithobjects:forkeys
Dictionaries created with nsdictionary cannot be changed once created, and dictionaries created with nsmutabledictionary can be changed.
Nsarray *listofobjects=[nsarray arraywithobjects:@ "Hello World" @ "Bonjour tout le monde" @ "Hola Mundo", nil];
Nsarray *listofkeys=[nsarray arraywithobjects:@ "中文版", @ "French", @ "Spanish", nil];
Nsdictionary *dictionary2=[nsdictionary dictionarywithobjects:listofobjects Forkeys:listofkeys];
Constructor key value
#import <Foundation/Foundation.h>
int main (int argc,const char *argv[]) {
@autoreleasepoo; {
Nsdictionart *dictionary1=[[nsdictionary Alloc]init];
Nsarray *listofobjects=[nsarray arraywithobjects:@ "Hello World" @ "Bonjour tout le monde" @ "Hola Mundo", Nil];
Nsarray *listofkeys=[nsarray araywithobjects:@ "中文版", @ "French", @ "Spanish", nil];
Nsdictionary *dictionary2=[nsdictionary Dictionarywithobjects:listofobject Forkeys:listofkeys];
NSLog (@ "dictionary2=%@", Dictionary2);
}
return 0;
}
Print out the following message
The%@ corresponds to the following values:
English= "Hello World";
French = "Bonjour tout le monde";
Spanish= "Hola Mundo";


Issue: You want to obtain references to objects in the dictionary to access their properties or to send messages to the object.
Use the Objectforkey: method to obtain the corresponding reference for the key you provided:
NSString *helloworld=[dictionary objectforkey:@ "中文版"];






/**
*example
*/
#import <Foundation/Foundation.h>
int main (int argc,const *argv[]) {
autoreleasepool{
Nsarray *listofobjects=[nsarray arraywithobjects:@ "Hello World" @ "Bonjour tout le monde" @ "Hola Moundo", nil];
Nsarray *listofkeys=[nsarray arraywithobjects:@ "中文版", @ "French", @ "Spanish", nil];
Nsdictionary *dictionary=[nsdictionary dictionarywithobjects:listofobjects Forkeys:listofkeys];
NSString *helloworld=[dictionary objectforkey:@ "中文版"];
NSLog (@ "%@", HelloWorld);
}
return 0;
}


/**
* Gets the number of elements in the dictionary the Count property in the Nsdictionary class can be
*/
#import <Foundation/Foundation.h>
int main (int argc,const char *argv[]) {
@autoreleasepool {
Nsarray *listofobjects=[nsarray arraywithobjects:@ "Hello World" @ "Bonjour tout le monde" @ "Hola Mundo", nil];
Nsdictionary *listofkeys=[nsarray arraywithobjects:@ "中文版", @ "French", @ "Spanish", nil];
Nsdictionary *dictionary=[nsdictionary dictionarywithobjects::listofobjects Forkeys:listofkeys];
Nsuinteger Count=dictionary.count;
NSLog (@ "The dictionary contains%lu items", count);
}
return 0;
}


/**
* Traverse the dictionary to send the same message to each object in the dictionary or access the same properties
*allvalues converting a dictionary to an array
* * Use For-each to iterate through an array
* * Use Enumeratekeysandobjectsusingblock: Process each object in the dictionary
*/
For (NSString *s in [dictionary allvalues]) {
NSLog (@ "value:%@", s);
}
Allvalues will return the key value as an array
/**
* You can also use blocks via Emumeratekeysandobjectsusingblock
*/
[Dictionary emueratekeysandobjectsusingblock:^ (ID key,id obj,bool *stop) {
NSLog (@ "key=%@", key,obj);
}];


/**
* Traverse
*/
#import <Foundation/Foudation.h>
int main (int argc,const char *argv[]) {
@autoreleasepool {
Nsarray *listofobjects=[nsarray arraywithobjects:@ "Hello World" @ "Bonjour tout le monde" @ "Hola Mundo", nil];
Nsarray *listofkeys=[nsarray arraywithobjects:@ "中文版", @ "French", @ "Spanish", nil];
Nsdictionary *dictionary=[nsdictionary dictionarywithobjects:listofobjects forkeys:listofkeys[;
For (NSString *s in [dictionary AllKeys]) {
NSLog (@ "key:%@", s);
}
[Dictionary enumeratekeysandobjectsusingblock:^ (ID key,id obj,bool *stop) {
NSLog (@ "key =%@ and obj =%@", key,obj);
}];
}
return 0;
}


/*
* Manipulate dictionaries to add, delete, and insert objects in the dictionary. Way to use Nsmutabledictionary
*/
Object is created:
Nsmutabledictionary *dictionary = [[Nsmutabledictionary alloc]init];
To add a key-value pair
[Dictionary setobject:@ "Hello World" forkey:@ "中文版"];
[Dictionary setobject:@ "Bonjour tout le mode" forkey:@ "French"];
[Dictionary setobject:@ "Hola Mundo forkey:@" Spanish "];








[Dictionary [email protected] "French"];








/**
*example
*/
#import <Foundation/Foundation.h>
int main (int argc,const char *argv[]) {
@autoreleasepool {
Nsmutabledictionary *dictionary = [[Nsmutabledictionary alloc]init];
[Dictionary setobject:@ "Hello World" forkey:@ "中文版"];
[Dictionary setobject:@ "Bonjour tout le monde" forkey:@ "French"];
[Dictionary setobject:@ "Hola Mundo" forkey:@ "Spanish"];
NSLog (@ "OBJECT ADDED to dictionary:%@", DICTIONARY);
[Dictionary removeobjectforkey:@ "French"];
NSLog (@ "objected removed from dictionary%@", DICTIONARY);
[Dictionary removeallobjects];
NSLog (@ "All OBJECTS removed from dictionary:%@", DICTIONARY);
}
return 0;
}


After the first addition, the result is:
OBJECT ADDED to dictionary:{
English= "Hello World";
French= "Bonjour tout le monde";
Spanish= "Hola Mundo";
}
Remove an element for the first time
OBJECT removed from dictionary:{
中文版 = "Hello World";
Spanish= "Hola Mundo";
}
Finally, all the elements have been removed.
All OBJECTS removed from dictionary:{
}




Saves the objects in the dictionary to the file system for later or other programs to use.
There are writetofile in nsdictionary to write the dictionary to the file.
Nsarray *listofobjects=[nsarray arraywithobjects:@ "Hello World" @ "Bonjour tout le monde" @ "Hola Mundo", nil];
Nsarray *listofkeys=[nsarray arraywithobjects:@ "中文版", @ "French", @ "Spanish", nil];
Nsdictionary *dictionary=[nsdictionary dictionarywithobjects:listofobjects Forkeys:listofkeys];


NSString [email protected] "/users/shared/dictionary.txt";
[Dictionary writetofile:filepathname Atomically:yes];








/**
*example
*/


#import <Foundation/Foundation.h>
int main (int argc,const char *argv[]) {
@autoreleasepool {
Nsarray *listofobject=[nsarray arraywithobjects:@ "Hello World" @ "Bonjour tout le monde" @ "Hola Mundo", nil];
Nsarray *listofkeys = [Nsarray arraywithobjects:@ "中文版", @ "French", @ "Spanish", nil];
Nsdictionary *dictionary=[nsdictionary dictionarywithobjects:listofobjects Forkeys:listofkeys];
NSString [email protected] "/users/shared/dictionary.txt";
[Dictionary writetofile:filepathname Atomically:yes];
}
return 0;
}
/**
* Content in the file
*/
<?xml version= "1.0" encoding= "Utf-8"?>
<! DOCTYPE plist Public "-//apple//dtd plist 1.0//en" "Http://www.apple.com/DTDS/PropertyList-1.0.dtd" >
<plist version= "1.0" >
<dict>
<key>english</key>
<string>hello world</string>
<key>french</key>
<string>bonjour tout le monde</string>
<key>spanish</key>
<string>hola mundo</string>
</dict>
</plist>


/**
* Read File Initwithcontentsoffile
*/
NSString *[email protected] "/users/shared/dictionary.txt";
Nsdictionary *dictionary=[[nsdictionary Alloc]initwithcontentsoffile:filepathname];


/**
*example
*/
#import <Foundation/Foundation.h>
int main (int argc,const char *argv[]) {
@autoreleasepool {
NSString *[email protected] "/users/shared/dictionary.txt";
Nsdictionary *dictionary=[nsdictionary Alloc]initwithcontentsoffile:filepathname];
NSLog (@ "dictionary:%@", dictionary);
}
return 0;
}























































Overview of the iOS dictionary

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.