0

I want to initialiaze an array if it is nil.

The reason for this is that I have an optional array (as I want it to be nil until an API call is made - it is optional and this is part of the question context):

Here is the issue:

var array: [String]? = ["1","2","3"]
if array == nil { pageArray = []}
array! += ["4"]

The code works but is not easy to read. There is a force unwrap for array which is not good.

What the question isn't: Note this is not production code, does not contain the imports for UIKit or other code not related to the array.I may have badly named variable etc. but this question is about an array. I may have misspelt something in this question and it might not be clear, but I really need help on the initialization of the array and the title of the question (and the question itself) refers to this.

What I've tried I tried a guard, but of course guards should not fallthrough so this seems a poor option.

The question How can I initialize an array if and only if it is nil?

7
  • Your first issue is that LoginResponse response works by setting the value of a global variable like pageArray. That's nasty, and it's going to bite you in the ass. Instead, use a proper async mechanism. It could be callback closures, Futures/Promises, or observable streams like from RxSwift or Apple's Combine. I would start with medium.com/ios-os-x-development/… Commented Sep 23, 2019 at 2:21
  • I am. This is a code snippet to explain the problem with the initialization of an array. It is nothing to do with global variables or similar, the response is given to give some idea of the issue at hand (note: there is also no network code here at all, I haven't included UIKit/Foundation and more: this isn't a mistake it is to make this a small example about the initialization of an array). Commented Sep 23, 2019 at 2:24
  • Hmm, then I don't get it. What relevance does let content = LoginResponse(linkslist: ["1","2","3"]) have, exactly? Does it effect pageArray in some capacity? If it doesn't, then why is there the seperation between declaration (var pageArray: [Page]?) and initialization (if pageArray == nil { pageArray = [] })? Why couldn't you just write var pageArray = [] Commented Sep 23, 2019 at 2:45
  • It's a minimal example, without an API call. To try to head off inevitable questions of "just don't use an optional array". I'll rewrite the question then, to give less context for you then to make it easy to understand. Commented Sep 23, 2019 at 2:47
  • My issue is that I assumed that everythign you provided was already part of a minimal example. As such, I figure that there's a reason why the line let content = LoginResponse(linkslist: ["1","2","3"]) is where it is, and that there's a reason that you wrote a seperate decl/init rather than just var pageArray = []. Commented Sep 23, 2019 at 2:50

1 Answer 1

3

First of all, you should better re-consider if you really need to distinguish an empty Array and nil. You may think you need it, but your example is so simplified that readers cannot judge if it really is needed.

Assuming you need it Optional, I would write it as:

var array: [String]? = ["1","2","3"]
array = (array ?? []) + ["4"]
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.