iOS Swift: Downcasting AnyObject -
i know number of downcasting cocoa swift questions have been asked , there bugs, have tried lot of methods found here , cannot them work, hoping can lend hand.
i new programming. making database program ios in swift.
i have person class:
class person : nsobject { var firstname : string var lastname : string init (firstname : string, lastname : string) { self.firstname = firstname self.lastname = lastname } func encodewithcoder(acoder: nscoder!) { acoder.encodeobject(firstname, forkey:"firstname") acoder.encodeobject(lastname, forkey:"lastname") } init (coder adecoder: nscoder!) { self.firstname = adecoder.decodeobjectforkey("firstname") string self.lastname = adecoder.decodeobjectforkey("lasname") string } }
i declare array of class @ top of view controller:
var peoplearray = [person]()
i fill array declaring sample users , append array:
var nateb = person(firstname: "nate", lastname: "birkholz") var natec = person(firstname: "nate", lastname: "carson") var nated = person(firstname: "nate", lastname: "donnelly") self.peoplearray.append(nateb) self.peoplearray.append(natec) self.peoplearray.append(nated)
i try save data plist file:
let filemanager = (nsfilemanager.defaultmanager()) let directorys : [string]? = nssearchpathfordirectoriesindomains(nssearchpathdirectory.documentdirectory,nssearchpathdomainmask.alldomainsmask, true) as? [string] println("value of directorys \(directorys)") if (directorys != nil){ let directories:[string] = directorys!; let pathtofile = directories[0]; //documents directory let plistfile = "peoplearray.plist" let plistpath = pathtofile.stringbyappendingpathcomponent(plistfile); if !filemanager.fileexistsatpath(plistpath){ //writing plist file self.createinitialpeople() println("declaring cocoaarray") var cocoaarray : nsarray = [nskeyedarchiver.archiveddatawithrootobject(peoplearray)] println("writing path") cocoaarray.writetofile(plistpath, atomically: true) let tellme = cocoaarray.writetofile(plistpath, atomically: true) println("return of write \(tellme)") }
a plist file inscrutable data created.
i close app , start again, try load file:
else { println("\n\nplist file found @ \(plistpath)") let cocoaarray = nsmutablearray.arraywithcontentsoffile(plistpath) peoplearray = cocoaarray array } }
and fail because cannot downcast "anyobject not identical 'person'. have tried downcasting in several ways have found listed here on stackoverflow , cannot successfully. frustrating.
update final code:
func createpeopleplist() { let filemanager = (nsfilemanager.defaultmanager()) let directorys : [string]? = nssearchpathfordirectoriesindomains(nssearchpathdirectory.documentdirectory,nssearchpathdomainmask.alldomainsmask, true) as? [string] println("value of directorys \(directorys)") if (directorys != nil){ let directories:[string] = directorys!; let pathtofile = directories[0]; //documents directory let plistfile = "peoplearray.plist" plistpath = pathtofile.stringbyappendingpathcomponent(plistfile); if !filemanager.fileexistsatpath(plistpath){ //writing plist file self.createinitialpeople() println("saving plist") [nskeyedarchiver.archiverootobject(peoplearray, tofile: plistpath)] println("writing path \(plistpath)") } else { //reading plist file println("\n\nplist file found @ \(plistpath)") peoplearray = nskeyedunarchiver.unarchiveobjectwithfile(plistpath) [person] } } }
your array still contains nsdata
when read in (the data created when archiving in first place). however, question why putting data in array before writing disk. peoplearray
can root object; there no need array wrapping it. can use archiverootobject(_:tofile:)
, unarchiveobjectwithfile(_:)
.
i recommend reading archives , serializations programming guide , property list programming guide, , nskeyedarchiver docs, further information.
Comments
Post a Comment