Application of nsobject and reflection mechanisms in the objective-C foundation framework

Source: Internet
Author: User

Common nsobject methods:
All OC objects have these methods. See the Demo code.

Reflection:
Class reflection
Instantiate an object using a string of the Class Name
Class class = nsclassfromstring (@ "student ");
Student * Stu = [[class alloc] init];

Class Name conversion to string:
Class class = [Student Class];
Nsstring * classname = nsstringfromclass (class );

Sel reflection:
Instantiate a method in the form of a method string
Sel selector = nsselectorfromstring (@ "setname :");
[STU performselector withobject: @ "Mike"];

Method to convert to string:
Nsstringfromselector (@ selector (setname :));

The test DEMO code is as follows:

# Import <Foundation/Foundation. h>
# Import "person. H"
# Import "student. H"

Void nsobject (){
Id Stu = [[STUDENT alloc] init] autorelease];
// Iskindofclass determines whether the object belongs to a class or subclass
If ([STU iskindofclass: [person class]) {
Nslog (@ "Stu belongs to person or inherits from person ");
}
// Ismemberofclass determines whether an object belongs to a certain class (excluding subclass)
Bool result = [STU ismemberofclass: [nsobject class];
Nslog (@ "% I", result );

// Directly call
[STU tests];
// Indirect call
[STU extends mselector: @ selector (tests)];
[STU initialize mselector: @ selector (Test2 :) withobject: @ "ABC"];

// Call with a latency of 2 seconds
[STU initialize mselector: @ selector (Test2 :) withobject: @ "123" afterdelay: 2];
}

Void reflect (){
// Change the string to class
Nsstring * STR = @ "student ";
Class class = nsclassfromstring (STR );

Student * Stu = [[class alloc] init];
Nslog (@ "% @", Stu );

// Change the class string
Nsstring * name = nsstringfromclass ([person class]);
Nslog (@ "Name: % @", name );

// Method reflection
Nsstring * method = @ "tests ";
Sel selector = nsselectorfromstring (method );
[STU primary mselector: Selector];

// Convert sel into a string
Nsstring * selectorname = nsstringfromselector (selector );
Nslog (@ "% @", selectorname );

[STU release];

}

Int main (INT argc, const char * argv [])
{

@ Autoreleasepool {
Nslog (@ "---- nsobject ----");
Nsobject ();
Nslog (@ "---- reflect ----");
Reflect ();
}
Return 0;
}
Person. h file:
# Import <Foundation/Foundation. h>
@ Interface person: nsobject
@ End

Person. M file:
# Import "person. H"
@ Implementation person
@ End

Student. h file;

# Import "person. H"
@ Interface Student: person
-(Void) tests;
-(Void) Test2 :( nsstring *) Param;
@ End

Student. M file:

# Import "student. H"
@ Implementation student
-(Void) tests {
Nslog (@ "excute 'test' method ");
}
-(Void) Test2 :( nsstring *) Param {
Nslog (@ "excete 'test2' method and Param is
% @ ", Param );
}
@ End

Related Article

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.