Dark Horse Programmer--OC Learning Summary--founation Framework

Source: Internet
Author: User

------Java Training, Android training, iOS training,. NET training, look forward to communicating with you! -------

1. Commonly used structural bodiesThe Nsrange nspoint nsrect nssize is defined in the Founation framework and they are often created like this: #import <founation/founation.h>void main () {@ Autoreleasepool{//range use Nsrange rang=nsmakerange (10,34);//Specify a starting and ending range//cgrang rang=cgpiontmake (10,34);// Cgrange can also be used to define nstring *str=stringformrange (rang);//String Print NSLog (@ "rang=%@", str); The use of rect contains x y width height nsrect rect=nsmakerect (2,3,45,32);  CGRect Rect=cgrectmake (2,3,45,32) nsstring *str2=stringformrect (rect); }  } Use of 2.NSStringThe NSString immutable string is an OC object that needs to free memory, and the static method call does not have to be freed #import <Founation/Founation.h> void Main () {@autoreleasepool { NSString *string=[[nsstring Alloc] init]//Create a nsstring[email protected] "fff"; [String release]; NSString *str=[[nsstring initwithformat:@ "His was%i", 24];//string formatted output [str release];//static method call NSString *str1=[nsstring stringwithstring:@ "FFFFF"]; NSString *str2=[nsstring stringwithformat:@ "Fffff%i", 34]; NSLog (@ "%@%@", STR,STR2); }  } Use of 3.NSMutableStringis a mutable string that also needs to manage memory #import <Founation/Founation.h> void Main () {@autoreleasepool {[nsmutablestring * str=[[  Nsmutablestring Alloc] initwithcapacity:10]//pre-allocated 10 words storage space for increased efficiency [str setstring:@ "SS"];  [Str appendstring:@ "FFF"];//splicing string [str appendformat:@ "FFFF age was%i and Heighe is%2f", 23,1,72];//formatted output [str release]; } 4.NSArray Non-variable group#import <Founation/Founation.h> void Main () {@autoreleasepool {nsarray *arr=[nsarray arraywithobjects:@ "JH", @ " WK ", @" 4 ", @" 5 ", nil];//definition array [arr objectatindex:2];//remove the third element [arr indexofobject:@" wk "];//remove the corner mark [Arr lastobject];// Remove the last element [arr count];//extract element number//traverse int i=0;for (ID ob in arr) {NSLog ("%i\t%@", i,obj); i++;//sort Nsarray *array=[nsarray arraywithobjects:@ "", @ "4", @ "2", Nil]nsarray *arr2=[array Sortedarrayusingseletcor: @seletcor (compare:)];//    Sort well to return a new array NSLog (@ "%@", arr2); }    } 5.NSMutableArray variable Array#import <Founation/Founation.h> void Main () {@autoreleasepool {nsmutablearray arr=[nsmutablearray array]; [Arr addobject:@ "FF"];//add content [arr removeobject:@ "ff"]//delete content Student *s=[[student alloc] init]; [Arr addobject:s]; Retain once [arr removeobject:s]//release once [s release]; [ARR release]//When the array is released, it will do a release operation for all elements once//sort Nsmutablearray arr=[nsarray arraywithobjects:@ "4" @ "3" @ "5" @ "2", nil ][arr Sortusingseletcor: @seletcor (compare:)];//sort well returns itself NSLog (@ "%@", array);} 6.NSDictionary Immutable Dictionary#import <Founation/Founation.h> void Main () {@autoreleasepool {nsdictionary *dict=[nsdictionary  dictionarywithobjectsandkeys:@ "v1", @ "V2", @ "V3", @ "K1", @ "K2", @ "K3", nil]; ID v_= [dict objectforkey:@ "K2"];//dictionary value can only be changed [Dict count];//dictionary all elements [dict allvalues];//dictionary All values can only be changed [Dict allkeys];/  /dictionary all key//traversal for (ID key in dict) {ID value=[dict objectforkey:key];   NSLog (@ "%@--%@", Key,value); }    }   7.NSMutableDictionary Non-variable group#import <Founation/Founation.h> void Main () {@autoreleasepool {nsmutabledictionary *dit=[nsmutabledictionary  Dictionary]; [Dict setobject:@ "v2" forkey:@ "K2"];//add element [dict removeallobjects];//Delete all key value pairs [dict [email protected] "K2"]// Delete the value//Dictionary of the Key object when destroyed will release each element once}   Use of 9.NSNumber#import <Founation/Founation.h> void Main () {@autoreleasepool {nsnumber *n=[nsnumber numberwithint:100];//  Wrap int Type 10 as NSNumber object Nsarray *arr=[nsarray Arraywithobject:num];  ID Num=[arr lastobject];//out of the array int n1=[num intvalue];//converted to int type NSLog (@ "n1=%i", N1); The result of the operation is: n1=100 Use of NSDate#import <Founation/Founation.h> void Main () {@autoreleasepool {nsdate *date=[nsdate date];//creation Date Date=[nsdate DATEWITHTIMEINTERVALSINCENOW:20]; Returns a time that is 20 seconds faster than now [date distantfuture];//automatically returns a future distant time [date distantpast];//randomly returns a time in the past NSDateFormatter *format=[[ns Dateformatter alloc] init]; [Email protected] "YYYY-MM-DD HH:mm:ss";//Format datetime nsstring *str=[format stringfromdate:date];//date format converted to string NSDate date1  =[format datefromstring:@ "2008-08-08 07:34:43"];//character format converted to date format NSLog (@ "str=%@", SRT);   NSLog (@ "date=%@". Date1);   [Format release]; The result of the operation:

Dark Horse Programmer--OC Learning Summary--founation Framework

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.