oc--foundation-Common Classes (5)--nsobject and reflection, copy syntax

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.