Swift'AnyObject'没有名为“make”的成员。 [英] Swift 'AnyObject' does not have a member named "make"

查看:179
本文介绍了Swift'AnyObject'没有名为“make”的成员。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很困惑,如何我可以有两个键作为字符串和一个工作,而另一个不。错误发生在接近结尾的行:



println(这里是(car.year)(car.make)(car.model))



可能导致问题的make变量是什么?

  protocol NSCoding {

}

class Car:NSObject {

var year:Int = 0
var make:String =
var model:String =

override init(){
super.init()
}

func encodeWithCoder aCoder:NSCoder!){
aCoder.encodeInteger(year,forKey:year)
aCoder.encodeObject(make,forKey:make)
aCoder.encodeObject(model,forKey: model)
}

init(coder aDecoder:NSCoder!){

super.init()

year = aDecoder .decodeIntegerForKey(year)
make = aDecoder.decodeObjectForKey(make)as String
model = aDecoder.decodeObjectForKey(model)as String

}
}

class CarData {


func archive(){
var documentDirectories:NSArray
var documentDirectory:String
var path:String
var unarchivedCars:NSArray
var allCars:NSArray

//创建用于归档的文件路径。
documentDirectories = NSSearchPathForDirectoriesInDomains(.DocumentDirectory,.UserDomainMask,true)

//从该列表中获取文档目录
documentDirectory = documentDirectories.objectAtIndex(0)as String

//用.archive文件名追加
path = documentDirectory.stringByAppendingPathComponent(swift_archiver_demo.archive)

var car1:Car! = Car()
var car2:Car! = Car()
var car3:Car! = Car()

car1.year = 1957
car1.make =Chevrolet
car1.model =Bel Air

car2。 year = 1964
car2.make =Dodge
car2.model =Polara

car3.year = 1972
car3.make =Plymouth
car3.model =Fury

allCars = [car1,car2,car3]

//'archiveRootObject:toFile'返回一个指向
//操作是否成功。我们可以使用它来记录消息。
if NSKeyedArchiver.archiveRootObject(allCars,toFile:path){
println(Success writing to file!)
} else {
println(无法写入文件! )
}

//现在可以取消归档数据并将其放入不同的数组,以验证
//这一切都可以工作。取消归档对象并将它们放入新数组
unarchivedCars = NSKeyedUnarchiver.unarchiveObjectWithFile(path)as NSArray

//输出新数组
for car:AnyObject in unarchivedCars {
println(这里是一个\(car.year)\(car.make)\(car.model))
}
}

}


解决方案

在for循环中使用downcasting。编译器需要知道car是Car类型,而不是AnyObject。

  
println(这里是一个\(car.year)\(car.make)\(car.model))
}
pre>

I am confused on how I can have two keys as strings and one works and the other doesn't. Error occurs in the line near the end:

println("Here's a (car.year) (car.make) (car.model)")

What is it about the "make" variable that could be causing the problem?

protocol NSCoding {

}

class Car:NSObject {

    var year: Int = 0
    var make: String = ""
    var model: String = ""

    override init() {
        super.init()
    }

    func encodeWithCoder(aCoder: NSCoder!) {
        aCoder.encodeInteger(year, forKey:"year")
        aCoder.encodeObject(make, forKey:"make")
        aCoder.encodeObject(model, forKey:"model")
    }

    init(coder aDecoder: NSCoder!) {

        super.init()

        year = aDecoder.decodeIntegerForKey("year")
        make = aDecoder.decodeObjectForKey("make") as String
        model = aDecoder.decodeObjectForKey("model") as String

    }
}

class CarData {


    func archiveData () {
        var documentDirectories:NSArray
        var documentDirectory:String
        var path:String
        var unarchivedCars:NSArray
        var allCars:NSArray

        // Create a filepath for archiving.
        documentDirectories = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)

        // Get document directory from that list
        documentDirectory = documentDirectories.objectAtIndex(0) as String

        // append with the .archive file name
        path = documentDirectory.stringByAppendingPathComponent("swift_archiver_demo.archive")

        var car1:Car! = Car()
        var car2:Car! = Car()
        var car3:Car! = Car()

        car1.year = 1957
        car1.make = "Chevrolet"
        car1.model = "Bel Air"

        car2.year = 1964
        car2.make = "Dodge"
        car2.model = "Polara"

        car3.year = 1972
        car3.make = "Plymouth"
        car3.model = "Fury"

        allCars = [car1, car2, car3]

        // The 'archiveRootObject:toFile' returns a bool indicating
        // whether or not the operation was successful. We can use that to log a message.
        if NSKeyedArchiver.archiveRootObject(allCars, toFile: path) {
            println("Success writing to file!")
        } else {
            println("Unable to write to file!")
        }

        // Now lets unarchive the data and put it into a different array to verify
        // that this all works. Unarchive the objects and put them in a new array
        unarchivedCars = NSKeyedUnarchiver.unarchiveObjectWithFile(path) as NSArray

        // Output the new array
        for car : AnyObject in unarchivedCars {
            println("Here's a \(car.year) \(car.make) \(car.model)")
        }
    }

}

解决方案

Use downcasting in your for loop. The compiler needs to know that car is of type Car and not just AnyObject.

for car in cars as [Car!] {
    println("Here's a \(car.year) \(car.make) \(car.model)")
}

这篇关于Swift'AnyObject'没有名为“make”的成员。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆