質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Xcode

Xcodeはソフトウェア開発のための、Appleの統合開発環境です。Mac OSXに付随するかたちで配布されています。

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

Q&A

解決済

1回答

629閲覧

UserDefaultsの値に無い

sakku727

総合スコア25

Xcode

Xcodeはソフトウェア開発のための、Appleの統合開発環境です。Mac OSXに付随するかたちで配布されています。

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

0グッド

0クリップ

投稿2019/12/31 02:11

編集2019/12/31 05:18

前提・実現したいこと

if indexPath.row == 0{
game.set(1, forKey: "mygame")
game.synchronize()
self.dismiss(animated: true, completion: nil)
}
の部分で1を押せばuserdefaultsに1が入るというコードなのですが、userdefaultsにgameという値がないというエラーが出てしまいます。どこがおかしいのかわかりません。どうすればいいのでしょうか。

発生している問題・エラーメッセージ

Value of type 'UserDefaults' has no member 'game'

該当のソースコード

swift

1import UIKit 2 3class game: UIViewController,UITableViewDelegate, UITableViewDataSource { 4 5 var data = ["1", "2", "3", "4", "5", "6"] 6 7 let game:UserDefaults = UserDefaults.standard 8 9 @IBOutlet weak var table: UITableView! 10 11 override func viewDidLoad() { 12 super.viewDidLoad() 13 table.delegate = self 14 table.dataSource = self 15 16 // Do any additional setup after loading the view. 17 } 18 19 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 20 return data.count 21 } 22 23 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 24 // セルの型を作る 25 let cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "myCell") 26 // セルに表示するテキストを作る 27 cell.textLabel?.text = data[indexPath.row] 28 // セルをリターンする 29 return cell 30 } 31 32 //押した数字をUserdefaultsに保存する 33 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath){ 34 if indexPath.row == 0{ 35 game.set(1, forKey: "mygame") 36 game.synchronize() 37 self.dismiss(animated: true, completion: nil) 38 }else if indexPath.row == 1{ 39 game.set(2, forKey: "mygame") 40 game.synchronize() 41 self.dismiss(animated: true, completion: nil) 42 }else if indexPath.row == 2{ 43 game.set(3, forKey: "mygame") 44 game.synchronize() 45 self.dismiss(animated: true, completion: nil) 46 }else if indexPath.row == 3{ 47 game.set(4, forKey: "mygame") 48 game.synchronize() 49 self.dismiss(animated: true, completion: nil) 50 }else if indexPath.row == 4{ 51 game.set(5, forKey: "mygame") 52 game.synchronize() 53 self.dismiss(animated: true, completion: nil) 54 }else if indexPath.row == 5{ 55 game.set(6, forKey: "mygame") 56 game.synchronize() 57 self.dismiss(animated: true, completion: nil) 58 } 59 60 } 61}

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

y_waiwai

2019/12/31 04:08

で、しつもんはなんでしょうか。 また、提示のコードはどういうもんでしょう
sakku727

2019/12/31 04:42

if indexPath.row == 0{ game.set(1, forKey: "mygame") game.synchronize() self.dismiss(animated: true, completion: nil) } の部分で1を押せばuserdefaultsに1が入るというコードなのですが、userdefaultsにgameという値がないというエラーが出てしまいます。 説明が足りなくてすみません。
y_waiwai

2019/12/31 04:44

それを質問に追記しましょう エラーメッセージもそのまま提示してください。 そして、あなたの聞きたいことも質問に追記してください
sakku727

2019/12/31 05:18

追記しました
guest

回答1

0

ベストアンサー

宣言した game がクラス名と同じだからじゃないでしょうか?
クラス名はアッパーキャメルケースでもう少しわかりやすいものに変えたほうが良さそうです。

仕様がわからないですが,もっと綺麗に書けますかね。

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath){ game.set(indexPath.row + 1, forKey: "mygame") self.dismiss(animated: true, completion: nil) }

投稿2020/01/01 12:29

TakuyaAso

総合スコア1361

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

sakku727

2020/01/02 00:59

ありがとうございます。クラス名を変えると解決しました。 綺麗なコードを心がけます。
TakuyaAso

2020/01/02 12:16

良かったです!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問