Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
194 views
in Technique[技术] by (71.8m points)

instantiation - how to instantiate an object of class from string in Objective-C?

I've a String who's value is the name of the Class[MyClass] which has to be instantiated, and MyClass has a method called

 -(void)FunctionInClass;

i'm using the method called NSClassFromString to instantiate MyClass.I want to know

1) what does NSClassFromString return?? 
2) what is id? 
3) How to call method -(void)FunctioninClass which is in MyClass using the instance.

how should i proceed , i'm doing it in Objective-C for iPhone app?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

1) what does NSClassFromString return??

It returns a Class, which means you can do [[NSClassFromString(@"MyClass") alloc] init]; see Mac Dev Center Docs for more info.

2) what is id?

See http://www.otierney.net/objective-c.html#idtype

3) How to call method -(void)FunctioninClass which is in MyClass using the instance.

id myclass = [[NSClassFromString(@"MyClass") alloc] init];
[myclass FunctioninClass];

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...