Objective-c – Fast enumeration in OS 3.0

iphoneiphone-sdk-3.0objective c

I just tried compileing my iPhone app against OS 3.0 and I get a compile error when using fast enumeration.

I'm trying to go through an NSArray containing cComment classes:

for (cComment* newComment in comments.comments)

And I get this error

error: type of accessor does not match the type of property 'comments'.

This works flawlessly when compiled with OS 2.2.1.

I understand the error, the enumaretion isn't strongly typed but since as far as I know generics/templates are not supported in objective-c. So currently I can only see one way around this:

for (id commentObject in comments.comments)
{
     cComment *newComment = (cComment *)commentObject;
}

Can anyone think of another way? Why has this changed? Any points to apple documentation about this change would be appreciated.

EDIT

Following Grouchal suggestion i tried this:
NSArray* allComments = comments.comments
and I got the same error so it seems its not about the enumeration after all

here's the code form the header file:

NSMutableArray *comments;

@property (readonly,nonatomic) NSArray* comments;

and the property is synthesized in the implementation file.

Changing the property to NSMutableArray seems to solve the problem but I don't see any reason why this should be the case. Any ideas.

Thanks.

Best Answer

The problem is in the property definition. You should change have it as:

@property (readonly,nonatomic) NSMutableArray* comments

On the downside, your array will be exposed as mutable (though I suppose that's what you wanted to prevent).

Related Topic