前提・実現したいこと
ViewControllerで得た値をInt型でUIVewの受け渡しをしたい
ViewControllerからUIViewへInt型で値の受け渡しをしたい機能の実装中に以下のエラーメッセージが発生しました。
発生している問題・エラーメッセージ
Cannot assign value of type 'Int' to type 'String'
該当のソースコード
swift ViewController部分
import UIKit
import PGFramework
import RealmSwift
class TopViewController: BaseViewController {
@IBOutlet weak var topMainView: TopMainView! @IBAction func didTapAddButton(_ sender: UIBarButtonItem) { let alertController: UIAlertController = UIAlertController(title: "todoを追加しますか?", message: nil, preferredStyle: .alert) let actionButton: UIAlertAction = UIAlertAction(title: "追加", style: .default) { (void) in let textField = alertController.textFields![0] as UITextField if let text = textField.text{ print("データベースと取得してタスクを追加する処理をここに書く") let todo = Todo() todo.text = text // Get the default Realm let realm = try! Realm() // Persist your data easily try! realm.write { realm.add(todo) } // Query Realm for all dogs less than 2 years old let todos = realm.objects(Todo.self) print(todos.count)// => 0 because no dogs have been added to the Realm yet let todoCount = todos.count self.topMainView.toolsCount = todoCount } } let cancelButton: UIAlertAction = UIAlertAction(title: "キャンセル", style: .cancel, handler: nil) alertController.addTextField { (textField) in textField.placeholder = "todeの名前を追加してください" } alertController.addAction(actionButton) alertController.addAction(cancelButton) present(alertController, animated: true, completion: nil) }
}
// MARK: - Life cycle
extension TopViewController {
override func viewDidLoad() {
super.viewDidLoad()
print(Realm.Configuration.defaultConfiguration.fileURL!)
}
override func loadView() { super.loadView() }
}
// MARK: - Protocol
extension TopViewController {
}
// MARK: - method
extension TopViewController {
}
試したこと
エラー文の箇所の
let todoCount = todos.count
self.topMainView.toolsCount = todoCount
を
let todoCount: Int = todos.count
や
self.topMainView.toolsCount = Int(todoCount)
などをしてInt型での値の受け渡しを試みた
補足情報(FW/ツールのバージョンなど)
UIVIew部分のコード
import UIKit
import PGFramework
import RealmSwift
protocol TopMainViewDelegate: NSObjectProtocol{
}
extension TopMainViewDelegate {
}
// MARK: - Property
class TopMainView: BaseView {
weak var delegate: TopMainViewDelegate? = nil
@IBOutlet weak var tableView: UITableView!
var toolsCount = ""
}
// MARK: - Life cycle
extension TopMainView {
override func awakeFromNib() {
super.awakeFromNib()
tableView.dataSource = self
tableView.delegate = self
loadTableViewCellFromXib(tableView: tableView, cellName: "TopTableViewCell")
}
}
// MARK: - Protocol
extension TopMainView: UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 10
// return Int(toolsCount)!
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "TopTableViewCell", for: indexPath) return cell }
}
extension TopMainView: UITableViewDelegate{
}
// MARK: - method
extension TopMainView {
}
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/08/14 09:00