0

I have this array defined inside a class:

var pickOption: [String]?

inside my classes init, I am trying to append to the array:

override init!(reuseIdentifier identifier: String!) {
      self.pickOption?.append("aaa")
      print(self.pickOption)
}

but self.pickOption is returning nil, why? and how can I fix it?

0

2 Answers 2

1

With first statement var pickOption: [String]? you have just declared the array of type string but never allocate the memory. As this is optional type, it will be nil at the time of declaration.

You need to allocate memory for array before using it. You can declare array as this var pickOption = [String]() and rest of code will do the work!!

Sign up to request clarification or add additional context in comments.

Comments

0

You have to init it declare it like this

var pickOption = [String]()

as this line

self.pickOption?

with the optional won't run as in it's moment pickOption is nil

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.