iOS learns the third day, from an interface file and implementation file (Student.h and STUDENT.M). For related articles Please refer to: OC Featured string processing method.
1. Code specification:
1.1: Capitalize the first letter of the class name
1.2: Nomenclature of methods with hump nomenclature
2. Dry Goods
2.1: To instantiate a class in a class, you need to first introduce the class's interface file, such as ***.h.
The instantiation of a class in 2.2:oc is Student *student = [[Student alloc] init], which can be understood as Student class inherits NSObject method in Alloc class, Alloc This method is used to instantiate an object, INI T is the default construction method
2.3: The method that the object calls in OC is implemented by [], [object name Method name];
? ? Defines the syntax for an object:
? ? ? The class name? * Object name = [[Class name alloc] init]; Or
? ? ? The class name? * Object name = [class name new];
? ? The methods to assign and invoke the object's member variables are as follows:
? ? ? ? object name--member Variable name = specific value;
? ? ? ? [Object name Method name];
2.4: Class methods and Object methods
? ? As mentioned above, the method at the beginning of the minus sign is the object method, which needs to be called by the object after instantiating the class. Object methods allow calling object methods and object variables? The method that starts with a plus sign is a class method that can be called directly by a class.
? ? Here are some rules for some method calls:
? ? ? ? 1. Class methods can invoke class methods;
? ? ? ? 2. Class methods cannot invoke object methods;
? ? ? 3. Class methods are not allowed to use object variables, and class methods can use self (the equivalent of this in Java); ? ?
? ? ? ? 4. Class methods can be called through classes, but objects cannot call class methods
2.5:property Reading and writing:
Property Read-write determines whether attributes have setter methods
? ? ? (1) ReadWrite: Specifies that the property is read-write, which is the default value, so you can omit
? ? ? ? ? @property (ReadWrite) NSString *name;
? ? ? (2) ReadOnly: Indicates that the property is read-only. The system does not setter methods, but there are getter methods
? ? ? ? ? @property (readonly) NSString *name;
2.6: String processing method
1> the creation of the string: nsstring *str = [nsstring stringwithstring:@ "Hello"];
NSString *str = @ "Hello";
2> string Length: Nsuinteger len = [str length];
3> string comparison: = = Compares the pointer of a string isequaltostring the contents of a string, the return value is bool type
Retain: Always shallow copy. The reference count is added one at a time. Returns whether the object is mutable and consistent with the object being copied.
Copy: The reference count does not change for a deep copy of a mutable object, and for an immutable object is a shallow copy, the reference count is added one at a time. Always returns an immutable object.
Mutablecopy: Always deep copy, reference count does not change. Always returns a Mutable object.
4> The comparison function of a string compare
NSString The return value of the following compare function is an enumeration type Nscomparisonresult; Nsorderedascending=-1, nsorderedsame=0,nsordereddescending = 1;?
@" ABCDE " @ "bcdef"= [StrCompare1 compare:strcompare2];
5> concatenation function of strings
Stringbyappendingstring: (NSString *) astring;
Stringbyappendingformat: (NSString *) format ...;
6> Searching for strings
?-(BOOL) Hasprefix: (NSString *) string: Determines whether a string begins with string;
?-(BOOL) Hassuffix: (NSString *) string: Determines whether a string is terminated by string;
?-(Nsrange) rangeofstring: (NSString *) string; pattern-matching string, the return type is a struct-nsrange, and the structure body Nsrange has two values that are integral, where the location represents the position of the substring in the original string, and length represents the lengths.
NSString *parent =@"123456789"; NSString*pre =@"123"; NSString*suf =@"789"; NSString*mid =@"456"; if([parent Hasprefix:pre] = =YES) {NSLog (@"succeed"); } if([parent Hassuffix:suf] = =YES) {NSLog (@"succeed"); } Nsrange Range=[Parent Rangeofstring:mid]; NSLog (@"Location =%lu, length =%lu", (unsignedLong) Range.location, (unsignedLong) range.length);
Interception of 7> strings
-(NSString *) Substringfromindex: (Nsuinteger) from gets the string content from the beginning of the string to the end of the string, and does not contain characters from the from position.
?-(NSString *) Substringtoindex: (Nsuinterger) to get the string contents from the beginning of the string to the position of the string, containing the characters to the position;
-(NSString *) Substringwithrange: (Nsrange) range to obtain the string based on the Nsrange range;
NSString *parent =@"123456789"; NSString*toend = [Parent Substringfromindex:6]; NSLog (@"%@", Toend); NSString*frombegin = [Parent Substringtoindex:3]; NSLog (@"%@", Frombegin); Nsrange Range= {3,3}; NSString*rangestring =[Parent Substringwithrange:range]; NSLog (@"%@", rangestring);
8> type conversion functions for strings
-(double) doublevalue; Converts the contents of a string to a double type
-(float) floatvalue; Convert the corresponding string to float type
-(int) intvalue; converts the corresponding string to an int type
9> string-Case Conversion functions
-(NSString *) uppercasestring: Change the word embox to uppercase;
-(NSString *) lowercasestring: Change the word embox to lowercase;
-(NSString *) Capitalizedstring: Capitalize the first letter;
10> string substitution function
-(NSString *) stringbyreplacingoccurrencesofstring: (NSString *) target withstring: (NSString *) Replacement; Converts the string target to replacement;
?-(NSString *) Stringbyreplacingcharactersinrange: (nsrange) Range withstring: (NSString *) Replacement; Replace the string in the specified range;
-(Nsarray *) componentsseparatedbystring: (NSString *) Separator: Splits a character in a string and returns an array object;
NSString *str =@"abc,def,g";
To replace a specific string in a string with a specified string
NSString*after = [str stringbyreplacingoccurrencesofstring:@"a"Withstring:@"zzzzzzz"]; NSLog (@"%@", after);
Replace the string in range with the appropriate string Nsrange range= {0,6}; NSString*rangestring = [str stringbyreplacingcharactersinrange:range withstring:@"a"]; NSLog (@"%@", rangestring);
Splits the string and stores the separated string in the array Nsarray*split = [str componentsseparatedbystring:@","]; for(inti =0; i < [split Count]; i++) {NSLog (@"%@", [split objectatindex:i]); }
iOS learning the third day of essays