Reputation: 1132
I have a method with an id parameter in it. I also have an id as a property inside my class. My question is in the init method is there a way I can determine if the passed parameter can be retained so I can do something like:
someProperty = [idParameter retain];
Thanks
Upvotes: 0
Views: 92
Reputation: 73936
All Objective-C objects (i.e. anything inheriting from the NSObject
class or implementing the NSObject
protocol) implement retain
. It's implemented by the NSObject
class and it's a required method for the protocol, so you cannot have an Objective-C object that you cannot call retain
on.
The only time you'd not be able to call it in these circumstances is if your variable of type id
was not pointing to an Objective-C object. This would be a mistake, do not do this.
Upvotes: 3
Reputation: 16316
Every object that inherits from NSObject has a respondsToSelector:
method. (Documentation)
Therefore, you could write:
if ([idParameter respondsToSelector:@selector(retain)])
someProperty = [idParameter retain];
Upvotes: 2