iOS Swift: Adopting Objective C protocol and returning NSArray -
i have delegate class coded in swift. have obj c class has delegate property of swift class. swift delegate class adopts obj c protocol required property of type nsarray*. swift code builds mutable array class var returns [anyobject]. building of mutable array , assignment of swift class var works, function returns nil objc class. when set breakpoint , step through line line works, if let run returns nil every time. have put in log statements verify swift class builds mutable array every time objc code keeps getting nil. thing have gotten work change objc protocol property nsmutablearray seem bypassing problem. how can make work using nsarray?
if make swift code return nsarray! compile error saying swift class not implement required delegate method.
if change swift code return
array<anyobject>
it compiles still has same problem of returning nil objc class.
the objc protocol looks this:
@protocol mysocialdelegate <nsobject> -(nsarray*)availablechannels; @end
the swift delegate class looks this:
class mysocialdelegateswiftclass: nsobject, mysocialdelegate { private var socialchannels = nsmutablearray() func availablechannels() -> [anyobject]! { if ( self.socialchannels.count == 0 ) { let channel = ...getchannel() if ( channel != nil && channel?.guid != nil ) { self.socialchannels.addobject(channel) } if( somecondition ) { let channel = ...getchannel() if ( channel != nil && channel?.guid != nil ) { self.socialchannels.addobject(channel!) } } } // self.socialchannels contain 2 objects return self.socialchannels [anyobject] } }
and objc class calls swift delegate looks this:
viewcontroller *vc = [[uistoryboard storyboardwithname... vc.socialdelegate = instanceofmysocialdelegateswiftclass; if([vc.socialdelegate respondstoselector:@selector(availablechannels)]) vc.availablechannels = [vc.socialdelegate availablechannels]; // vc.availablechannels nil
have tried returning nsarray in swift implementation of availablechannels
?
private var socialchannels = nsmutablearray() func availablechannels() -> nsarray? { ... return self.socialchannels // socialchannels nsmutablearray }
Comments
Post a Comment