Check if optional array is empty

后端 未结 7 2241
感情败类
感情败类 2020-11-29 18:21

In Objective-C, when I have an array

NSArray *array;

and I want to check if it is not empty, I always do:

if (array.count &         


        
7条回答
  •  情深已故
    2020-11-29 18:43

    Option D: If the array doesn't need to be optional, because you only really care if it's empty or not, initialise it as an empty array instead of an optional:

    var array = [Int]()
    

    Now it will always exist, and you can simply check for isEmpty.

提交回复
热议问题