Monday, 15 March 2010

uistoryboard - Swift - How to override an init function? (Was a category in Objective-C) -



uistoryboard - Swift - How to override an init function? (Was a category in Objective-C) -

i have objective-c category used customise

uistoryboard's '+ (uistoryboard *)storyboardwithname:(nsstring *)name'.

it used determine storyboard file phone call when dealing multiple devices. need alter swift.

it seems need create "swift extension", having issues getting work.

here have:

extension uistoryboard { convenience init(name: string, storyboardbundleornil: nsbundle?) { var storyboard: uistoryboard? var storyboardname: string? // don't adjust name if it's universal storyboard (xcode 6, ios 8) storyboardname = name; if (name.rangeofstring("_universal") == nil) { if (uidevice.currentdevice().userinterfaceidiom == .pad) { storyboardname = storyboardname!+"_ipad" } else { storyboardname = storyboardname!+"_iphone" } } // storyboard = uistoryboard(name: storyboardname!, storyboardbundleornil: storyboardbundleornil!) assert(storyboard != nil, "storyboard %@ not found!") //return storyboard!; self.init(name: storyboardname!, storyboardbundleornil: storyboardbundleornil!) } }

you cannot override swift method in extension. see documentation on extensions reference.

you cannot utilize extensions override existing methods or properties on objective-c types.

you've used wrong signature. right signature

init(name: string, bundle: nsbundle?)

it allow create convenience initializer, , utilize when phone call in own code, framework's implementation gets called external code. seems bug me allow declare extension convenience initializer same name, can't find way phone call original initializer, infinite recursion way you've written it.

swift uistoryboard category init

No comments:

Post a Comment