Jakub
Jakub

Reputation: 13860

Override setter in objective-c

I want set NSDate from NSInteger and i have @property:

@property (strong,nonatomic) NSDate *date;

I want setter:

-(void)setDate:(NSInteger *)tStamp {
    thumbImgUrl = [NSDate dateWithTimeIntervalSince1970:tStamp];
}

So i want to set my date from NSInteger value not NSDate value. How can i declare this setter? When i try to put it in header file it works ok, but i'm getting a warning:

Type of property 'date' does not match type of accessor 'setDate:

And if i want to declare it in .m file i'm getting error:

duplicate declaration of method setDate:

Why? There is a way to declare setter with different input type?

Upvotes: 1

Views: 866

Answers (2)

Adam Johnson
Adam Johnson

Reputation: 2216

You may be able to accept a type of id as the input parameter to the setter function.

You are getting the duplicate declaration of method setDate: because most likely you have the line @synthesize date in your implementation file. This automatically generates the getter/setters for you. Remove that line, since you are providing your own implementation.

Upvotes: 0

TheAmateurProgrammer
TheAmateurProgrammer

Reputation: 9392

Generally this is a really bad way of doing things in Objective-C, and I would highly recommend you to change your method name to something else such as -(void)setDateWithInteger:(NSInteger)tStamp. (Speaking of which, NSInteger is a primitive, not an object) And to answer your question, no, there is no way to declare a setter with different input type if you use @property. If you do insist doing it your way, then you will have to declare the method without using @property.

Upvotes: 5

Related Questions