Cocoa advanced Development Runtime primary basic variables

Source: Internet
Author: User

# # # To do iOS development time is not short, it is time to see what they have written to share the object copy:
id object_copy(id// 对象释放 id object_dispose(id obj)TsetClass *obj = [TsetClassnew];id objTest =object_copy(obj,sizeof(obj));

(LLDB) PO &obj
0x00007fff54a7cb08
(LLDB) PO &objtest
0x00007fff54a7cb00
There is no doubt that the printed memory address is not the same, this method is similar to a deep copy of the pointer

Deep copy and shallow copy
    • Deep copy and shallow copy
      A shallow copy copies the object itself, and the attributes and objects contained in the object do not replicate.
      Deep copy copies all, including object properties and other objects.
      The foundation framework supports replicated classes, which by default are shallow copy
    • Custom copies of objects
      object has the copy attribute, must implement the Nscopying,nsmutablecopying protocol, implement the Copywithzone method and Mutablecopywithzone method of the protocol.
      The difference between deep copy and shallow copy lies in the realization of Copywithzone method
      Object_dispose (objtest);//equivalent to [objtest release]; This method is just the encapsulation of the runtime.
      Execute [objtest release] again to know what errors are reported;

List:testruntime (1034,0x111b66300) malloc: * error for object 0x7ffe1a75b940:pointer being freed is not allocat Ed

Changing the class of an object/getting the object's class
    • Changing the class of an object
TsetClass *obj1 = [TsetClass new]; Class aClass =object_set0x7f8e4bd14480>(lldb) po aClassTsetClass有没有出乎你的想象0.0.
    • Gets the class of the object
TsetClass *obj2 = [TsetClass new];Class aLogClass =object_getClass(obj2);(lldb) po aLogClassTsetClassNSString *className = [NSStringstringWithCString:object_getClassName(obj2)encoding:NSUTF8StringEncoding];NSLog(@"className:%@", className);(lldb) po classNameTsetClass
    • Add a method to a class
BOOLClass_addmethod (Class cls,sel name,imp IMP,Const Char*types) Tsetclass *instance = [[Tsetclassalloc]init]; method Add Class_addmethod ([Tsetclass class],@selector(Ocmethod:), (IMP) cfunction,"[Email protected]:@]);if([Instance Respondstoselector:@selector(Ocmethod:)]) {NSLog(@"Yes, instance respondstoselector: @selector (ocmethod:)"); }Else{NSLog(@"Sorry"); }intA = (int) [Instance ocmethod:@"I am an OC Method,c function implementation"];NSLog(@"a:%d", a);intCfunction (ID  Self, SEL _cmd,NSString*STR) {NSLog(@"%@", str);return 520;//Return a value randomly}
    • Get all methods of a class/all properties

      Get all methods of a class

u_int count;Method* methods= class_copyMethodList([UIViewController class], &count);for0{     SEL name = method_getName(methods[i]);     NSString *strName = [NSString stringWithCString:sel_getName(name)encoding:NSUTF8StringEncoding];//        NSLog(@"%@",strName);}

Get all properties of a class

u_int count;objc_property_t *properties=class_copyPropertyList([TsetClass class], &count);for (int0; i < count ; i++) {    constchar* propertyName =property_getName(properties[i]);        NSString *strName = [NSString stringWithCString:propertyName encoding:NSUTF8StringEncoding];        NSLog(@"%@",strName);    }
    • Gets the value of the global variable (myfloat is a property variable for the class)
float myFloatValue;object_getInstanceVariable(self,"myFloat"(void*)&myFloatValue);NSLog(@"%f", myFloatValue);
    • Set the value of a global variable
float10.00f;unsignedint addr = (unsignedint)&newValue;object_setInstanceVariable(self,"myFloat", *(float**)addr);

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Cocoa advanced Development Runtime primary basic variables

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.