swift - NSCoding for a non-compliant object (MKMapItem) -
trying save mkmapitem part of custom class.
import uikit import mapkit class place: nsobject, nscoding { var mapitem : mkmapitem! var type : category! init(mapitem: mkmapitem, type: category) { self.mapitem = mapitem self.type = type } // mark: nscoding required init?(coder decoder: nscoder) { mapitem = decoder.decodeobject(forkey: "mapitem") as! mkmapitem? type = decoder.decodeobject(forkey: "type") as! category? } func encode(with coder: nscoder) { coder.encode(mapitem, forkey: "mapitem") coder.encode(type, forkey: "type") } }
but won't work because mkmapitem not nscoding-compliant (compiler doesn't complain though). understand how encode custom classes, can't figure out how object defined ios.
i know there 1 answer out there objective-c on this, swift solution. thanks.
ps have tried subclass mkmapitem , provide "new" initializers, though take considerable code changes elsewhere. leads "cannot assign property: 'placemark' get-only property". both 'placemark' , 'iscurrentlocation' get-only.
import uikit import mapkit class newmapitem: mkmapitem { required init(placemark: mkplacemark, iscurrentlocation:bool, name: string, phonenumber: string, url: url, timezone: timezone) { self.placemark = placemark //compiler complains "get-only" self.iscurrentlocation = iscurrentlocation //compiler complains "get-only" self.name = name self.phonenumber = phonenumber self.url = url self.timezone = timezone } // mark: nscoding init?(coder decoder: nscoder) { placemark = (decoder.decodeobject(forkey: "placemark") as! mkplacemark?)! //compiler complains "get-only" iscurrentlocation = decoder.decodebool(forkey: "iscurrentlocation") //compiler complains "get-only" name = decoder.decodeobject(forkey: "name") as? string phonenumber = decoder.decodeobject(forkey: "phonenumber") as? string url = decoder.decodeobject(forkey: "url") as! url? timezone = decoder.decodeobject(forkey: "timezone") as! timezone? } func encode(with coder: nscoder) { coder.encode(placemark, forkey: "placemark") coder.encode(iscurrentlocation, forkey: "iscurrentlocation") coder.encode(name, forkey: "name") coder.encode(phonenumber, forkey: "phonenumber") coder.encode(url, forkey: "url") coder.encode(timezone, forkey: "timezone") } }
i have figured out.
break down mapitem in "func encode(with coder: nscoder)" , build again in "init?(coder decoder: nscoder)"
i did take advantage of mkmapitem initializer placemark.
mkmapitem(placemark: placemark!)
here working class:
import uikit import mapkit class place: nsobject, nscoding { var mapitem : mkmapitem! var type : category! init(mapitem: mkmapitem, type: category) { self.mapitem = mapitem self.type = type } // mark: nscoding required init?(coder decoder: nscoder) { //liberate properties of mapitem , rebuild // let iscurrentlocation = decoder.decodeobject(forkey: "iscurrentlocation") as! bool //don't need set let name = decoder.decodeobject(forkey: "name") as! string? let phonenumber = decoder.decodeobject(forkey: "phonenumber") as! string? let url = decoder.decodeobject(forkey: "url") as! url? let timezone = decoder.decodeobject(forkey: "timezone") as! timezone? let placemark = decoder.decodeobject(forkey: "placemark") as! mkplacemark? self.mapitem = mkmapitem(placemark: placemark!) self.mapitem.name = name self.mapitem.url = url self.mapitem.phonenumber = phonenumber //self.mapitem.iscurrentlocation = iscurrentlocation //don't need this. set self.mapitem.timezone = timezone type = decoder.decodeobject(forkey: "type") as! category? } func encode(with coder: nscoder) { let placemark = mapitem.placemark let name = mapitem.name let phonenumber = mapitem.phonenumber let url = mapitem.url let timezone = mapitem.timezone coder.encode(name, forkey: "name") coder.encode(phonenumber,forkey: "phonenumber") coder.encode(url, forkey: "url") coder.encode(timezone, forkey: "timezone") coder.encode(type, forkey: "type") coder.encode(placemark, forkey: "placemark") } }
Comments
Post a Comment