views:

59

answers:

2

Does code like this (potentially) violate the iPhone Developer License Agreement?

Class clazz = NSClassFromString(@"WNEntity");
id entity = [clazz entityWithImage:@"Icon.png"];
SEL setPositionSelector = NSSelectorFromString(@"setPosition:");
objc_msgSend(entity, setPositionSelector, CGPointMake(200, 100));

I'm working on code that dynamically allocates classes from XML and calls methods on them via objc_msgSend. It's just very convenient constructing my objects that way but it worries me because i have no idea whether this is ok or violates the License by dynamically executing code or maybe even calling private (?) API functions. They wouldn't be documented if they were private, right?

Can someone shed some light on this? Have you had an App approved or rejected using code similar to the above?

I'm pretty sure that this is ok but i wan't to hear it from someone else! :)

+1  A: 

If the method you're calling is documented, you're not violating the agreement. There's nothing wrong with using objc_msgSend(), because these "reflection" functions are fully documented.

Philippe Leybaert
+1  A: 

You need to use a similar structure in order to support different versions of iOS (or a "universal" app that works on iPhone and iPad) so it should be fine.

One point, though: I'm not sure that you need to directly use objc_msgSend. Could you not use performSelector:withObject:afterDelay: or one of the other, similar methods of NSObject?

Stephen Darlington
objc_msgSend seemed easier because the parameters i need to pass will be mostly float and CGPoint, not objects. Are there any benefits in using performSelector or NSInvocation?
GamingHorror
@GamingHorror My general rule is to stick with higher-level constructs unless there's a good reason to do otherwise. I think passing scalars would be considered a good reason to go directly to `objc_msgSend`.
Stephen Darlington
I did some research and learned that objc_msgSend has issues when you pass floats as arguments or receive structs as return values, so i'm going with NSInvocation to avoid those issues. It's more verbose but indeed cleaner API-wise.
GamingHorror
NSInvocation is fine, but you could also consider wrapping your floats/points in an NSValue and using performSelector:withObject:
JeremyP