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

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

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

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

Swift

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

Q&A

解決済

1回答

2271閲覧

ViewControllerの値をUIViewに値の受け渡しをしたい

bonbonaoba

総合スコア11

Xcode

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

Swift

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

0グッド

0クリップ

投稿2019/08/14 08:22

前提・実現したいこと

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 {

}

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

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

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

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

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

guest

回答1

0

ベストアンサー

Cannot assign value of type 'Int' to type 'String'
Google翻訳
タイプ「Int」の値をタイプ「String」に割り当てることはできません

self.topMainView.toolsCountはString型です。

Swift

1var toolsCount = "" // 空文字を指定しているので、String型

なので、渡す値を下記の様にString型にしてください。

Swift

1self.topMainView.toolsCount = "(todos.Count)"

投稿2019/08/14 08:46

dsuzuki

総合スコア1682

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

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

bonbonaoba

2019/08/14 09:00

dsuzukiさん回答ありがとうございます。 空文字を指定がString型とは知らなかったので勉強になりました。 ご指摘通り self.topMainView.toolsCount = "(todos.Count)" にコードを書いたらbuildが無事できました。 ですので、今UIViewに渡ったstring型をInt型に変更するコードを考えるフェーズに行くことができました。 ありがとうございます。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問