While working on a project i made a class i instantiate in another class (nothing special) but if i try to call a method for this instance i get this:
Terminating app due to uncaught exception ‘NSInvalidArgumentException’, reason: ‘-[CALayer animate]: unrecognized selector sent to instance
here is the class .h :
#import <QuartzCore/QuartzCore.h>
@interface SFStripe : CALayer {
NSTimer *animation;
}
- (id)initWithXPosition:(NSInteger)positionX yPosition:(NSInteger)positionY;
- (id)initEndedWithXPosition:(NSInteger)positionX yPosition:(NSInteger)positionY;
- (void)animate;
- (void)reduceSize;
- (void)logSomething;
@property NSTimer *animation;
@end
and here the .m :
#import "SFStripe.h"
@implementation SFStripe
@synthesize animation;
- (id)initWithXPosition:(NSInteger)positionX yPosition:(NSInteger)positionY {
self = [CALayer layer];
self.backgroundColor = [UIColor blackColor].CGColor;
self.frame = CGRectMake(positionX, positionY, 5, 20);
self.cornerRadius = 5;
return self;
}
- (id)initEndedWithXPosition:(NSInteger)positionX yPosition:(NSInteger)positionY {
self = [CALayer layer];
self.backgroundColor = [UIColor grayColor].CGColor;
self.frame = CGRectMake(positionX, (positionY + 7.5), 5, 5);
self.cornerRadius = 2;
self.delegate = self;
return self;
}
- (void) logSomething {
NSLog(@"It did work!");
}
- (void)reduceSize {
if (self.frame.size.width > 0 && self.frame.size.height >= 5) {
self.frame = CGRectMake(self.frame.origin.x, (self.frame.origin.y - 0.5), self.frame.size.width, (self.frame.size.height - 0.5));
[self setNeedsDisplay];
NSLog(@"Width: %d", (int)self.frame.size.width);
NSLog(@"Height: %d", (int)self.frame.size.height);
} else {
self.backgroundColor = [UIColor grayColor].CGColor;
self.frame = CGRectMake(self.frame.origin.x, (self.frame.origin.y + 7.5), 5, 5);
[animation invalidate];
}
}
- (void)animate {
animation = [NSTimer scheduledTimerWithTimeInterval:0.03 target:self selector:@selector(reduceSize) userInfo:nil repeats:YES];
}
@end
i imported this class into a fresh project just to see if it is working there but get the same error. here is how i call one of the methods for an instance of the class (i get the same error for all of the methods)
in the mainview.h (of the clean project):
#import <UIKit/UIKit.h>
#import "SFStripe.h"
#import <QuartzCore/QuartzCore.h>
@interface STViewController : UIViewController {
SFStripe *stripe;
}
- (IBAction)animate:(id)sender;
@end
i created the instance and in the .m i let the action call the method for the instance:
#import <QuartzCore/QuartzCore.h>
#import "STViewController.h"
#import "SFStripe.h"
@interface STViewController ()
@end
@implementation STViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
stripe = [[SFStripe alloc] initWithXPosition:30 yPosition:30];
[self.view.layer addSublayer:stripe];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)animate:(id)sender {
[stripe animate];
}
@end
sorry for all the code but i cant find an answer for this problem that helps and hope someone can help me!
This is not the correct way to write an initializer:
You’re assigning
selfto be a plain old CALayer instance, rather than an instance of your subclass.Use
self = [super init]instead, and then check thatselfis not nil.