To describe the convenience, paste the following code:
@interface student:nsobject-(void) go;-(void) ShowName: (NSString *) name;-(void) Introduce: (NSString *) name:(NSString *) address, @endStudent * stu = [[Student alloc]init];
A method of creating and initializing
Alloc
Allocwithzone
New
Copy--only immutable to immutable is a shallow copy (equivalent to retain), others are deep copies, resulting in immutable copies (NSString, Nsset, etc.)
Mutablecopy-deep copy, resulting in a variable copy (nsmutablstring, Nsmutablset, etc.)
Init
Focus on copy and Mutablecopy
Copy: Only immutable to immutable is a shallow copy (equivalent to retain), others are deep copies, resulting in immutable copies (NSString, Nsset, etc.)
Mutablecopy: Deep copy, producing a variable copy (nsmutablstring, Nsmutablset, etc.)
Not all objects support copy,mutablecopy;
Classes that adhere to the Nscopying/nsmutablecopying protocol can send copy/mutablecopy messages.
The following two methods are declared in each of the two protocols:
-(ID) Copywithzone: (Nszone *) zone;-(ID) Mutablecopywithzone: (Nszone *) zone;
Example:
<pre name= "code" class= "OBJC" > @interface student:nsobject <NSCopying> @end @interface Student (ID) Copywithzone: (Nszone *) zone{//the replica object created does not need to be freed, there is a caller free//[self class] to prevent inheritance, return the parent class object student* copy = [[Self class] Allocwithzone:zone] Init];return copy;} @end
Two memory management method retain increments the object's counter Retaincount returns an object with the current counter release to reduce the object's counter autorelease automatically reduces the object's counter, but is implemented in a deferred manner. Dealloc apply to classes to release object instance variables and release dynamic memory three ways to determine Iskindofclass: Determine whether an object is an object of a class or subclass Ismemberofclass: Determines whether an object is an object of a class comfirmstoprotocal: Determines whether an object implements a protocol Instancesrespondtoselector: Determines whether an object implements a method Respondstoselector: Determines whether a class implements a method four indirect call Performselector: PerformSelector:withObjct:performSelector:withObjct:withObjct:performSelector:withObjct:afterDelay:
Equivalent to [Stu go]; [Stu Performselector: @selector (GO)];//equivalent to [Stu showname:@ "June"]; [Stu Performselector: @selector (showname:) withobjct:@ "June"], equivalent to [Stu introduce:@ "June": @ "Address"]; [Stu Performselector: @selector (introduce::) withobjct:@ "June" withobjct:@ "address"];//delay 2 seconds after call [Stu Performselector: @selector (showname:) withobjct:@ "June" afterdelay:2];
Five description
NSObject has a description method
+ (NSString *) description;
When we call NSLog (@ "%@", instance), the description method is called.
When customizing a class, you can override this method.
Six Reflections
Reflection into classes and methods based on strings
1 class Reflection
String class name to class nsstring* className = @ "Student"; Class class = Nsclassfromstring (ClassName); person* p = [[Class Alloc] init];//class to String class name nsstring* strclass = Nsstringfromclass ([person class]);
2 Method Reflection
String method name to method nsstring* Funname = @ "Go"; SEL selector = nsselectorfromstring (funname); [Stu performselector:selector];//method to string method name nsstring* Strfun = nsstringfromselector (selector);
Seven NSObject Essence
@interface nsobject <NSObject> { Class Isa;}
You can see that the NSObject object has only one member variable class ISA;
What is class?
typedef struct OBJC_CLASS *class;
Its struct objc_class the alias of the pointer.
Objc_class is defined as follows:
struct Objc_class { class Isa;};
Similar to the nsobject definition, there are also IDs.
typedef struct OBJC_OBJECT {
Class Isa;
} *id;
So the ID is the struct objc_object pointer.
As can be seen from the above, Nsobject,objc_class,objc_object has only one objc_class * type, which is the variable Isa of class type
To conclude that the class is represented by the OBJC_CLASS structure, the object is represented by the objc_object structure, and the object ISA is used to indicate which class the object is an instance of.
Look at the following source code:
-(BOOL) Ismemberofclass: (Class) CLS { return [self Class] = = CLS;} -(Class) class { return Object_getclass (self);} Class object_getclass (id obj) { return _object_getclass (obj);} Static inline Class _object_getclass (id obj) { if (obj) return obj->isa; else return Nil;}
If obj is not empty, the returned class type is Obj->isa, otherwise, nil is returned.
This confirms from the source that ISA is the type that represents an object.
iOS Review notes 15:nsobject