Reflection:
OC Reflection mechanism refers to: in the running state, for any class, can know all the properties and methods of the class, for any object, can invoke its arbitrary methods and properties, this dynamic access to information and the dynamic method of invoking the object is the function of the OC reflection mechanism. Class reflection instantiating an object as a string of the class name converts the class name into a string of the sel by instantiating the method as a string
// reflection of the class name NSString *str=class=*person=[[class alloc]init]; // class is converted to a string NSString *name=nsstringfromclass ([person clas]); // reflection of the method NSString *method=@ "Test"; SEL selector=nsselectorfromstring (method); [ Person Performselector:selector]; // To convert a method to a string NSString *selectorname=nsstringfromselector (selector);
Copy and Mutablecopy (the purpose of the copy syntax is to change the copy without affecting the source object)
- An object can create a copy of an object using the Copy or Mutablecopy method
- Copy-needs to implement the Nscopying protocol first, creating immutable replicas (such as NSString, Nsarray, Nsdictionary)
- Mutablecopy-needs to implement the Nsmutablecopying protocol first, creating a mutable copy (such as nsmutablestring, Nsmutablearray, Nsmutabledictionary)
- Deep copy: A copy of the content, the source object and the copy point to a different two objects. The source object reference counter is unchanged, and the copy counter is set to 1 ( essentially to see if a new object is generated )
- Shallow copy: pointer copy, source object and copy point to the same object. Object reference counter +1 is actually equivalent to doing a retain operation ( essentially seeing if the resulting object is the same as the source object )
- Only immutable objects create immutable replicas is shallow copy
Invoking the Copy or Mutablecopy method produces a replica that is independent of the source object, only the methods that the object invokes are examples of copy syntax:
is Ten *str2=[str1 copy]; // copy out of the original object, no new objects produced
mutablecopy Example:
is Ten *str1=[str mutablecopy]; // A new object is created and therefore a deep copy
Copy syntax considerations: If you want to customize copy, you must follow nscopying and implement Copywithzone: Methods if you want to customize Mutablecopy, you must follow nsmutablecopying, and implement Mutablecopywithzone: Method with Copy as an example, it is recommended to replace the direct class name with [self class]
oc--foundation-Common Classes (5)--nsobject and reflection, copy syntax