Optionals in designing Swift API -
i'm creating new api use in app. swift api guidelines have no mention of optionals.
i have structured api accept data in 1 format , return in format use in creation. i.e.
func createmydatafromyourdata(data: yourdata?) -> mydata? {}
the function checks see if data nil, , proceeds appropriately. after writing occurred me api should deal non-optionals , user should checking nil before passing. have decided on rule yet?
there's not "rule" this. it's dependent on how app work. in case, you're trying create mydata object, suggest instead do:
class mydata { convenience init?(data: yourdata?) { guard let data = data else { return nil } self.init() // whatever data } }
that way it's obvious you're creating new object when call so:
let obj = mydata(data: yourdataobject)
obj nullable object itself. it's possible still create mydata object if yourdata nil, , in case you'd not make nullable.