javascript - JavaScriptCore Won't Pass string as NSObject * -
consider following jsexported objective-c class nsobject * property:
@protocol myobjectexport <jsexport> @property (nonatomic, strong) nsobject *myproperty; @end @interface myobject : nsobject <myobjectexport> @end @implementation myobject - (nsobject *)myproperty { nslog(@"in myproperty"); return nullptr; } - (void)setmyproperty:(nsobject *)myproperty { nslog(@"in setmyproperty"); } @end if execute code:
{ jscontext *context = [[jscontext alloc] init]; context[@"myobject"] = [[myobject alloc] init]; [context evaluatescript:@"var foo = myobject.myproperty; myobject.myproperty = foo;"]; } both myproperty , setmyproperty called.
but if execute code:
{ jscontext *context = [[jscontext alloc] init]; context[@"myobject"] = [[myobject alloc] init]; [context evaluatescript:@"myobject.myproperty = \"foo\";"]; } setmyproperty not called, presumably because "foo" not compatible nsobject *.
however, if execute code:
{ myobject *myobject = [[myobject alloc] init]; myobject.myproperty = @"foo"; } setmyproperty called.
of course, if replace nsobject * nsstring *, works fine. need property able hold variety of object types.
i don't control passed evaluatescript. however, there way can code things javascriptcore universally convert "foo" nsstring * prior passing objective-c callback? realize can intercept call on javascript side , necessary fiddling there, i'd avoid if possible.
if need hold 1 of many types should use id rather nsobject *. make generic pointer kind of void * in c.
even better use jsvalue * type since -[jsvalue is*] messages can figure out type passed. downside of though jsvalue * properties need backed jsmanagedvalue * ivars prevent retain cycles. see wwdc 2013 video 615, 2/3 of way through more on memory management issue.
Comments
Post a Comment