I want to check if my array is empty or null, and on base of which I want to create a condition for example.
if(array == EMPTY){
//do something
}
I hope I'm clear what I am asking, just need to check if my array is empty?
regards
if (!array || !array.count){
...
}
That checks if array is not nil, and if not - check if it is not empty.
array.count should be [array count] since you're not dealing with a property (var) here.array.count is just fine in that context. Syntactically, anyway. Stylistically? No particular standard is recommended at this time.if (!array.count)@property (readonly) NSUInteger count;if ([array count] == 0)
If the array is nil, it will be 0 as well, as nil maps to 0; therefore checking whether the array exists is unnecessary.
Also, you shouldn't use array.count as some suggested. It may -work-, but it's not a property, and will drive anyone who reads your code nuts if they know the difference between a property and a method.
UPDATE: Yes, I'm aware that years later, count is now officially a property.
Best performance.
if (array.firstObject == nil)
{
// The array is empty
}
The way to go with big arrays.
count property, which implies enumerating the array and can be a performance issue with big arraysSwift 3
As in latest version of swift 3 the ability to compare optionals with > and < is not avaliable
It is still possible to compare optionals with ==, so the best way to check if an optional array contains values is:
if array?.isEmpty == false {
print("There are objects!")
}
as per array count
if array?.count ?? 0 > 0 {
print("There are objects!")
}
There are other ways also and can be checked here link to the answer
null and empty are not the same things , i suggest you treat them in differently
if (array == [NSNull null]) {
NSLog(@"It's null");
} else if (array == nil || [array count] == 0) {
NSLog(@"It's empty");
}