Notes on the use of NSString in Objective-c:
One: Declares the attribute format of the NSString type
The property declaration is copy, which is a copy of the incoming parameter if the nsmutablestring type of data, so that changes in external parameters do not affect the value of the property.
1 @property (nonatomic,copy) NSString *str;
II: NSString Creation
//method One str1 pointer variable to the address of the @ "abc" in the in-memory literal constant area . NSString *str1=@"ABC";//method Two uses the Alloc method, but does not request memory in the heap area of memory, but applies memory in the literal constant area . NSString *str2=[[nsstring Alloc] initwithstring:@"ABC"];//method Three (method three and method one are considered equivalent in Xcode, and if you write a third method, Xcode suggests that the first method be written) nsstring *str3=[nsstring stringwithstring:@ "abc"];//method Four does not have a alloc, but the system will request memory in the heap area , and the reference count is initialized to 1.NSString *str4=[nsstring stringWithFormat:@"ABC"];//method Five uses the Alloc method to request memory in the heap area , and the reference count is initialized to 1.NSString *str5=[[nsstring Alloc] Initwithformat:@"ABC"];
1. Address assignment rules in the literal constant area : As long as the values are equal, the same memory area is shared, and the memory addresses of STR1 and str2 are the same.
2. The retaincount of the string in the literal constant area is set to a very large count, so the retain or release operation of the string variable in the literal constant area is equal to the invalid.
These string constants persist during the program's run and are managed by the system recycle when the program ends.
3.STR4 and STR5 are all in the heap area to apply for memory, as long as the memory requested in the heap, there will be a reference count, both methods initialize the Retaincount are set to 1, although the values of both methods are @ "ABC", but method four and method five memory address in the heap area is not the same, That is, STR4 and STR5 are two areas of memory in the heap area.
Three: Copy and mutablecopy mechanism of nsstring
1. Two copy methods for immutable strings
1 // Source String 2 nsstring *str1=@ "ABC"; 3 4 // immutable copies, both STR2 and str1, point to the @ "ABC" string in the constant area, str2 equals str1 5 nsstring *str2=[str1 copy]; 6 7 // variable replication, this method requests memory in the heap area, and copies the value of STR1. There are reference counts, but STR3 are immutable strings. 8 nsstring *str3=[str1 mutablecopy];
2. Two copy methods for variable strings
// source string, created in heap area string ]; [Str1 appendString: @" ABC " ]; // Immutable Replication NSString *str2=[str1 copy]; // variable Replication NSString *STR3=[STR2 Mutablecopy];
Both STR1,STR2 and STR3 are in the heap area, both have reference counts, and the value of Retaincount is 1.
STR2 and STR3 are immutable strings.
Four: Conversion of nsstring and C strings
The NSString string in the source code begins with @, such as: @ "ABC"
In Objective-c, the string of C is directly quoted: "ABC"
1.c string = NSString String code example:
1 // 2 char *s= " abc " 3 // 5 nsstring *str=[nsstring stringwithcstring:s encoding:nsutf8stringencoding]; 6 // 8 nsstring *str1=[[ NSString Alloc] initwithcstring:s encoding:nsutf8stringencoding];
Both methods of converting nsstring are created in the heap area , and both have reference counts.
2.NSString string = c String code example:
NSString *str= @ "ABC"; // Method One Const Char *s1=[str utf8string]; // Method Two Const Char *s2=[str cstringusingencoding:nsutf8stringencoding];
The return value of the two methods must be set to the const char * type.
V: string and Array conversions
1. String Conversions to arrays
// string NSString *data=@ "15,13,18,9,23"; // Array Nsarray *temp=[data componentsseparatedbystring:@ ","];
2. Array conversion to string
// Array Nsarray *[email protected][@"1"@"2"@"3 " ]; // string NSString *str=[arr componentsjoinedbystring:@ ","];
Six: Comparison of strings
NSString *str1=*str2=@ "This isa string"; // The comparison must determine whether the recipient of the message is nil if (str1 && [str1 compare:str2] = = nsordersame) { NSLog (@ " equals " ) ); }
iOS Knowledge point Summary one (nsstring)