Idan Moshe
Idan Moshe

Reputation: 1535

Is there any way to build a function from NSString?

Lets say I download some text from webservice contain objective-c code, is there any way to make it selector?

EDIT: I edited my title, what I mean is building a function from downloaded string at runtime.

Upvotes: 2

Views: 110

Answers (3)

Andrei Shender
Andrei Shender

Reputation: 2497

It sounds like you want load additional functionalitiy from webservices? Do you have just functions/selectors names in that text or bodies/implementations also? In latter case you can't run non-compiled code, you should first compile it and then, dependent whether platform enables you to load binaries at the runtime - you can try to run it.

If it's just method names -

NSSelectorFromString()

Upvotes: 0

cream-corn
cream-corn

Reputation: 1840

uhh, what do you mean obj-c code...

Are you downloading a class/code that you need to compile at runtime?

or you already have these methods in your implementation, the service only tells you what methods to call?

If the latter is correct, then you can use this:

SEL aSelector = NSSelectorFromString(@"methodName")

beware that if this method contains parameters you will a string like this:

SEL aSelector = NSSelectorFromString(@"methodNameWithParam:anotherParam:")

NSSelectorFromString ref

If the former is correct, welcome to the Objective-c runtime, It is technically possible but i do not know the apple guidelines will take too kindly to you downloading and patching code at runtime.

hope it helps.

Upvotes: 4

Turix
Turix

Reputation: 4490

You can use the NSSelectorFromString() function to get a SEL (selector) from an NSString object.

Upvotes: 0

Related Questions