Question

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.

Was it helpful?

Solution 3

I'm going to close this question and ask a new one under objective-c changes for iPhone OS 3.0.

I implemented the getter method myself to return an NSArray and it works just fine. Also changing the property to NSMutableArray works but it really isn't what I want to do.

I have no idea why this changed. This is an objective-c change and not an OS change. Also there is no mention of it in the documentation of changes between 2.2 API 3.0 API.

Thanks everyone for responding.

OTHER TIPS

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).

I don't see any reason why this code shouldn't work in OS 3.0 I have similar code working already.

What happens if you change the code to

NSArray *commentArray = comments.comments;
for (cComment *newComment in commentArray){
.....
}

I've just noticed as I've written this where the * is in your code - is this the problem? should it really be cComment* in the for declaration?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top