Is it possible to pass a unknown type of parameter to a objective-C
method?
In C#
you can write <T>
to achieve this, but I know that objective-C
doesn't have generics so are there some other way to make this possible in objective-C
?
I need this because I want to create a method that changes the text color of different objects like placeholder text of a UITextField
and UIButton
. So my plan was to create one method called textWhite
and then inside this method to check for the kind of object and then run matching code to make the text color white.
Yes, it is possible to pass unknown type of parameter. See below example.
-(void) fooMethod:(id)unknownTypeParameter {
if( [unknownTypeParameter isKindOfClass:[Animal Class]]) {
Animal *referanceObj = (Animal *) unknownTypeParameter;
referanceObj.noOfLegs = 4;
}
}
Refer link for use id object as parameter Using (id) in Objective-C
You can use the anonymous type id
for that. Don't forget to include a check if the object responds to the selectors you're going to use.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With