yodie
yodie

Reputation: 431

NSObjectFromString... Any way to do it?

So Objective-C has these nice functions NSClassFromString() and NSProtocolFromString(), which return a class or protocol from a passed string name. Is there any way to do this with an object?

Upvotes: 3

Views: 324

Answers (3)

Tomen
Tomen

Reputation: 4854

If you want to instantiate an Object, you could also do this

Class class = NSClassFromString(className);
id object = [class new];

Upvotes: 0

John Rudy
John Rudy

Reputation: 37858

The closest thing I can think of is -description, but I suspect you're looking more for introspection into all the members than the basic description. If you are, you're going with Chuck's answer, because you need to serialize/deserialize.

Upvotes: 0

Chuck
Chuck

Reputation: 237110

No, because objects don't have canonical names or string representations. With a class, there either is a class called "NSWindow" or there isn't. With objects, that correspondence doesn't really apply. If you're looking to serialize an object, check out the NSCoding protocol and accompanying documentation.

Upvotes: 5

Related Questions