I would like to have an array as a member of my table view controller. The array will be a data source.
What are the differences or advantages/disadvantages the following ways of having a member variable array.
class BinViewController: UITableViewController, WKNavigationDelegate {
var peopleArray1 = [String]()
var peopleArray2: [String] = []
var peopleArray3: [String]!
var peopleArray4: [String]?
These two basically do the same thing:
They declare and initialize an empty array of type
[String]
.These two in the other hand, are also similar:
The array itself for both of them could be
nil
, which they currently are as they are declared.The difference is that
peopleArray3
is an implicitly unwrapped optional array of Strings, whilepeopleArray4
is an optional array of Strings.What this means for
peopleArray3
is that you're telling the complier that you will set a value to it before it's used. If you try to access it while its value isnil
, the app will crash.For
peopleArray4
, you're telling the compiler that the array might benil
at any time, so you'll need to check ifpeopleArray4
is notnil
before you access it.I think there is not the right question that you need to ask yourself. There is no advantage or disadvantage. There are ways to initialize a member of a class based on what you need and your logic that you want to implement.
var peopleArray1 = [String]()
you are saying to the compiler please let me create an empty array of string. It's declared and initialized. Also you let the compiler to infer the type of your array compared to your second statement.var peopleArray2: [String] = []
it's 99% the same thing as your first statement. You are creating en empty array of String. Declared and initialized. Here the only difference is that you specify the type of your array. You don't let the compiler to infer the type of your array.var variable:TypeVariable = ...
. It's recommended by Apple to let the compiler to infer the type of your variables members when you can because you win a readable codevar peopleArray3: [String]!
it's pretty different than your previous 2 initializations. First, you need to know that Swift is a very strongly typed language and you need to initialize every variable before using it. One thing that you can experiment is try to make the same statement without the ! or ? and you will your compiler warned you with an issue. Sometimes you just don't know what is the initial value but you are sur that at one moment, you will have a value. A variable is these two possible states : no value or a value are called Optionals. With this statement you are saying i want to create an array of string that might be nil or might have some String values. If you print your array you will have a message like "Optional([String])". To get the value of your Optional you need to unwrap it with the exclamation mark only when you are sure that you have a value. If not, your program is going to crash. Here with this initialization, you are saying to the compiler to not be worried because you know that in the future your array is going to have some values.var peopleArray4: [String]?
is the same as the previous one. The main difference is that everytime you want to access peopleArray values you will need to put an exclamation mark to unwrap his value. There is no advantage/disadvantage just logic to use Optionals. If your array might be nil and further have values or is it a static array...