ios - Implement a Swift protocol by automatically mapping method calls to keys in a dictionary -
in objective c have been able implement automatic mechanism allow object private nsdictionary property implement simple protocol automatically converting method invocations dictionary 'valueforkey' requests, , passing these dictionary has appropriate key:value.
for example, have object 'abcartist', implement artistprotocol. protocol has method -(nsstring*)artistname; , 'abcartist' object implement method returning [self.privatedictionary valueforkey:artistname];
this process relied on overriding nsobject methods:
- (void)forwardinvocation:(nsinvocation *)invocation; - (nsmethodsignature *)methodsignatureforselector:(sel)selector; - (id)forwardingtargetforselector:(sel)aselector;
i have tried apply same process in swift, unable use compiler error:
'nsinvocation' unavailable in swift: nsinvocation , related apis not available
is there way has found implement protocol automatically querying dictionary value same name protocol method?
the use case in mapping dictionary of json content returned api, if automatic mapping can accomplished, need write protocol, , forward generated json dictionary.
that kind of dynamic handling of arbitrary messages @ runtime can done in objective-c. 1 thing, calls pure swift methods don't go through objective-c dynamic dispatch mechanism. have @objc
method, , forwardinvocation:
logic have implemented in objective-c.
Comments
Post a Comment