IOS review notes 15: NSObject
To facilitate the description, paste the following code:
@interface Student:NSObject-(void)go;-(void)showName:(NSString *)name;-(void)introduce:(NSString *)name :(NSString*)address;@endStudent* stu = [[Student alloc]init];
1. Creation and initialization Methods
Alloc
AllocWithZone
New
Copy-only the copy that cannot be changed to immutable is a shortest copy (equivalent to retain). The others are all deep copies that generate immutable copies (NSString, NSSet, etc)
MutableCopy-deep copy to generate variable copies (NSMutablString, NSMutablSet, etc)
Init
Focus on copy and mutableCopy
Copy: only the files that cannot be changed to immutable are shortest copies (equivalent to retain). Others are deep copies, which generate immutable copies (NSString, NSSet, etc)
MutableCopy: Deep copy to generate variable copies (NSMutablString, NSMutablSet, etc)
Not all objects support copy and mutableCopy;
Classes that comply with the NSCopying/NSMutableCopying protocol can send copy/mutableCopy messages.
The two Protocols respectively declare the following two methods:
- (id)copyWithZone:(NSZone *)zone;- (id)mutableCopyWithZone:(NSZone *)zone;
Example:
@ Interface Student: NSObject
@ End @ interface Student (id) copyWithZone :( NSZone *) zone {// The created copy object does not need to be released, and some callers release it // [self class] to prevent inheritance, returns the parent class Object Student * copy = [[self class] allocWithZone: zone] init]; return copy ;}@ end
Second, the memory management method retain adds the counter retainCount of the object, returns the current counter release of an object, reduces the counter autorelease of the object, and automatically reduces the counter of the object, but is implemented in a delayed manner. Dealloc is applied to the class to release the object instance variables and release the dynamic memory. Method 3: isKindOfClass: checks whether the object is an isMemberOfClass of a class or a subclass object: determines whether the object is a Class Object comfirmsToProtocal: determine whether the object has implemented a certain protocol instancesRespondToSelector: Determine whether the object has implemented a certain method respondsToSelector: Determine whether the class has implemented a certain method. 4. Indirectly call javasmselector: withObjct: optional mselector: withObjct: afterDelay:// Equivalent to [stu go]; [stu worker mselector: @ selector (go)]; // equivalent to [stu showName: @ "Jun"]; [stu worker mselector: @ selector (showName :) withObjct: @ "Jun"]; equivalent to [stu introduce: @ "Jun": @ "address"]; [stu extends mselector: @ selector (introduce: :) withObjct: @ "Jun" withObjct: @ "address"]; // call [stu wait mselector: @ selector (showName :) withObjct: @ "Jun" afterDelay: 2];
5. Description
NSObject has a description method.
+ (NSString *) description;
When NSLog (@ "% @", instance); is called, the description method is called.
You can override this method when customizing a class.
Six reflection
Class and method based on string reflection
1 Class reflection
// Name of the string Class to the 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 );
NSObject nature
@interface NSObject
{ Class isa;}
We can see that the NSObject object only has one member variable Class isa;
What is Class?
typedef struct objc_class *Class;
The alias of the objc_class pointer.
The definition of objc_class is as follows:
struct objc_class { Class isa;};
Similar to the NSObject definition, there are also IDs.
Typedef struct objc_object {
Class isa;
} * Id;
Therefore, id is the objc_object pointer of the struct.
We can see from the above that NSObject, objc_class, and objc_object all have only one objc_class * type, that is, the Class type variable isa.
The class is represented by the objc_class structure, the object is represented by the objc_object structure, and the isa of the object is used to identify the instance of the class of the object.
See 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 proves from the source code that isa represents the type of an object.