###前提・実現したいこと
swift3、xcode8でMaster-Detail Applicationのテンプレートを使い、
元から入れた文字列を表示するだけのアプリを作ろうとしています。
リストの編集機能をコメントでブロックし、
var objectsに文字列を入れて表示させようとしました。
すると、NSDateを入れるところにStringを入れようとしている、と言われ
対処法がわかりません。
元のテンプレから変更したところにはコメントを入れてあります。
お力お貸しください。
###発生している問題・エラーメッセージ
Cannot assign value of type "String" to type "NSDate"
###該当のソースコード
import UIKit class MasterViewController: UITableViewController { var detailViewController: DetailViewController? = nil //文末の()を削除 var objects = ["king","Queen"] override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. //コメントで機能を追加するブロック /* self.navigationItem.leftBarButtonItem = self.editButtonItem let addButton = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(insertNewObject(_:))) self.navigationItem.rightBarButtonItem = addButton if let split = self.splitViewController { let controllers = split.viewControllers self.detailViewController = (controllers[controllers.count-1] as! UINavigationController).topViewController as? DetailViewController } */ } override func viewWillAppear(_ animated: Bool) { self.clearsSelectionOnViewWillAppear = self.splitViewController!.isCollapsed super.viewWillAppear(animated) } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } //コメントで機能をブロック /* func insertNewObject(_ sender: Any) { objects.insert(NSDate(), at: 0) let indexPath = IndexPath(row: 0, section: 0) self.tableView.insertRows(at: [indexPath], with: .automatic) } */ // MARK: - Segues override func prepare(for segue: UIStoryboardSegue, sender: Any?) { if segue.identifier == "showDetail" { if let indexPath = self.tableView.indexPathForSelectedRow { //文末の as! NSDate を削除 //この下の部分で上記のエラーメッセージが出ています。 let object = objects[indexPath.row] let controller = (segue.destination as! UINavigationController).topViewController as! DetailViewController controller.detailItem = object controller.navigationItem.leftBarButtonItem = self.splitViewController?.displayModeButtonItem controller.navigationItem.leftItemsSupplementBackButton = true } } } // MARK: - Table View override func numberOfSections(in tableView: UITableView) -> Int { return 1 } override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return objects.count } override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) //文末の as! NSDate を削除 let object = objects[indexPath.row] //文末の .description を削除 cell.textLabel!.text = object return cell }
###試したこと
let object = objects[indexPath.row] as! String
とし、objectをStringとして宣言しようとしたのですが、
元からStringのものをString宣言しても意味はない、とエラーメッセージが出ました。
###補足情報(言語/FW/ツール等のバージョンなど)
より詳細な情報
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/10/11 09:03
2016/10/11 09:06
2016/10/11 09:07
2016/10/11 09:19
2016/10/11 09:22
2016/10/11 09:29