-1

I have a array of my custom model, and I want to check if it is not nil and its size is greater then 0.

Following is my array with custom object

var listCountries : [Countries]? = nil

now In viewDIdLoad I want to make a check on it. I am new to Swift. I have good experience in working in Java.

I have read out Optional values concept and guard, if let statements. But I am unable to understand how efficiently they may be used. I have read too much SO questions but failed to figure out.

for example , if I want to check the upper given array in java I have only to do

if(listCountries != null && listCountries.size()>0){
    //DO something 
}

So to summarize my question:

  1. How to make the upper given(Java code) check in to swift 4.? What is more smooth and reliable way.
  2. What is a use of if let , guard, guard let statements. if I declare a variable (array, string) as optional I have to bear optional check like force wrapping each and every place. This is for me so making too much confusion.

Please help. I know this question has been asked in different ways. But this has some different context.

Android teem
  • 780
  • 2
  • 13
  • 36

4 Answers4

8

Just use ??.

if !(listCountries ?? []).isEmpty {

However, since you want to probably use listCountries in the if block, you should unwrap

if let listCountries = self.listCountries, !listCountries.isEmpty {

Ideally, if nil and empty means the same to you, don't even use an optional:

var listCountries: [Countries] = []
Sulthan
  • 128,090
  • 22
  • 218
  • 270
5

I would do it something like...

if let list = listCountries, !list.isEmpty { // Can also use list.count > 0
    // do something
}

Even though you are not using the list inside the braces you are still using the list in the condition.

Or, like Sulthan said... make it non-optional to begin with if it makes no difference.

Fogmeister
  • 76,236
  • 42
  • 207
  • 306
2

Obviously, I would assume that you are able to recognize the difference between nil array and empty array.

So, if we tried to implement a literal translation to your question:

I want to check if it is not nil and its size is greater then 0

For the first condition:

// "I want to check if it is not nil":
if let unwrappedList = listCountries {
    // ...
}

and for the second condition:

// "I want to check if it is not nil":
if let unwrappedList = listCountries {
    // "and its size is greater then 0":
    if !unwrappedList.isEmpty {
        // ...
    }
}

However, you could combine both of the conditions by using the comma to achieve the multi-clause condition:

// I want to check if it is not nil and its size is greater then 0
if let unwrappedList = listCountries, !unwrappedList.isEmpty {
    // ...
}

Or by using guard statement:

// I want to check if it is not nil and its size is greater then 0
guard let unwrappedList = listCountries, !unwrappedList.isEmpty else {
    return
}
Ahmad F
  • 30,560
  • 17
  • 97
  • 143
-1
if let list = listCountries {
    if(!list.isEmpty && list.count > 0) {
            //value   
        }
}