Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing unknown type of parameter for method in objective c, possible?

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.

like image 995
user2408952 Avatar asked Jan 20 '14 09:01

user2408952


2 Answers

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

like image 121
Akshay Nalawade Avatar answered Oct 09 '22 15:10

Akshay Nalawade


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.

like image 27
Alexander Avatar answered Oct 09 '22 13:10

Alexander