Objective-C block to Swift Closure translating Estimote "Examples" app -
i'm translating estimote "examples" ios app objective-c swift , have run problem translating following:
@property (nonatomic, copy) void (^completion)(clbeacon *); - (id)initwithscantype:(estscantype)scantype completion:(void (^)(id))completion { self = [super init]; if (self) { self.scantype = scantype; self.completion = [completion copy]; } return self; } demoviewcontroller = [[estbeacontablevc alloc] initwithscantype:estscantypebeacon completion:^(clbeacon *beacon) { estdistancedemovc *distancedemovc = [[estdistancedemovc alloc] initwithbeacon:beacon]; [self.navigationcontroller pushviewcontroller:distancedemovc animated:yes]; }];
how can translated swift? i've tried many solutions other posts , documentation, still haven't gotten right syntax.
first, initializers called init
; not have func
. , call superclass initializer, super.init()
, without needing check return nil
. also, class's own properties should initialized before calling superclass initializer:
init(scantype: estscantype completion: (clbeacon? -> void)?) { self.scantype = scantype self.completion = completion super.init() }
then, create object , call initializer calling name of class. can use trailing closure expression if last argument of call function type. , should use optional chaining instead of forcing optional, preserve same behavior objective-c doesn't crash when called on nil
.
demoviewcontroller = estbeacontablevc(scantype:estscantypebeacon) { beacon in let distancedemovc = estdistancedemovc(beacon:beacon) self.navigationcontroller?.pushviewcontroller(distancedemovc animated:true) }
Comments
Post a Comment