开发者

how to detect if an array isnt empty?

开发者 https://www.devze.com 2023-01-14 17:51 出处:网络
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 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 ther开发者_如何学JAVAe 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];


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];
    }
    


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.


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
}


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

0

精彩评论

暂无评论...
验证码 换一张
取 消