0

Actually, the problem is in @property (nonatomic, copy) NSMutableArray* arrayOfObjects;

should be: @property (nonatomic, strong) NSMutableArray* arrayOfObjects; yeah? Thank,s for that guys!

I do not exactly understand how this bug worked, but I will try to get to know :)


I have trying to solve one problem since over an hour... I am afraid it is something very simple, but something I do not understand yet. I am guessing that this is something with memory management stuff cause this is my definitely weak point yet.

Post a little bit similar to 'Unrecognized selector sent to instance' and some others but they didnt solved my problem...

In a nutshell (pasting much cause don't know where the potential bug is):

@interface MyCustomObject : NSObject {
    NSString* name;
    int birthDate;
    double heightInMeters;
}

@property(strong, nonatomic) NSString * name;
@property(nonatomic) int birthDay;
@property(nonatomic) double heightInMeters;

-(id)initWithDate:(int)birthDate 
        AndName:(NSString *)nameString 
        AndHeight:(double)h;
@end

//////////////////////////////////

#import "MyCustomObject.h"

@implementation MyCustomObject

@synthesize name;
@synthesize birthDay;
@synthesize heightInMeters;

-(id)initWithDate:(int)bd AndName:(NSString *)nameString AndHeight:(double)h{

    if(self = [super init]){
        self.birthDay = bd;
        self.name = nameString;
        self.heightInMeters = h;
        return self;
    }
    return nil;
}
@end

And some kind of database for MyCustomObjects:

DataCollection.h:

@interface DataCollection : NSObject{

    NSMutableArray* arrayOfObjects;

}
@property (nonatomic, copy) NSMutableArray* arrayOfObjects;

    -(void)addElement:(id)el;
@end

And implementation:

    #import "DataCollection.h"

    @implementation DataCollection 
    @synthesize arrayOfObjects;

    -(id)init{
        if(self = [super init]){
            self.arrayOfObjects = [[NSMutableArray array] init];
            NSLog(@"Number of record before init: %d", [self.arrayOfObjects count]);
            [self addElement:[[MyCustomObject alloc] initWithDate:1988 AndName:@"Georg" AndHeight:1.88]];
            [self addElement:[[MyCustomObject alloc] initWithDate:1951 AndName:@"Sebastian" AndHeight:1.58]];
            NSLog(@"Number of record before init: %d", [self.arrayOfObjects count]);
            return self;
        }
        return nil;
    }

    -(void)addElement:(id)el{
        // UNRECOGNIZED SELECTOR ????
[self.arrayOfObjects addObject:el];
    }

    @end

Result is:

2013-03-05 15:42:56.826 XMLTest[11787:207] Number of record before init: 0 Current language: auto; currently objective-c 2013-03-05 15:43:51.446 XMLTest[11787:207] -[__NSArrayI addObject:]: unrecognized selector sent to instance 0x6816110

Do you see what I am doing wrong? I am guessing that this is something with memory management stuff

Cœur
  • 37,241
  • 25
  • 195
  • 267
radekEm
  • 4,617
  • 6
  • 32
  • 45
  • 1
    Why are you using (nonatomic, copy) and not (nonatomic, strong) ? – johan Mar 05 '13 at 14:54
  • Or at the very least `(nonatomic, retain)` – Dan F Mar 05 '13 at 14:55
  • 1
    That could likely be your problem, when you do the assignment of `self.arrayOfObjects = blah` it is calling `copy` on the `NSMutableArray` which may be returning a non-mutable copy of the array – Dan F Mar 05 '13 at 14:56
  • Possible duplicate of [How can I debug 'unrecognized selector sent to instance' error](https://stackoverflow.com/questions/25853947/how-can-i-debug-unrecognized-selector-sent-to-instance-error) – Cœur Jul 08 '19 at 05:53

2 Answers2

3

If you change

@property (nonatomic, copy) NSMutableArray* arrayOfObjects;

to

@property (nonatomic, strong) NSMutableArray* arrayOfObjects;

That should fix your problem

Darren Findlay
  • 2,533
  • 2
  • 29
  • 46
  • 1
    You should mention that `copy` will create an immutable copy (hence the error the OP is seeing), as well as not actually updating the instance variable as desired. – trojanfoe Mar 05 '13 at 15:05
  • 1
    Yes, as trojanfoe said, when your @property directive is set to copy, your getter method will actually make a copy of your arrayOfObjects, but it will be an NSArray instead of an NSMutableArray. NSArray does not implement the addObject: method, therefore resulting in the Unrecognised selector error. – Darren Findlay Mar 05 '13 at 16:09
0

I didn't actually read your question - it's too long, but I think I may know your problem. Looking only at the debugger output at the end of your post, it looks like you're trying to send addObject: to an NSArray object. If you want to add and remove objects to an array, you need to use NSMutableArray instead.

jhabbott
  • 18,461
  • 9
  • 58
  • 95
  • Yeah, a sneaky problem for sure. :) The point is - trust the debugger messages, follow them up. This would lead you to "why is my mutable array being converted to an array?" which would lead to your answer. :) – jhabbott Mar 05 '13 at 15:50
  • Yeah, after some research and learning I found your answer not so bad :D – radekEm Mar 05 '13 at 19:23