Is it conventional for Swift UIViewController implementations to use default property values, or to initialize properties in viewDidLoad? -
in swift implementation of uiviewcontroller, have opportunity use default property values instead of having conduct uiviewcontroller initializer ceremony. example:
class viewcontroller: uiviewcontroller { let model = somemodelobject() what impacts of approach, rather instantiating model property in, say, viewdidload? example:
class viewcontroller: uiviewcontroller { var model: somemodelobject? override func viewdidload() { super.viewdidload() model = somemodelobject() if object being assigned property instantiated in known manner, wouldn't setting default property value make more sense rather making optional , assigning value within viewdidload? in oc implementation, we'd typically implement init, , use if ([super init]) idiom conditionally initialize controller properties. however, seems of sample code apple, @ time of writing, doesn't follow idiom in swift default property values.
it's still what's conventional in swift. we're still making up. , apple's sample code notoriously bad guide (the programming guides have been better thing follow). reading devforums better way see swift team thinks swift (but styles changing language changes).
assigning values in declaration going best approach in many cases, long can it. things aren't available until viewdidload, can't initialize earlier.
in objc, not great put things in init, since init not called. wind having put things in both init , initwithcoder: (which means need hoist initialization method "setup"). there's bit of song-and-dance around how init integrates initwithnibname:bundle: can bite since that's designated initializer (not init). have careful.
all leads objc code put initialization in viewdidload, since it's more reliable (at least since ios 6 when got rid of viewdidunload).
the point of isn't simple question in objc. in swift, i'd better place put model things in default initializers, say, , rest in viewdidload in objc. we're still figuring out.
Comments
Post a Comment