1.Foundation Frame
Reference a picture
Cocoa can be seen as a collection of many frameworks, including the foundation framework and the Uikit framework in iOS, where the foundation framework is primarily API-ready for program development, and Uikit includes many UI-drawn functions.
2. String manipulation
Initialize string:
NSString *STR1 = [[NSString alloc] init];
STR1 = @ "Hello,ios.";
NSString *STR2 = [[NSString alloc] initwithstring:@ "Hello,ios."];
NSString *STR3 = [[NSString alloc] initwithformat:@ "Age was%i,name is%.2f", 20,56.0f];
NSString *STR4 = [[NSString alloc] initwithutf8string:@ "Hello,ios."];
Format case:
Convert to lowercase
NSLog (@ "%@", [@ "Hello,ios." lowercasestring]);
Convert to uppercase
NSLog (@ "%@", [@ "Hello,ios." uppercasestring]);
First letter uppercase, other lowercase letters
NSLog (@ "%@", [@ "Hello,ios." capitalizedstring]);
Compare strings:
Judging if they are equal
BOOL res = [@ "abc" isequaltostring:@ "abc"];
Size comparison, there are three kinds of results: nsorderedascending/nsordereddescending/nsorderedsame
Nscomparisonresult res = [@ "abc" compare:@ "abc"];
if (res = = nsorderedascending)
NSLog ("Left < Right");
else if (res = = nsordereddescending)
NSLog ("Left > Right");
Else
NSLog ("left = right");
String prefixes and suffixes are judged:
[@ "abcdef" hasprefix:@ "abc"];
[@ "ABCdef" hassuffix:@ "def"];
To find a string:
Nsrange range = [@ "ABCDEFG" rangeofstring:@ "CD"];
if (range.location = = Nsnotfound)
NSLog (@ "not found.");
Else
NSLog (@ ' range is%@ ', Nsstringfromrange (range));//range is {2,2}
String segmentation:
NSLog (@ "%@", [@ "abcdef" substringfromindex:3]);//def
NSLog (@ "%@", [@ "abcdef" substringtoindex:3]);//abc
NSLog (@ "%@", [@ "abcdef" substringwithrange:nsmakerange (2,3)]);//CDE
NSString *str = @ "A.B.C.D.E.F";
Nsarray *array = [str componentsseparatedbystring:@ "];
NSLog (@ "%@", array);//(A,B,C,D,E,F);
Type conversions:
NSLog (@ "%i", [@ "intvalue]);//12
NSLog (@ "%zi", [@ "Hello,ios." length]);//10
NSLog (@ "%c", [@ "Hello,ios." characteratindex:0]);//h
const char *s = [@ ' Hello,ios. Utf8string];
NSLog (@ "%s", s);//hello,ios.
File operation:
Read File contents
NSString *path = @ "/users/shvier/desktop/demo.txt";
NSString *str = [NSString stringwithcontentsoffile:path encoding:nsutf8stringencoding Error:nil];
NSLog (@ "%@", str);//hello,ios.
Write file contents
NSString *path = @ "/users/shvier/desktop/demo.txt";
NSString *str = @ "Hello,ios";
[Str writetofile:path atomically:yes encoding:nsutf8stringencoding error:nil];//atomically represents a one-time write, if an error occurs during writing, Don't write it all up.
The first day of "Objective-c learning record"