Search code examples
iosobjective-carraysios10

Array of UIButtons return error


I have an array of UIButton objects (I have 5 buttons so I wanted to store them in an array for easy processing). But Array give me error

"Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArray0 addObject:]: unrecognized selector sent to instance"

@property (weak, nonatomic) IBOutlet UIButton *starOne;
@property (weak, nonatomic) IBOutlet UIButton *starTwo;
@property (weak, nonatomic) IBOutlet UIButton *starThree;
@property (weak, nonatomic) IBOutlet UIButton *starFour;
@property (weak, nonatomic) IBOutlet UIButton *starFive;
@property (nonatomic, copy) NSMutableArray *_starButtons;

I have below code in viewDidLoad method

- (void)viewDidLoad {
   self._starButtons=[[NSMutableArray alloc]init];
    [self._starButtons addObject:self.starOne];
    [self._starButtons addObject:self.starTwo];
    [self._starButtons addObject:self.starThree];
    [self._starButtons addObject:self.starFour];
    [self._starButtons addObject:self.starFive];

 NSLog(@"%@",self._starButtons);
}

Please help me where i am going wrong.


Solution

  • First remove copy from declaration of array property, make it strong.

    Second thing as you have said in comment that you have programmatically created buttons then you not need IBOutlets. So, remove IBOutlets from all properties of button.

    Your declaration should like,

     @property (weak, nonatomic) UIButton *starOne;
     @property (weak, nonatomic) UIButton *starTwo;
     @property (weak, nonatomic) UIButton *starThree;
     @property (weak, nonatomic) UIButton *starFour;
     @property (weak, nonatomic) UIButton *starFive;
     @property (nonatomic, strong) NSMutableArray *_starButtons;