iOS Review Note 14: Common data structures and the like

Source: Internet
Author: User
Tags dateformat

A nsstring/nsmutablestring string
1 nsstring
<pre name= "code" class= "OBJC" >nsstring* S1 = @ "string";//nsstring* s2 = [[NSString alloc] Initwithformat (@ "%d is one ", 1)]; nsstring* s2 = [NSString stringWithFormat (@ "%d is one", 1)];//C language strings and OC strings convert each other//nsstring* OS = [[NSString alloc] Initwith Utf8string: "C String"]; nsstring* OS = [NSString stringwithutf8string: "C String"];const char* cs = [OS utf8string]; Initialize string with file content//nsstring* s3 = [[NSString alloc] initwithcontentsoffile:@ "file absolute path"//encoding:nsutf8stringencoding//In Text code//ERROR:NIL]; nsstring* s3 = [nsstring stringwithcontentsoffile:@ "File absolute path" encoding:nsutf8stringencoding//Chinese encoding error:nil];//url:/// Resource path//protocol header:///resource path//file:///resource Path//ftp:///resource path//nsurl* URL = [[Nsurl alloc] initwithstring:@ "file://path"]; nsurl* url = [nsurl urlwithstring:@ "file://path"];//nsstring* s6 = [[NSString alloc] initwithcontentsofurl:url//encoding: nsutf8stringencoding//Chinese code//Error:nil]; nsstring* s6 = [NSString stringWithContentsOfURL:urlencoding:NSUTF8StringEncoding//Chinese code Error:nil];//String split nsarray* arr = [@ "one|two|three|four|five" componentsseparatedbystring:@ "|"] Write file [s1 writetofile:@ "file path" Atomiccally:yes encoding:nsutf8stringencoding Error:nil]; [S1 WriteToURL:urlatomiccally:YES encoding:nsutf8stringencoding Error:nil]; nsstring* Strnum = @ "ten"; int n = [Strnum intvalue];


Subclass of 2 Nsmutablestringnsstring
nsmutablestring* ms1 = [nsmutablestring stringwithformat:@ "My age is 10"];//add string [ms1 appendstring:@ "or 13"];//no return value, modify M The value of S1 itself//delete string Nsrange RG = [S1 rangofstring:@ "age"]; [Ms1 DELETECHARACTERSINRANGE:RG]; nsstring* ns1 = @ "my age"; nsstring* ns2 = [ns1 stringbyappendingstring:@ "is 10"];//returns a new string without modifying the value of NS1




Two sets of classes
1 Nsarray/nsmutablearray Array
OC arrays can only hold OC objects and cannot hold the underlying data type and nil

Nsarray: Immutable group nsmutablearray: variable array//array creation nsarray* ARR1 = [Nsarray array]; nsarray* arr2 = [arraywithobject:@ "name"];//nil array element end flag nsarray* ARR3 = [arraywithobjects:@ "name" @ "id", nil]; nsarray* ARR4 = @[@ "One", @ "one", @ "three", @ "four"];//array elements unsigned long count1 = [Arr3 count];unsigned long count2 = ARR3 . count;//array element Access nsstring* e1 = [Arr3 objectatindex:1]; nsstring* e2 = arr3[0];//array traversal for (int i = 0; i < Arr4.count; ++i) {arr4[i];} for (id obj in arr4) {obj;    [Arr4 indexofobject:obj];//gets the subscript of the array}[arr4 enumerateobjectsusingblock:^ (id obj, Nsuinteger idx, BOOL *stop) {if (idx = = 1) {*stop = yes;//jumps out of traversal}}];/ /variable array nsmutablearray* marr = [[Nsmutablearray alloc] init];; [Marr addobject:@ "one"]; [Marr addobject:@ ""]; [Marr addobject:@ "three"];//variable array-delete [Marr removeobject:@ "one"]; [Marr removeobjectatindex:0]; [Marr Removeallobjects];



2 Nsset/nsmutableset Collection
Only OC objects can be stored, cannot hold the underlying data type and hold nil
nsset* ST1 = [Nsset set]; nsset* st2 = [Nsset setwithobject:@ "one"]; nsset* ST3 = [Nsset setwithobjects:@ "one", @ "a", nil];//randomly takes out an object nsstring* ssobj = [St3 anyobject]; nsmutableset* mst = [Nsmutableset set]; [MST addobject:@ "three"];//delete [MST removeobject:@ "one"]; [MST removeallobjects];3 nsdictionary/nsmutabledictionary Dictionary//create nsarray* keys = @[@ "name" @ "Address"]; nsarray* OBJS = @[@ "June", @ "China"]; nsdictionary* Dict1 = [nsdictionary dictionarywithobjests:objs Forkeys:keys]; nsdictionary* dict2 = [nsdictionary dictionarywithobjestsandkeys:@ "name", @ "June", @ "address", @ "China", nil]; nsdictionary* dict3 = @{@ "name": @ "June", @ "address": @ "China"};//variable dictionary nsmutabledictionary* msdict = [nsdictionary dictionary];//add element [Dict1 setobject:@ "June" forkey:@ "name"]; [Dict1 setobject:@ "Hanfeng" forkey:@ "name"];//overwrite the previous value//remove [Dict1 removeobjectforkey:@ "name"];//Access id obj1 = [dict1  objectforkey:@ "Name"];id obj2 = dict1{@ "name"}//get the number of key-value pairs int count = dict1.count//Get all the key values and then traverse this array nsarray* ks = [Dict1 AllKeys]for (int i = 0; i < Dict1.count; ++i) {id k = Keys[i];id v = dict1[k];//[dict1 objectforkey:k];} [Dict1 enumeratekeysandobjectsusingblock:^ (id key,id obj, BOOL *stop) {//Key, obj}];


The above three classes (Nsarray, Nsset, nsdictionary) are called collection classes
Nsarray/nsmutablearray
* Orderly
* Quick Create: @[obj1,obj2,...]
* Quick access: array name [i]


Nsset/nsmutableset
* Unordered


Nsdictionary/nsmutabledictionary
* Unordered
* Quick Create: @{key1:value1,key2:value2,...}
* Quick access: Dictionary name {key}


Three nsvalue/nsnumber numbers
1 Nsvalue
Wrapping other data types (common structures) into OC objects so that they can be put into OC arrays and dictionaries.
Cgpoint p = cgpointmake (10, 10); nsvalue* v = [nsvalue valuewithpoint:p]cgpoint pt = [v Pointvalue];



2 NSNumber
The underlying data type (number) is loaded into an object so that it can be put into the OC array and the dictionary.
NSNumber NUM1 = [NSNumber numberwithint:10]; NSNumber num2 = [NSNumber numberwithlong:10]; NSNumber num3 = [NSNumber numberwithbool:true]; NSNumber num4 = [NSNumber numberwithfloat:10.0]; NSNumber num4 = @10;//quick Convert int count = 5; NSNumber NUM5 = @ (count), ... int i = [NUM1 intvalue];int l = [num1 longvalue];int b = [num1 boolvalue];int f = [Num1 floatv Alue];..




Four nsdate dates
<pre name= "code" class= "OBJC" >nsdate* d1 = [NSDate date];//return current time NSLog (@ "%@", D1)//GMT time (0 o'clock area), with Beijing (East eight district) Time difference 8 hours nsdate* d2 = [nsdate datewithtimeinterval:5 sincedate:d1];//interval nstimeinterval sec1 = [D2 timeIntervalSince1970] ; Nstimeinterval sec2 = [D2 timeintervalsincenow];//date formatting nsdateformatter* f1 = [[NSDateFormatter alloc]init];//y year M-month D-Day H ( 12-hour system) H (24-hour system) m min s s f1.dateformat = @ "Yyyy-mm-dd HH:mm:ss"; nsstring* datestr = [F1 stringfromdate:d1]; nsstring* timestr = @ "2015/02/04 16:57"; nsdateformatter* F2 = [[NSDateFormatter Alloc]init];f2.dateformat = @ "Yyyy/mm/dd hh:mm"; nsdate* d3 = [F2 datefromstring:timestr];


iOS Review Note 14: Common data structures and the like

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.