2016-10-29 14 views
1

私はいくつかの同様の質問を行っていますが、なぜこれが起こるのか分かりません。swift '[Liquor]'の値を期待される引数型 'inout _'に変換できません。

var liquors = [Liquor]() 

func loadSampleLiquors(){ 
    let photo1 = UIImage(named: "Chateau Lafite Rothschild 1993") 
    let liquor1 = Liquor(name: "Chateau Lafite Rothschild", year: "1993", photo: photo1, rating: 7) 
    liquors += [liquor1] // Here is the error happen  
} 

エラーmessageis:期待される引数の型に「[酒]」型の値を変換できません「inoutの_」

「年」はゼロかもしれないが、私は私を通過するので、これはおそらくですそれがうまくいくはずのコード、 "let let liquors = xxx"を使って修正しようとしましたが、デコード関数にEXC_BAD_INSTRUCTIONがあるので、ここにすべてのコードを投稿します:

ここは私の酒ですクラス:

var name: String 
var year: String 
var photo: UIImage? 
var rating: Int 

struct PropertyKey { 
    static let nameKey = "name" 
    static let yearKey = "year" 
    static let photoKey = "photo" 
    static let ratingKey = "rating" 
} 

私は、データを格納するためにNSCodingを使用します。

func encode(with aCoder: NSCoder) { 
    aCoder.encode(name, forKey: PropertyKey.nameKey) 
    aCoder.encode(year, forKey: PropertyKey.yearKey) 
    aCoder.encode(photo, forKey: PropertyKey.photoKey) 
    aCoder.encode(rating, forKey: PropertyKey.ratingKey) 
} 

required convenience init?(coder aDecoder: NSCoder) { 
    let name = aDecoder.decodeObject(forKey: PropertyKey.nameKey) as! String 
    let year = aDecoder.decodeObject(forKey: PropertyKey.yearKey) as! String 
    let photo = aDecoder.decodeObject(forKey: PropertyKey.photoKey) as? UIImage 
    let rating = aDecoder.decodeInteger(forKey: PropertyKey.ratingKey) 
    self.init(name: name, year:year, photo: photo, rating: rating) 
} 
+0

「酒類」の宣言はどこですか? – Alexander

+0

@AlexanderMomchliov私はちょうどそれを追加します – whatever123

+0

私は 'liquors'ではなく' liquor'の宣言を求めました – Alexander

答えて

2

あなたはアンラップ演算子を逃しています。これを試してください:

let liquor1 = Liquor(name: "Chateau Lafite Rothschild", year: "1993", photo: photo1, rating: 7)! 

通知 "!"最終的に

Liquorのinitはnilを返すことができるので、オプションの値を返します。それは ?初期の終わりに

required convenience init? 
関連する問題