1

I'm trying to initialise an array of Photos, and am doing so like this:

NSMutableArray *photoList = [[NSMutableArray alloc] init];
self.photos = photoList;

But when this line of code runs, I get this error:

'NSInvalidArgumentException', reason: '-[Project setPhotos:]: unrecognized selector sent to instance 0xee8e310'

I've spent about three hours trying to find a fix but couldn't, can anybody tell me what to do?

3
  • 1
    Do you have a property declared and synthesize'd it? Commented Aug 3, 2012 at 14:14
  • Why not just use self.photos = [[NSMutableArray alloc] init];? Commented Aug 3, 2012 at 14:15
  • I was trying to debug... Commented Aug 3, 2012 at 14:16

5 Answers 5

4

It seems to me that you haven't created a property photos and if you have then it would also seem that this property is not @synthesize'd in your implementation, maybe you are using @dynamic, in which case it is up to you to create a - (void)setPhotos:(NSMutableArray*)photos; method

Sign up to request clarification or add additional context in comments.

Comments

2

self does not have a property called photos

You need to add @property (nonatomic, strong) NSArray *photos; in your .h file, before the @end

and in the .m file, @synthesize photo;

Comments

2

This line of code:

self.photos = photoList;

gets turned into this line

[self setPhotos:photoList];

by the compiler - the dot notation is what is called "syntatic sugar" as is just makes it easier to type, it doesn't really shorten the code.

If you have created your own getters and setters (ie)

- (NSMutableArray *)photos;
- (void)setPhotos:(NSMutableArray *)myPhotos

Then you can use that sugar even though you don't have a property called "photos" - although this is considered a misuse of the feature (I show it for comparison purposes).

When you create a property named photos:

@property (nonatomic, strong) NSMutableArray *photos;

the compiler will generate an ivar for you using the same name, but not create the getters and setters. The line:

@synthesize photos;

asks the compiler to do a getter (in all cases) and a setter (if the property is read write). If you do not provide a @synthesize statement, the compiler normally complains, so people should be observing these warnings.

You can see in the error that you have no setPhotos, thus your problem can be fixed quite easily.

Comments

2

Seems like you haven't written a setter method for photos.

in your .h

@property (strong, nonatomic) NSArray * photos;

in your .m (if not using xcode 4.4)

@synthesize photos;

or you could just try writing it like this

photos =  [NSArray arrayWithArray:photoList];

For reference if you use [self setMyArray:array] or self.array = myArray Then you are using the setter method, which is something you probably want to do. If you just point array = myArray, you would be pointing to myArray and if it were released your pointer would point in to the abyss. It's good to not to do that, not using the setter means you are accessing the variable photos directly and this may not be what you want.

4 Comments

Or perhaps, @synthesize photos = _photos and _photos = .... Avoids the all too common mistake of confusing assignment with setter. +1 for noting that @synthesize no longer needed.
If you're using xCode 4.4 you get @synthesize photos = _photos for free.
Yeah, I know. This is why I was surprised when you omitted the underscored ivar reference from the "pre 4.4" example of the explicit @synthesize statement. It's a personal thing, but if nothing else, with 4.4, Apple seems to be further endorsing this as best practice.
it was omitted as it's a convention not a rule :)
1

Whoops, I'd used @dynamic photos instead of @synthesize photos

5 Comments

You're right, did you see my answer, I guessed you made that mistake.
I'll give you the answer you may as well have the rep :P
if you update your version of xCode you wouldn't have to right @synthesize any more.
Yea, I don't like that Xcode does everything for you, we're about to enter a stage of dummy developers... But for sure it does speed things along I guess. Just as long as you know how to do it the other way. Oh also, it's Xcode not xCode :) Cheers todd for the answer... if you'll still choose me. :)
Yeah, sorry about the delay, I just finished work and had to come home :P

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.