Difference between these arrays as member variable

2019-03-07 06:25发布

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]?

2条回答
一夜七次
2楼-- · 2019-03-07 07:03

These two basically do the same thing:

var peopleArray1 = [String]()
var peopleArray2: [String] = []

They declare and initialize an empty array of type [String].


These two in the other hand, are also similar:

var peopleArray3: [String]!
var peopleArray4: [String]?

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, while peopleArray4 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 is nil, the app will crash.

For peopleArray4, you're telling the compiler that the array might be nil at any time, so you'll need to check if peopleArray4 is not nil before you access it.

查看更多
SAY GOODBYE
3楼-- · 2019-03-07 07:18

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 code

  • var 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...

查看更多
登录 后发表回答