views:

98

answers:

4

I am trying to detect if an array isn't empty in order to be able to do a certain call. I tried using if (![array ==nil]) however that doesn't compile. I'm sure there is a really easy explanation to this.

Update

If array is empty I want to do this:

array = [[NSMutableArray alloc]init];

If it has an object I want to do this:

array = [[userDefaults arrayForKey:@"MyFavorites"] mutableCopy];
+6  A: 

If you declared it but did not assign anything to it at all:

NSMutableArray *array;

Then the array will be nil, meaning it isn't there at all so you can't say if it's empty or not, so you can't check anything.

If you did assign something to it, and you want to find out if the existing array is empty or not, that would depend on how you created it first.

  • If the array was assigned from some convenience method, it's autoreleased, so just do this:

    if ([array count] == 0) {
        array = [[NSMutableArray alloc] init];
    } else {
        array = [[userDefaults arrayForKey:@"MyFavorites"] mutableCopy];
    }
    
  • If the array was assigned from an init or copy method, or it was retained previously, store the count in a temporary variable, release the array and use the temporary variable to decide what to do:

    NSInteger count = [array count];
    [array release];
    
    
    if (count == 0) {
        array = [[NSMutableArray alloc] init];
    } else {
        array = [[userDefaults arrayForKey:@"MyFavorites"] mutableCopy];
    }
    
BoltClock
im trying to do this alloc and init and array and if an array is empty and if its not empty to copy it using nsuserdefaults ([[userDefaults arrayForKey:@"MyFavorites"] mutableCopy];) however i cant get it to work i tried the count but i cant implement it with the array creation
Alx
Pasting more code into your question might help.
BoltClock
A: 

Supposing you are talking about NSArray, if myArray has not been properly alloced+initialized (what you are trying to check) its reference will be nil, so you can do:

if(myArray) //or even if(myArray != nil) since myArray will be a pointer
{
//properly inizialized
}
else
{
//not properly inited
}

If it's been inited on the other hand, you can test its emptiness by checking the count property which returns the number of elements it contains

if([myArray > 0]) 
//there is at least one element
}
else
{
//no elements
}
rano
+1  A: 

In your case I'd always use without differentation

array = [[userDefaults arrayForKey:@"MyFavorites"] mutableCopy];

and set the default value in the user defaults to an empty array right away at program start before accessing the defaults (from Apple's example):

+ (void)initialize{

    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSDictionary *appDefaults = [NSDictionary
        dictionaryWithObject:[NSArray array] forKey:@"MyFavorites"];

    [defaults registerDefaults:appDefaults];
}

See Apple's doc on this.

Eiko
A: 

you can use count function of NSArray. it will work on NSMutableArray too....

syntext will be,

int ct=[array count];

ct will have number of items in array.

if it us empty it will be Zero

Nirmit Patel