2

i'm a newbie of the world of objective-c.

what i'd like to know is how to check whether a mutable array object exists.

here is an exmaple.

if(![appDelegate.answerList objectAtIndex:3])
    {
        answer = [[NSMutableArray alloc] init];
    }
    else
    {
        answer = [[NSMutableArray alloc] initWithArray:[appDelegate.answerList objectAtIndex:3]];
    }

above this code, 'answer' object is local, and 'answerList' object is on appDelegate class.

both are NSMutableArray objects.

i don't know whether answerList's third object is allocated or not.

If it already has an object, i wanna just copy an object from answerList's third object.

But that code doesn't work.

I'm not familiar with objective-c's methods.

please show me the way.

1 Answer 1

2

You can check if an array is empty by comparing it's count property to a number (in this case, 4, because arrays are 0 based). And because arrays do not accept objects that are nil and send a -retain message to all of their objects, you would technically only need to compare count instead of object existence. However, you can nest your current if...else... blocks in this as well for much more accurate (if redundant) results.

if([appDelegate.answerList count] <= 4)  //object exists, and the array contains a valid index.
Sign up to request clarification or add additional context in comments.

8 Comments

first of all, thank you very much. but the problem is answerList array object doesnt have objects serially. it means,.. it's like hm.. in old C code style, 'int array[5] = {0, NULL, 0, NULL, 1}. the problem is, i cannot access third part of non-allocated object with 'objectAtIndex' method.
Anytime. Anymore questions related to this?
first of all, thank you very much. but the problem is answerList array object doesnt have objects serially. it means,.. it's like hm.. in old C code style, 'int array[5] = {0, NULL, 0, NULL, 1}. the problem is, i cannot access third part of non-allocated object with 'objectAtIndex' method.
Yes. You are not allowed to insert non-objects, and non-initialized objects in NSArrays, which (I think) is where the problem lies. If you must, insert an NSNull object.
I get your point. you mean, NSArray removes empty sections sorts itself automatically, dont you?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.