Michael Buerger Michael Buerger - 5 months ago 29
Swift Question

PrepareForSegue works, but accessing destination's variables crashes application

I am trying to set the text component of a label in the method prepareForSegue but the application is crashing when the method is executed, i've looked at other discussions but none of them have helped me, I am programming in swift in the latest version of xCode and building to an iPhone.
When I remove the line that sets the label's text to "Meal name", no crash occurs.
The breakpoint is on the line that sets the label's text.
If anyone can help me that would be great, thank you.

-- Edit --

prepareForSegue
code of FirstViewController:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
let dest : SecondViewController = segue.destinationViewController as! SecondViewController
dest.mealNameLabel.text = "Meal Name"
}


Full stack trace: (-> is break)

libswiftCore.dylib`function signature specialization <Arg[0] = Exploded, Arg[1] = Exploded, Arg[2] = Dead, Arg[3] = Dead> of Swift._fatalErrorMessage (Swift.StaticString, Swift.StaticString, Swift.StaticString, Swift.UInt) -> ():
0x1001de55c <+0>: stp x29, x30, [sp, #-16]!
0x1001de560 <+4>: mov x29, sp
0x1001de564 <+8>: sub sp, sp, #16 ; =16
0x1001de568 <+12>: and w8, w2, #0x1
0x1001de56c <+16>: tbnz w8, #0, 0x1001de58c ; <+48>
0x1001de570 <+20>: tbnz x1, #63, 0x1001de5c8 ; <+108>
0x1001de574 <+24>: add x1, x0, x1
0x1001de578 <+28>: mov x2, x3
0x1001de57c <+32>: mov x3, x4
0x1001de580 <+36>: mov x4, x5
0x1001de584 <+40>: bl 0x1002265b0 ; function signature specialization <Arg[0] = Exploded, Arg[1] = Exploded> of Swift.(_fatalErrorMessage (Swift.StaticString, Swift.StaticString, Swift.StaticString, Swift.UInt) -> ()).(closure #2)

-> 0x1001de588 <+44>: brk #0x1
0x1001de58c <+48>: str xzr, [sp, #8]
0x1001de590 <+52>: cmp x0, w0, uxtw
0x1001de594 <+56>: b.ne 0x1001de6e0 ; <+388>
0x1001de598 <+60>: lsr w8, w0, #11
0x1001de59c <+64>: cmp w8, #26 ; =26
0x1001de5a0 <+68>: b.hi 0x1001de5ec ; <+144>
0x1001de5a4 <+72>: cmp w0, #128 ; =128
0x1001de5a8 <+76>: b.lo 0x1001de630 ; <+212>
0x1001de5ac <+80>: cmp w0, #2048 ; =2048
0x1001de5b0 <+84>: b.hs 0x1001de660 ; <+260>
0x1001de5b4 <+88>: movz x8, #0
0x1001de5b8 <+92>: movz x10, #0
0x1001de5bc <+96>: lsr w9, w0, #6
0x1001de5c0 <+100>: orr w9, w9, #0xffffffc0
0x1001de5c4 <+104>: b 0x1001de694 ; <+312>
0x1001de5c8 <+108>: adr x0, #723338 ; "fatal error"
0x1001de5cc <+112>: nop
0x1001de5d0 <+116>: adr x3, #723840 ; "UnsafeBufferPointer with negative count"
0x1001de5d4 <+120>: nop
0x1001de5d8 <+124>: movz w1, #0xb
0x1001de5dc <+128>: orr w2, wzr, #0x2
0x1001de5e0 <+132>: movz w4, #0x27
0x1001de5e4 <+136>: orr w5, wzr, #0x2
0x1001de5e8 <+140>: bl 0x1001de55c ; <+0>
0x1001de5ec <+144>: cmp w0, #14, lsl #12 ; =57344
0x1001de5f0 <+148>: b.lo 0x1001de63c ; <+224>
0x1001de5f4 <+152>: cmp w0, #272, lsl #12 ; =1114112
0x1001de5f8 <+156>: b.hs 0x1001de738 ; <+476>
0x1001de5fc <+160>: lsr w8, w0, #6
0x1001de600 <+164>: lsr w9, w0, #16
0x1001de604 <+168>: cbz w9, 0x1001de664 ; <+264>
0x1001de608 <+172>: lsr w9, w0, #18
0x1001de60c <+176>: orr w9, w9, #0xf0
0x1001de610 <+180>: cmp w9, w9, uxtb
0x1001de614 <+184>: b.ne 0x1001de6e0 ; <+388>
0x1001de618 <+188>: orr w10, wzr, #0xffffff80
0x1001de61c <+192>: bfxil w10, w0, #12, #6
0x1001de620 <+196>: and x12, x9, #0xff
0x1001de624 <+200>: str x12, [sp, #8]
0x1001de628 <+204>: orr w11, wzr, #0x1
0x1001de62c <+208>: b 0x1001de674 ; <+280>
0x1001de630 <+212>: movz x8, #0
0x1001de634 <+216>: movz x9, #0
0x1001de638 <+220>: b 0x1001de6d0 ; <+372>
0x1001de63c <+224>: adr x0, #723222 ; "fatal error"
0x1001de640 <+228>: nop
0x1001de644 <+232>: adr x3, #726652 ; "high- and low-surrogate code points are not valid Unicode scalar values"
0x1001de648 <+236>: nop
0x1001de64c <+240>: movz w1, #0xb
0x1001de650 <+244>: orr w2, wzr, #0x2
0x1001de654 <+248>: movz w4, #0x47
0x1001de658 <+252>: orr w5, wzr, #0x2
0x1001de65c <+256>: bl 0x1001de55c ; <+0>
0x1001de660 <+260>: lsr w8, w0, #6
0x1001de664 <+264>: lsr w9, w0, #12
0x1001de668 <+268>: movz x11, #0
0x1001de66c <+272>: movz x12, #0
0x1001de670 <+276>: orr w10, w9, #0xffffffe0
0x1001de674 <+280>: lsl x13, x11, #3
0x1001de678 <+284>: orr w9, wzr, #0xffffff80
0x1001de67c <+288>: bfxil w9, w8, #0, #6
0x1001de680 <+292>: and w8, w10, #0xff
0x1001de684 <+296>: lsl x8, x8, x13
0x1001de688 <+300>: orr x10, x8, x12
0x1001de68c <+304>: str x10, [sp, #8]
0x1001de690 <+308>: add x8, x11, #1 ; =1
0x1001de694 <+312>: orr w11, wzr, #0x8
0x1001de698 <+316>: umulh x11, x8, x11
0x1001de69c <+320>: cmp xzr, x11
0x1001de6a0 <+324>: b.ne 0x1001de6e0 ; <+388>
0x1001de6a4 <+328>: lsl x11, x8, #3
0x1001de6a8 <+332>: cmp x11, #63 ; =63
0x1001de6ac <+336>: b.hi 0x1001de714 ; <+440>
0x1001de6b0 <+340>: orr w12, wzr, #0x80
0x1001de6b4 <+344>: bfxil x12, x0, #0, #6
0x1001de6b8 <+348>: and w9, w9, #0xff
0x1001de6bc <+352>: lsl x9, x9, x11
0x1001de6c0 <+356>: orr x9, x9, x10
0x1001de6c4 <+360>: str x9, [sp, #8]
0x1001de6c8 <+364>: add x8, x8, #1 ; =1
0x1001de6cc <+368>: mov x0, x12
0x1001de6d0 <+372>: orr w10, wzr, #0x8
0x1001de6d4 <+376>: umulh x10, x8, x10
0x1001de6d8 <+380>: cmp xzr, x10
0x1001de6dc <+384>: b.eq 0x1001de6e4 ; <+392>
0x1001de6e0 <+388>: brk #0x1
0x1001de6e4 <+392>: lsl x10, x8, #3
0x1001de6e8 <+396>: cmp x10, #64 ; =64
0x1001de6ec <+400>: b.hs 0x1001de714 ; <+440>
0x1001de6f0 <+404>: and x11, x0, #0xff
0x1001de6f4 <+408>: lsl x10, x11, x10
0x1001de6f8 <+412>: orr x9, x10, x9
0x1001de6fc <+416>: str x9, [sp, #8]
0x1001de700 <+420>: add x9, sp, #8 ; =8
0x1001de704 <+424>: add x8, x8, x9
0x1001de708 <+428>: add x1, x8, #1 ; =1
0x1001de70c <+432>: add x0, sp, #8 ; =8
0x1001de710 <+436>: b 0x1001de578 ; <+28>
0x1001de714 <+440>: adr x0, #723006 ; "fatal error"
0x1001de718 <+444>: nop
0x1001de71c <+448>: adr x3, #723412 ; "shift amount is larger than type size in bits"
0x1001de720 <+452>: nop
0x1001de724 <+456>: movz w1, #0xb
0x1001de728 <+460>: orr w2, wzr, #0x2
0x1001de72c <+464>: movz w4, #0x2d
0x1001de730 <+468>: orr w5, wzr, #0x2
0x1001de734 <+472>: bl 0x1001de55c ; <+0>
0x1001de738 <+476>: adr x0, #722970 ; "fatal error"
0x1001de73c <+480>: nop
0x1001de740 <+484>: adr x3, #726480 ; "value is outside of Unicode codespace"
0x1001de744 <+488>: nop
0x1001de748 <+492>: movz w1, #0xb
0x1001de74c <+496>: orr w2, wzr, #0x2
0x1001de750 <+500>: movz w4, #0x25
0x1001de754 <+504>: orr w5, wzr, #0x2
0x1001de758 <+508>: bl 0x1001de55c ; <+0>


This is the code for the view controller that I am accessing with dest:
(Thanks to Emptyless)

//
// SecondViewController.swift
// FoodTracker
//
// Created by Michael Buerger on 7/4/16.
// Copyright © 2016 Michael Buerger. All rights reserved.
//

import UIKit

class SecondViewController: UIViewController {

// MARK: Properties
@IBOutlet weak var mealNameLabel: UILabel!
var mealName: String?

override func viewDidLoad() {
super.viewDidLoad()
if mealName != nil {
mealNameLabel.text = mealName
}

}
}

Answer

Most likely the problem is that you are accessing an IBOutlet that has not yet been initialized. If instead of setting the UITextLabel directly you create a variable that holds the text you might have more luck:

As property of SecondViewController:

var labelText : String?

In prepareForSegue() of FirstViewController:

des.labelText = "Your Text"

and in the viewDidLoad() of SeconViewController:

self.MeatNameLabel.text = labelText