A画面からB画面への遷移した際にデータの受け渡しを行う場合に同じようなコードでエラーがでる場合と出ない場合があり困っています。
エラーの形式は「その形式のデータは存在しません」というエラーで、型の指定が間違っているのか?と探っていますが、出てきません。
画面Aのコード
var bookMark:[likeSight]
required init(coder aDecoder:NSCoder){ bookMark = [] bookMark.append(likeSight(name: "Yahoo", sightUrl: "http://www.yahoo.co.jp")) bookMark.append(likeSight(name: "Bing", sightUrl:"http://www.bing.com/?pc=APPT")) bookMark.append(likeSight(name: "Apple Developer", sightUrl: "https://developer.apple.com")) bookMark.append(likeSight(name: "Soft Bank", sightUrl: "http://www.softbank.jp/mobile/")) super.init(coder: aDecoder)!} override func viewDidLoad() { super.viewDidLoad()
}
override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } // MARK: - Table view data source override func numberOfSections(in tableView: UITableView) -> Int { // #warning Incomplete implementation, return the number of sections return 1 } override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { // #warning Incomplete implementation, return the number of rows return bookMark.count } override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "no1", for: indexPath) let Data = bookMark[(indexPath as NSIndexPath).row] as likeSight // タイトルはサイト名 cell.textLabel?.text = Data.name return cell }
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "MoveWeb" { if let selectPath = self.tableView.indexPathForSelectedRow{ let urlData = bookMark[(selectPath as NSIndexPath).row] as likeSight //移動先のViewControllerの変数itemにURLデータをセットする (segue.destination as! ViewController).Item = urlData }}}}
データを受け取る画面Bのコード
import UIKit
class ViewController: UIViewController {
var Item:likeSight? { didSet{ } } @IBOutlet weak var webView: UIWebView! override func viewDidLoad() { super.viewDidLoad() self.configureView() } func configureView(){ //Itemに値があればurlDataに代入する if let urlData:likeSight = self.Item{ //webViewが設定されているなら続ける if let webView = self.webView { //アクセスするURLをNSURLデータに変換してアクセスする let url = URL(string: urlData.sightUrl) let urlReq = URLRequest(url: url!) // リクエストURLからウェブサイトを開く webView.loadRequest(urlReq) }} }
このコードではエラーが出ません。
しかしこのコードではエラーが出ます。
画面Aのコード
import UIKit
struct dataMap {
var Name:String
var place:String
var lat:String
var long:String
}
class TableViewController: UITableViewController {
@IBOutlet var table: UITableView! //構造体を含んだ配列 var myList:[dataMap] //初期化のメソッド required init?(coder aDecoder: NSCoder) { myList = [] myList.append(dataMap(Name: "西公園", place:" 福岡市中央区大濠公園1-2",lat:"33.598083",long:"130.375417")) super.init(coder: aDecoder) } override func viewDidLoad() { super.viewDidLoad() } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } // MARK: - Table view data source override func numberOfSections(in tableView: UITableView) -> Int { // #warning Incomplete implementation, return the number of sections return 1 } override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { // #warning Incomplete implementation, return the number of rows return myList.count } override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) // 表示させるの一覧のデータ let Data = myList[(indexPath as NSIndexPath).row] as dataMap cell.textLabel?.text = Data.Name cell.detailTextLabel?.text = Data.place return cell }
画面Bのコード
import UIKit
**class ViewController: UIViewController **{
**var MapList:myList { didSet {} }** override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. }}
classの部分にno initializers(初期化のメソッドが存在しない)
var ~ の部分にuse ob undeclared type(指定した形式がない)のエラーが出ます。
同じようなコードなのにエラーが出る違いがわかりません。
またエラーの解消方法を色々と試しましたが、解決に至りません。
どなたか良いアドバイスをよろしくお願いします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。