swift - Variable used before being initialised -
i've written code below , error
(1) variable self.pointonmap used before being initialised.
and
(2) variable self.pointonmap captured closure before being initialised.
i don't see why because have declared self.pointonmap = mkpointannotation() should initialise enough.
class pin: nsmanagedobject { var mapview: mkmapview? var pointonmap: mkpointannotation? @nsmanaged var lat: double @nsmanaged var lon: double @nsmanaged var photos: photo? override init(entity: nsentitydescription, insertintomanagedobjectcontext context: nsmanagedobjectcontext?) { super.init(entity: entity, insertintomanagedobjectcontext: context) self.pointonmap = mkpointannotation() } init(dictionary: [string : anyobject], context: nsmanagedobjectcontext) { let entity = nsentitydescription.entityforname("pin", inmanagedobjectcontext: context)! super.init(entity: entity, insertintomanagedobjectcontext: context) lat = 1.0 lon = 2.0 self.pointonmap = mkpointannotation() println(self.pointonmap) // returns error (1) self.pointonmap!.coordinate = cllocationcoordinate2d(latitude: lat, longitude: lon) // returns error (2) }
i can't reproduce setup, can't test exact example, think should use convenience initializer , self.init this:
class pin: nsmanagedobject { var mapview: mkmapview? var pointonmap: mkpointannotation? @nsmanaged var lat: double @nsmanaged var lon: double @nsmanaged var photos: photo? override init(entity: nsentitydescription, insertintomanagedobjectcontext context: nsmanagedobjectcontext?) { super.init(entity: entity, insertintomanagedobjectcontext: context) self.pointonmap = mkpointannotation() } // replace `init` `convenience init` convenience init(dictionary: [string : anyobject], context: nsmanagedobjectcontext) { let entity = nsentitydescription.entityforname("pin", inmanagedobjectcontext: context)! // call overriding init `self.init(...)` instead of `super.init(...)` self.init(entity: entity, insertintomanagedobjectcontext: context) // now, self.pointonmap has value declared `override init` lat = 1.0 lon = 2.0 self.pointonmap = mkpointannotation() println(self.pointonmap) self.pointonmap!.coordinate = cllocationcoordinate2d(latitude: lat, longitude: lon) } }
have @ "designated , convenience initializers in action" in page, think covers case.
Comments
Post a Comment