Method for creating nsstring:
Nsstring * str1 = @ "Hello World ";
Nsstring * str2 = [[nsstring alloc] init];
Str2 = @ "Hello World ";
[Str2 release];
Nsstring * str3 = [[nsstring alloc] initwithstring: @ "Hello World"];
[Str3 release]; // The memory is recycled, but the variable can change the pointer position and continue to use this variable.
Str3 = [nsstring stringwithstring: @ "hello"]; // This method does not require memory management because it references the static nsstring method.
// Input a string in the C language and convert it into an OC string
Nsstring * str4 = [[nsstring alloc] initwithuf8string: "Hello World"];
[Str4 release];
Str4 = [nsstring stringwithuf8string: "Hello"]; // This method does not require memory management because it references the static nsstring method.
// Format %. 2f: Float Type, retain two decimal places
Nsstring * str5 = [[nsstring alloc] initwithformat: @ "My age is % I and height is %. 2f", 25, 1.76f];
[Str5 release];
Str5 = [nsstring stringwithformat: @ "hello"]; // This method does not require memory management because it references the static nsstring method.
Memory leakage occurs in the following methods:
// Format %. 2f: Float Type, retain two decimal places
// [[Nsstring alloc] initwithformat: @ "My age is % I and height is %. 2f", 25, 1.76f]; not released
Nsstring * str5 = [[nsstring alloc] initwithformat: @ "My age is % I and height is %. 2f", 25, 1.76f];
// [Nsstring stringwithformat: @ "hello"]; it is automatically released and should not be released by running [str5 release ].
Str5 = [nsstring stringwithformat: @ "hello"];
[Str5 release]; // The Field pointer is incorrect.
Below is the demoCode:
The demo mainly demonstrates pointer pointing to a pointer, reading remote and local file content
# Import <Foundation/Foundation. h>
Void test (){
Nsstring * Path = @ "/users/gongpb/develop/nsstring/test.txt ";
// Expiration method, which cannot read Chinese Characters
Nsstring * STR = [nsstring stringwithcontentsoffile: path];
Nslog (@ "text.txt content is: % @", STR );
Nsstring * str1 = [nsstring stringwithcontentsoffile: path encoding: nsutf8stringencoding error: Nil];
Nslog (@ "test.txt content is: % @", str1 );
/**
This exception mechanism is not frequently used
@ Try {
}
@ Catch (nsexception * exception ){
}
@ Finally {
}
**/
Nserror * error;
// Error :( nserror **) error; ** indicates the pointer to the pointer
Nsstring * str2 = [nsstring stringwithcontentsoffile: Path
Encoding: kcfstringencodinggb_18030_2000 error: & error];
If (error = nil ){
Nslog (@ "success ");
} Else {
Nslog (@ "error: % @", error );
}
Nslog (@ "text.txt content is 2: % @", str2 );
// File: // The two backslashes are fixed. A backslashes at the front of the users indicate the path
Nsurl * url = [nsurl urlwithstring: @ "file: // users/gongpb/develop/nsstring/test.txt"];
Nsstring * urlstr1 = [nsstring stringwithcontentsofurl: URL
Encoding: nsutf8stringencoding error: Nil];
Nslog (@ "url content is: % @", urlstr1 );
// Remote URL
Nsurl * remoteurl = [nsurl urlwithstring: @ "http://www.baidu.com"];
Nsstring * remotestr = [nsstring stringwithcontentsofurl: remoteurl
Encoding: nsutf8stringencoding error: Nil];
Nslog (@ "remote URL content is: % @", remotestr );
}
Void test1 (nsstring * Str ){
STR = @ "123 ";
}
Void Test2 (nsstring ** Str ){
(* Str) = @" 456 ";
}
# Pragma mark string Export
Void stringexport (){
Nslog (@ "-------- export --------");
Nsstring * STR = @ "123456 ";
Nsstring * Path = @ "/users/gongpb/develop/nsstring/abc.txt ";
Nserror * error;
[STR writetofile: path atomically: Yes encoding: nsutf8stringencoding error: & error];
If (error = nil ){
Nslog (@ "success ");
} Else {
Nslog (@ "error: % @", [error localizeddescription]);
}
}
Int main (INT argc, const char * argv [])
{
@ Autoreleasepool {
Test ();
// The pointer variable stores the address
Nsstring * s = nil;
Test1 (s); // The value of s cannot be changed.
Nslog (@ "s value = % @", S );
// Point & S incoming s address
Test2 (& S );
Nslog (@ "Age is % @", S );
// Export
Stringexport ();
}
Return 0;
}