views:

368

answers:

3

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.

A: 

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?

Grouchal
I tried your suggestion and it seems the problem isn't related to the enumeration at all. NSArray *commentArray = comments.comments results in the exact same errorI'll upadte my question with more details
Ron Srebro
NSFastEnumeration is smart enough to only call the expression that provides the expression once and assigns it to its own scoped variable (invisible to the called) so such an assignment would be redundant.
Quinn Taylor
+1  A: 

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

Marco Mustapic
Exactly what I wanted to prevent, and since NSMutableArray is a subclass of NSArray this really should work as it did in OS 2.2.1. No idea why this was changes in OS 3.
Ron Srebro
If exposing the ivar as mutable is undesirable, you could instead change the ivar from NSMutableArray* to id. This would use dynamic (rather than static) typing for references to "comments", which should also get rid of the compile problems.
Quinn Taylor
Agreed, there really shouldn't be any change, and the mutable subclass should work just fine, especially for a mutable subclass. If you can reliably reproduce this in a small sample of code, and demonstrate that it breaks in iPhone OS 3, you should definitely file a bug: http://bugreport.apple.com
Quinn Taylor
Thanks Quinn. This is definitely reproducible but I'll try and find more info on the issue before filling a bug.
Ron Srebro
A: 

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.

Ron Srebro
It may just be that the compiler is stricter for some reason. (Which compiler are you using, anyway? In newer versions of Xcode you can choose.) You shouldn't have to close this question — you (or I) can retag it with something more appropriate.
Quinn Taylor
Since I'm really rather new to Mac development and Xcode I'm not really sure what you mean when you say which compiler. I'm changing my compling settings using the dropdown box found on Xcode main screen (Maybe that's the problem). Since I'm also new to the site I'm also not sure about the appropriate way to handle the question but I did submit a new one. Would appricate your advice.New Question - http://stackoverflow.com/questions/1019365/objective-c-changes-between-os-2-2-1-and-os-3
Ron Srebro