Objective-C id & NSObject

I understand that the id is for any type of object, even objects that do not inherit NSObject, such as things from Cocoa. I was told almost always to use id, but what if I created an API and had a method that I would like to make clear that it should only accept a certain type of object, an object called Animal, I would still use

(id) animal

or i will do

(Animal) animal

Many thanks!

+3
source share
2 answers

id - - void *, , Objective-C. , id , , :

- (id)animal;  // OK if 'animal' could be any type of object
- (Animal*)animal; // much better if you know that 'animal' points to an object of type 'Animal'

, Cocoa Cocoa Touch. UIView:

- (BOOL)isDescendantOfView:(UIView *)view;  // returns YES for self.
- (UIView *)viewWithTag:(NSInteger)tag;     // recursive search. includes self

, UIView* . -, UIView, .

UIView*, , UIView:

[[topView viewWithTag:someTag] removeFromSuperview];

, .

+6

, Animal, NSObject id. . Animal, ,

+2

All Articles