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

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

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

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

Swift 2

Swift 2は、Apple社が独自に開発を行っている言語「Swift」のアップグレード版です。iOSやOS X、さらにLinuxにも対応可能です。また、throws-catchベースのエラーハンドリングが追加されています。

Q&A

解決済

1回答

2705閲覧

swift3.0 構造体のデータの受け渡しでエラーの違いがわからない

udb

総合スコア25

Swift

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

Swift 2

Swift 2は、Apple社が独自に開発を行っている言語「Swift」のアップグレード版です。iOSやOS X、さらにLinuxにも対応可能です。また、throws-catchベースのエラーハンドリングが追加されています。

0グッド

0クリップ

投稿2016/10/04 22:58

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(指定した形式がない)のエラーが出ます。

同じようなコードなのにエラーが出る違いがわかりません。
またエラーの解消方法を色々と試しましたが、解決に至りません。

どなたか良いアドバイスをよろしくお願いします。

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

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

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

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

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

guest

回答1

0

ベストアンサー

データの型が違いますね、以下のようにすることでエラーはなくなると思いますがいかがでしょうか?

B画面のコード

swift

1import UIKit 2 3class ViewController2: UIViewController { 4 5  // myList → dataMap 6 var MapList:dataMap? { 7 didSet { 8 } 9 } 10 11 override func viewDidLoad() { 12 super.viewDidLoad() 13 // Do any additional setup after loading the view, typically from a nib. 14 } 15 16 override func didReceiveMemoryWarning() { 17 super.didReceiveMemoryWarning() 18 // Dispose of any resources that can be recreated. 19 } 20} 21

投稿2016/10/04 23:24

編集2016/10/04 23:31
_Kentarou

総合スコア8490

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問