0

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.

4
  • You have to declare receivedData on the top level of the class. And what is graph(for segue? Commented Aug 12, 2019 at 12:57
  • I want to draw a graph by using "Charts" lib. So I had named segue as graph. Commented Aug 13, 2019 at 0:07
  • And What is meaning of "declare receivedData on the top level of the class"? Should i have to declare on the View2? Commented Aug 13, 2019 at 0:09
  • top level means outside of viewDidLoad Commented Aug 13, 2019 at 4:24

1 Answer 1

1

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")
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.