0

Swift 5/Xcode 12.4.

I know how to initialize an empty array:

var myarray = [String]()

I know how to initialize an optional String:

var mystring:String? = ""

How do I create an empty, optional Array and initialize it? I don't want the contents to be optional but the array itself, so I can set it to/check it for nil.

This answer suggests using var myArray = [String]?() but this just shows a "No exact matches in call to initializer " error in playground.

var myarray:[String]? = () also displays an error ("Cannot convert value of type '()' to specified type '[String]?'").

19
  • 5
    Do you mean var myarray:[String]? = []? Commented Mar 4, 2021 at 10:44
  • 3
    Optional<Array> is an anti-pattern in Swift. You can represent the lack of value with an empty array, there's no need to wrap it in an Optional. Commented Mar 4, 2021 at 10:50
  • 1
    @Neph in Java you can use a null-check because everything (except for primitives) is a reference type, so it can be null. However, in Swift, only optionals can be nil and you should only use an optional to a type that can either have a value or not, nil represents the lack of value. However, in case of collections, you can represent the lack of value using an empty collection, hence it's discouraged to use optional collections, since they just complicate your interface without any benefits (in most cases). Commented Mar 4, 2021 at 10:57
  • 1
    "continue using that because that's what I'm already used to" is also an anti-pattern, though. And not just in programming! Commented Mar 4, 2021 at 11:21
  • 1
    "on your own" you can indeed program any way "that compiles and runs the way you want". but as soon as you have a team and reusability - you're in the world of modern software engineering which is entirely about standard standards. say you came up with some fantastic thing (a great control in iOS, some amazing AI routine, a great image processing library, or whatever), and you put it on github, and it became loved and popular. anything "weird" you wrote (don't even mention weird formatting) would simply be ........... instantly changed, that is to say fixed, by everyone else. Commented Mar 4, 2021 at 12:41

1 Answer 1

5

If you really want to do it:

var myArray: [String]? = []

Array literals are denoted by []. not (). () is an empty tuple.

However, as people have mentioned in the comments, this is an anti-pattern. If an "empty array" and "nil" don't mean something different to your code, then just use a regular [String], and use an empty array to indicate "nothing".

Also from the comments, you seem to be trying to write Swift code the way you write your Java code. I suggest you don't do that. When in Rome, do as the Romans do :)

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

1 Comment

Thanks for the answer. An empty array and a nil array are two different things in my code: If it's empty it just hasn't been filled yet (but there is data), if it's nil then it can never be filled because the data for it simply doesn't exist. I need a way to differentiate that and an optional array is the best way to do this without adding and setting an extra bool in x different spots.

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.