LoginSignup
0
0

More than 5 years have passed since last update.

【Swift】Swift のチュートリアルでエラー (Terminating app due to uncaught exception 'NSInvalidUnarchiveOperationException', reason: '*** -[NSKeyedUnarchiver decodeInt64ForKey:]: value for key (rating) is not an integer number')

Posted at

SwiftをはじめようとAppleのチュートリアルに沿ってサンプルアプリを作っていた時に生じたエラーです。

チュートリアル

「FoodTracker」というサンプルアプリを作りながら、SwiftとXcodeについて学んでいくことができます。

エラー

上記チュートリアルを順調に進めていき、最後の最後、「Persist Data」のレッスンで以下のエラーに遭遇しました。

Terminating app due to uncaught exception 'NSInvalidUnarchiveOperationException', reason: '*** -[NSKeyedUnarchiver decodeInt64ForKey:]: value for key (rating) is not an integer number'

このレッスンでは、アプリ内で追加・編集・削除した料理のデータ(料理名・画像・料理の評点)をファイルに保存できるようにするレッスンです。

問題の箇所は、Meal.swiftの以下の箇所です。

Meal.swift

let rating = aDecoder.decodeIntegerForKey(PropertyKey.ratingKey)

上記コードでも問題なくコンパイルされ、アプリも動作していました。
しかし、データ保存が可能になるということで、一度アプリを落として再度立ち上げた時にエラーが生じました。
保存したデータの読み込み時になにやら起こったようです。

解決方法

以下URLの通りです :grinning:

Meal.swift

// let rating = aDecoder.decodeIntegerForKey(PropertyKey.ratingKey)

let rating = aDecoder.decodeObjectForKey(PropertyKey.ratingKey) as! Int

decodeIntegerForKeyではなく、decodeObjectForKeyを使用して、Intにダウンキャストします。

参考

iOSもはじめたばかりですが、頑張っていきます :fist:

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0