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

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

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

iOSとは、Apple製のスマートフォンであるiPhoneやタブレット端末のiPadに搭載しているオペレーションシステム(OS)です。その他にもiPod touch・Apple TVにも搭載されています。

Xcode

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

Swift

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

Q&A

解決済

1回答

1274閲覧

永続保存について

blakekei

総合スコア35

iOS

iOSとは、Apple製のスマートフォンであるiPhoneやタブレット端末のiPadに搭載しているオペレーションシステム(OS)です。その他にもiPod touch・Apple TVにも搭載されています。

Xcode

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

Swift

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

0グッド

0クリップ

投稿2017/02/06 03:44

編集2017/02/06 06:26

永続的にデータを保存したいのですがエラーが起きしまいどこが間違っているのか分からないのです
coredataのENTITYSでEntityとEntity1を作りviewcontroller.swiftに書き加えたのですが多分どこかの入力ミスでエラーが起きたのだと思います。
やりたいこととしては入力したデータをアプリを閉じても消えないようにしたいです。

swift

1//viewcontroller.swift 2import CoreData 3import UIKit 4 5class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { 6 7 8 @IBOutlet weak var tableView: UITableView! 9 //coreデータのEntity 10 var sectionTitleArray = [Entity]() 11 var dataArrayGroup: [[Entity1]] = [] 12 13 override func viewDidLoad() { 14 super.viewDidLoad() 15 16 tableView.estimatedRowHeight = 44 17 tableView.rowHeight = UITableViewAutomaticDimension 18 tableView.tableFooterView = UIView() 19 } 20 21 22 @IBAction func addSection(_ sender: UIButton) { 23 24 let alert = UIAlertController(title:"Sction追加", 25 message: "SectionTitleを入力してください", 26 preferredStyle: .alert) 27 28 let cancelAction = UIAlertAction(title: "Cancel", 29 style: .cancel, 30 handler: 31 { action -> Void in 32 print("Cancel") 33 }) 34 35 let defaultAction = UIAlertAction(title: "OK", 36 style: .default, 37 handler: 38 { action -> Void in 39 40 // TextFieldから値を取得 41 if let textFields = alert.textFields { 42 for textField in textFields { 43 44 if let text = textField.text, !text.isEmpty { 45 46 // 取得したテキストをセクションのタイトルとして追加する 47 print(text) 48 49 //ここでエラーが起きた 50 self.sectionTitleArray[Entity].insert(text, at: 0) 51 self.dataArrayGroup.insert([], at: 0) 52 self.tableView.insertSections(IndexSet(integer: 0), with: .automatic) 53 } 54 } 55 } 56 }) 57 58 alert.addAction(cancelAction) 59 alert.addAction(defaultAction) 60 alert.addTextField(configurationHandler: { text -> Void in }) 61 present(alert, animated: true, completion: nil) 62 } 63 64 @IBAction func deleteSection(_ sender: UIButton) { 65 66 if sectionTitleArray.isEmpty { 67 return 68 } 69 70 let alert = UIAlertController(title:"Sction削除", 71 message: "削除するSectionTitleを入力してください", 72 preferredStyle: .alert) 73 74 let cancelAction = UIAlertAction(title: "Cancel", 75 style: .cancel, 76 handler: 77 { action -> Void in 78 print("Cancel") 79 }) 80 81 let defaultAction = UIAlertAction(title: "OK", 82 style: .default, 83 handler: 84 { action -> Void in 85 86 // TextFieldから値を取得 87 if let textFields = alert.textFields { 88 for textField in textFields { 89 90 if let text = textField.text, 91 !text.isEmpty, 92//ここでもエラー 93 let index = self.sectionTitleArray[Entity].index(of: text) { 94 95 self.sectionTitleArray.remove(at: index) 96 self.dataArrayGroup.remove(at: index) 97 self.tableView.deleteSections(IndexSet(integer: index), with: .automatic) 98 } 99 } 100 } 101 }) 102 103 alert.addAction(cancelAction) 104 alert.addAction(defaultAction) 105 alert.addTextField(configurationHandler: { text -> Void in }) 106 present(alert, animated: true, completion: nil) 107 } 108 109 @IBOutlet weak var deletetext: UITextField! 110 111 // Rowを追加する(アニメーションはご自由に) 112 @IBAction func addRow(_ sender: UIButton) { 113 //func addRow() { 114 115 if dataArrayGroup.count == 0 { 116 return 117 } 118 119 let alert = UIAlertController(title:"Row追加", 120 message: "個数と金額を入力してください", 121 preferredStyle: .alert) 122 123 let cancelAction = UIAlertAction(title: "Cancel", 124 style: .cancel, 125 handler: 126 { action -> Void in 127 print("Cancel") 128 }) 129 130 let count = dataArrayGroup[0].count 131 let defaultAction = UIAlertAction(title: "OK", 132 style: .default, 133 handler: 134 { action -> Void in 135 136 // TextFieldから値を取得 137 if let textFields = alert.textFields { 138 139 let data = Entity1() 140 141 for textField in textFields { 142 143 if let text = textField.text, !text.isEmpty, let _ = Int(text) { 144 if textField.tag == 1 { 145 data.count = text 146 } else { 147 data.price = text 148 } 149 } 150 } 151 152 self.dataArrayGroup[0].insert(data, at: count) 153 self.tableView.insertRows(at: [IndexPath(row: count, section: 0)], with: .automatic) 154 } 155 }) 156 157 158 alert.addTextField { 159 $0.placeholder = "個数" 160 $0.keyboardType = .numberPad 161 $0.tag = 1 162 } 163 alert.addTextField { 164 $0.placeholder = "金額" 165 $0.keyboardType = .numberPad 166 $0.tag = 2 167 } 168 169 alert.addAction(cancelAction) 170 alert.addAction(defaultAction) 171 present(alert, animated: true, completion: nil) 172 } 173 174 175 176 // MARK: - TableView Delegate & DataSource 177 //この部分です。 178 func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? { 179 if sectionTitleArray.count == 0 { 180 return nil 181 } else { 182//ここでもエラーが起きる 183 return sectionTitleArray[Entity] 184 } 185 } 186 187 // Section Count 188 func numberOfSections(in tableView: UITableView) -> Int { 189 return dataArrayGroup.count 190 } 191 192 // Row Count 193 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 194 return dataArrayGroup[section].count 195 } 196 197 // Generate Cell 198 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 199 let cell = tableView.dequeueReusableCell(withIdentifier: "CustomCell", for: indexPath) as! CustomCell 200 cell.data = dataArrayGroup[indexPath.section][indexPath.row] 201 cell.indexLabel.text = String(indexPath.row + 1) 202 return cell 203 } 204 205 // Select Cell 206 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 207 tableView.deselectRow(at: indexPath as IndexPath, animated: true) 208 } 209 210 func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) { 211 if editingStyle == UITableViewCellEditingStyle.delete { 212 dataArrayGroup[indexPath.section].remove(at: indexPath.row) 213 tableView.deleteRows(at: [indexPath], with: UITableViewRowAnimation.automatic) 214 } 215 } 216 217 //cellが削除が可能なことを伝える 218 func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCellEditingStyle { 219 return UITableViewCellEditingStyle.delete; 220 } 221} 222

swift

1//DataModel.swift 2import CoreData 3import Foundation 4 5class Entity1 { 6 7 var count: String = "0" 8 var price: String = "0" 9}

swift

1import CoreData 2import UIKit 3 4class CustomCell: UITableViewCell { 5 6 @IBOutlet weak var indexLabel: UILabel! 7 @IBOutlet weak var countLabel: UILabel! 8 @IBOutlet weak var priceLabel: UILabel! 9 10 var data: Entity1! { 11 didSet { 12 countLabel.text = data.count 13 priceLabel.text = data.price 14 } 15 } 16}

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

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

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

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

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

guest

回答1

0

ベストアンサー

エラーの出ている箇所は、いずれもsectionTitleArray[Entity]となっているようですが、括弧の中には添字(配列の何番目か)を書かなければいけません。(質問のコード内にはEntityの定義が書かれていませんが、クラスの名前ですよね?)

投稿2017/02/06 06:50

fuzzball

総合スコア16731

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問