Objective-C Programming Reading Notes
Chapter 2 numbers, strings, and sets
String Common Operations
Nsstring * str1 = @ "this is string ";
Nsstring * str2 = [str1 substringwithrange: nsmakerange (8, 6)];
Nsange subrange = [str1 rangeofstring: str2];
If (subrange. Location = nsnotfound)
{
Nslog (@ "string not found ");
} Else {
Nslog (@ "string is at index % lu, length is % lu", subrange. Location, subrange. Length );
}
Nsmutablestring * mstr = [nsmutablestring stringwithstring: str1];
Nsuinteger number = [mstr replaceoccurrencesofstring: @ "this is"
Withstring: @ "an example"
Options: Nil
Range: nsmakerange (0, mstr. Length)];
Common Array Operations
Nsarray * books = @ [@ "book4", @ "book1", @ "book2", @ "book3"];
Nsarray * sortedbooks = [books sortedarrayusingcomparator: ^ nscomparisonresult (ID obj1, Id obj2 ){
Return [obj1 compare: obj2];
}];
For (nsstring * book in sortedbooks ){
Nslog (@ "% @", book );
}
Nsmutablearray * array = [@ [@ "1", @ "2"] mutablecopy];
Array [1] = @ "3 ";
Nslog (@ "% @", array [1]);
Nsmutabledictionary * People = [@ {@ "K1": @ "V1"} mutablecopy];
People [@ "K1"] = @ "V2 ";
Nslog (@ "% @", people [@ "K1"]);
Chapter 4 usage documents
Copy files and use nsprocessinfo
Basic file operations: nsfilehandle
Nsfilemanager * fm = [nsfilemanager defamanager manager];
[Nsbundle mainbundle] pathforresource :( nsstring *) oftype :( nsstring *)]
Nsarray * images = [[nsbundle mainbundle] pathsforresourcesoftype: @ "jpg" indirectory: @ "Images"];
Nsurl * url = [nsurl urlwithstring: @ "http://www.google.com"];
Chapter 4 Memory Management and automatic reference count
// When a large number of temporary objects appear in the program
For (I = 0; I <n; ++ I ){
@ Autoreleasepool {
... // Deal with temporary objects
}
}
When the arc encounters a method call, if the method name starts with alloc, new, copy, mutablecopy, or Int, it assumes that the method returns the object owner to the method caller.
Disable arc for a single file
Compiler flags-Fno-objc-Arc
Chapter 4 copying objects