2016-12-03 4 views
0

私はなぜそれが起こるのかわからない問題があります。セグを準備することでデータを転送する

Ive created a class called "Apples" on a new Swife and called it Apples.

class Apples { 
    var points = points() 
    var appleName = String? 
} 

constructed a struct called points to define the apple parameters

struct points { 

    var taste = 0 
    var smell = 0 
    var texture = 0 
    var color = 0 


} 

and constructed a function to make new apples

func newApples(t:Int, s: Int, tex: Int, col: Int, appName = String) 
{ 
    let newApple = Apples() 
    newApple.points.taste = t 
    newApple.points.smell = s 
    newApple.points.texture = tex 
    newApple.points.color = col 
newApple.appleName = appName 
} 

and then I got 2 ViewControllers, 1 is AppleViewController, and the 2nd is ChoiceViewController.

I called

let pinkLady = newApples(t: 5, s: 4, tex: 4, col: 5, appName = "Pink Lady") 

on the AppleViewController

On the ChoiceViewController I got some UILabel Outlet(appleLabel) which I want to change according to the Apple the user picked on the 1st AppleViewController

the problem lies when I try to make a Prepare(for segue:) function

override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 
     let choiceVC = segue.destination as! ChoiceViewController 
     choiceVC.appleLabel.text = pinkLady.appleName 
    } 

And in that last line of code lies the problem, When im running the app and click the button with no line of code in (prepare for segue) function it performs the segue just fine, but with it it calls "fatal error: unexpectedly found nil while unwrapping an Optional value"

I tried all sort of syntax such as

choiceVC.appleLabel.text = ((pinkLady.appleName)!) 
choiceVC.appleLabel.text = pinkLady.appleName? 
choiceVC.appleLabel.text = ("\(pinkLady.appleName?)") 

and then I tried to just put

print((pinkLady.appleName)!)

inside the prepare(for segue) function and when it performed it printed out to the console Pink Lady with no problem. i really dont know what im missing here.

+0

あなたの問題は「致命的なエラーです:予期せぬことに、オプション値をアンラッピングしている間に見つかりません」という意味です。 prepareForSegueにブレークポイントを設定し、nilを見つけるまで行単位で移動します。 – Multinerd

+0

あなたのappleLabelがメモリにまだ割り当てられていないので、この問題が起こると思います。 choiseVCでvar srtingを宣言し、次にviewdidload funcでuilabelのテキスト – Asike

+0

に割り当てます。私が言ったように、appleLabel.textをappleLabel.textを pinkLady.appleNameに変更する行を呼び出しますが、同じ値を印刷するとそれはゼロを返しません、それはピンクの女性を返す –

答えて

0

私はあなたのappleLabelがまだメモリ内に割り当てられていなかったため、問題が発生したと思います。 choiseVCでvar文字列を宣言してから、viewdidload funcでuilabelのテキストに割り当てます。

0

まず、静的な値を入力してみてください。

choiceVC.appleLabel.text = "This Is Apple" 

この静的データを使用すると、エラーは発生しません。これは、コードが正しいことを意味します。問題はpinkLady.appleNameが見つかりました。

他の賢明な利用代わりセグエコードのこのコードApplesVCから

let choiceVC = self.storyboard?.instantiateViewController(withIdentifier: "ChoiceViewController") as? ChoiceViewController 
choiceVC.appleLabel.text = (pinkLady.appleName 
self.navigationController?.pushViewController(choiceVC!, animated: true) 
-1
Make an object say pinkLadyInChoice, in ChoiceVC of type Apples. 
and pass pinkLady object to declared above, here pinkLadyInChoice, through prepareForSegue Method. 
Then in ChoiceVC set text of labels by using object pinkLadyInChoice here. 

は、次の書き込みchoiceVCでChoiceVC strPinkLadyオブジェクト

にpinkLadyオブジェクトを渡す: -

let strPinkLady : Apples 
choiceVC.appleLabel.text = strPinkLady.appleName 
0

このコードを試してください...

let strPinkLady : String = pinkLady.appleName? 
choiceVC.appleLabel.text = strPinkLady 
関連する問題