gpt4 book ai didi

iOS swift : Downcasting AnyObject

转载 作者:行者123 更新时间:2023-11-29 02:44:05 26 4
gpt4 key购买 nike

我知道有人问了很多从 Cocoa 到 Swift 的向下转换问题,并且有一些错误,但是我已经尝试了很多在这里找到的方法,但我无法让它们工作,希望有人能伸出援手。

我是编程新手。我正在快速为 iOS 制作一个数据库程序。

我有一个人类:

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") as String
self.lastName = aDecoder.decodeObjectForKey("lasName") as String
}

}

我在 View Controller 的顶部声明了一个类数组:

    var peopleArray = [Person]()

然后我通过声明一些示例用户来填充数组并将其附加到数组:

    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)

然后我尝试将数据保存到 plist 文件中:

let fileManager = (NSFileManager.defaultManager())
let directorys : [String]? = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory,NSSearchPathDomainMask.AllDomainsMask, true) as? [String]

println("value of directorys is \(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 to path")
cocoaArray.writeToFile(plistpath, atomically: true)
let tellMe = cocoaArray.writeToFile(plistpath, atomically: true)
println("Return of write is \(tellMe)")
}

创建了一个包含难以理解的数据的 Plist 文件。

我关闭应用程序并重新启动它,然后尝试加载文件:

         else {            
println("\n\nPlist file found at \(plistpath)")

let cocoaArray = NSMutableArray.arrayWithContentsOfFile(plistpath)
peopleArray = cocoaArray as Array
}
}

我失败了,因为我无法向下转换“AnyObject 与‘Person’不相同。我已经尝试通过在 StackOverflow 上列出的几种方式向下转换它,但无法成功。这真的很令人沮丧。

更新最终代码:

func createPeoplePlist() {

let fileManager = (NSFileManager.defaultManager())
let directorys : [String]? = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory,NSSearchPathDomainMask.AllDomainsMask, true) as? [String]

println("value of directorys is \(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 to Plist")

[NSKeyedArchiver.archiveRootObject(peopleArray, toFile: plistpath)]

println("writing to path \(plistpath)")


} else { //Reading Plist file
println("\n\nPlist file found at \(plistpath)")

peopleArray = NSKeyedUnarchiver.unarchiveObjectWithFile(plistpath) as [Person]



}
}


}

最佳答案

当您读回数组时,您的数组仍然包含 NSData(您首先在归档时创建的数据)。但是,我会质疑为什么在写入磁盘之前甚至将数据放在另一个数组中。 peopleArray 本身可以是根对象;不需要额外的数组来包装它。然后你可以简单地使用 archiveRootObject(_:toFile:)unarchiveObjectWithFile(_:)

我还建议阅读 Archives and Serializations Programming GuideProperty List Programming Guide , 和 NSKeyedArchiver文档,以获取更多信息。

关于iOS swift : Downcasting AnyObject,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25356580/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com