swift
1import UIKit 2import CoreData 3 4class ViewController: UIViewController, UITableViewDataSource { 5 6 @IBOutlet weak var tableView: UITableView! 7 8 var people = [Person]() 9 10 11 12 override func viewDidLoad() { 13 super.viewDidLoad() 14 15 16 title = "\"The List\"" 17 //tableView.register(UITableViewCell.self, forCellReuseIdentifier: "Cell") 18 } 19 20 func tableView(_ tableVeiw: UITableView, numberOfRowsInSection section: Int) -> Int { 21 return people.count 22 } 23 24 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 25 let cell = tableView.dequeueReusableCell(withIdentifier: "Cell") 26 27 let person = people[indexPath.row] 28 29 cell!.textLabel!.text = person.value(forKey: "name") as? String 30 31 //subtitle 現在日時を取得 32 let nowtime = Person() 33 34 nowtime.time = NSDate() // 現在日時の取得 35 let dateFormatter = DateFormatter() 36 dateFormatter.locale = NSLocale(localeIdentifier: "en_US") as Locale! // ロケールの設定 37 dateFormatter.dateFormat = "yyyy年MM月dd日 HH:mm"//:ss" // 日付フォーマットの設定 38 39 //let nowtime = Kei[indexPath.row] 40 let dateString = dateFormatter.string(from: nowtime.time as! Date) 41 print(dateString) // -> 2014/06/25 02:13:18 42 cell!.detailTextLabel!.text = dateString 43 //cell!.detailTextLabel!.text = person.value(forKey: "dateString") as? String 44 45 return cell! 46 } 47 48 func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) { 49 let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext 50 51 if editingStyle == .delete{ 52 //self.tableView.setEditing(true, animated: true) 53 54 context.delete(people[indexPath.row]) 55 56 (UIApplication.shared.delegate as! AppDelegate).saveContext() 57 58 do { 59 people = try context.fetch(Person.fetchRequest()) 60 }catch let error as NSError { 61 print("Could not save \(error), \(error.userInfo)") 62 } 63 tableView.reloadData() 64 } 65 } 66 67 @IBAction func addName(_ sender: Any) { 68 let alert = UIAlertController(title: "New name", message: "Enter a new name", preferredStyle: .alert) 69 70 let saveAction = UIAlertAction(title: "Save", style: .default) { (action) in 71 let textField = alert.textFields?.first 72 self.saveName(name: textField!.text!) 73 self.tableView.reloadData() 74 } 75 76 let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil) 77 78 alert.addTextField(configurationHandler: nil) 79 alert.addAction(saveAction) 80 alert.addAction(cancelAction) 81 82 present(alert, animated: true, completion: nil) 83 } 84 85 func saveName(name: String){ 86 let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext 87 88 let person = Person(entity: Person.entity(), insertInto: context) 89 90 person.setValue(name, forKey: "name") 91 92 do{ 93 try context.save() 94 people.append(person) 95 //self.data = [NSData()] 96 } catch let error as NSError { 97 print("Could not save \(error), \(error.userInfo)") 98 } 99 } 100 101 override func viewWillAppear(_ animated: Bool) { 102 super.viewWillAppear(animated) 103 104 let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext 105 106 do{ 107 let result = try context.fetch(Person.fetchRequest()) 108 people = result as! [Person] 109 }catch let error as NSError { 110 print("Could not save \(error), \(error.userInfo)") 111 } 112 //tableView.reloadData() 113 } 114 115 override func didReceiveMemoryWarning() { 116 super.didReceiveMemoryWarning() 117 } 118 119} 120
実現したいこと
アラートテキストで入力した最終更新時間の年月日時刻をsubtitleに保存し時間が進んでも変わらないようにしたい
やってみたこと
//subtitle 現在日時を取得
let now = NSDate()
NSDateのコードを以下のように変更し、コード的にはエラーはないのですが実行するとsignal SIGABRTが出てどこが間違っているのかいくら探しても見つからなくて困っています。
//subtitle 現在日時を取得 let nowtime = Person() nowtime.time = NSDate() // 現在日時の取得
エンティティはこのようになっています
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2017/02/26 14:04
2017/02/26 15:15 編集
退会済みユーザー
2017/02/26 15:52
2017/02/26 23:57 編集
退会済みユーザー
2017/02/27 04:01
2017/02/27 04:29
退会済みユーザー
2017/02/27 13:24
2017/02/27 15:24