문경욱
문경욱

Reputation: 51

Xcode pass Array by using segue

I made an array and put some values into it.

And by using segue,

I want to send the array to another view.

Here are codes.

----- View1 -----

override func viewDidLoad() {
    super.viewDidLoad()
    var receivedData: Array<String> = Array()
    receivedData.append("0.123")
    receivedData.append("0.190")
    receivedData.append("0.210")
    receivedData.append("0.213")
}


 @IBAction func graph(for segue: UIStoryboardSegue, sender: Any?) 
{

        if segue.destination is GraphViewController
        {
            let vc = segue.destination as? GraphViewController
            vc?.transferedData = receivedData
        }

}

----- View2 -----

var transferedData: Array<String> = Array()

But it doesn't work.

There are errors likes

"unrecognized selector sent to instance"

"Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: unrecognized selector sent to instance "

" libc++abi.dylib: terminating with uncaught exception of type NSException"

Help me to solve this problem.

Thanks for reading.

Upvotes: 0

Views: 43

Answers (1)

SDS
SDS

Reputation: 211

The reference of receivedData array will not accessible outside viewDidLoad() method which you have created. you have to declare it as a global array for that class, then you can access that array throughout your class.


Please declare receivedData outside of viewDidLoad() function. Please do following.

var receivedData: Array<String> = Array()

override func viewDidLoad() {
    super.viewDidLoad()

    receivedData.append("0.123")
    receivedData.append("0.190")
    receivedData.append("0.210")
    receivedData.append("0.213")
}

Upvotes: 1

Related Questions