I have a class called FooView which is a subclass of UIView and I’m trying to cast a UIView object into FooView.
I do this as follows
FooView *myView = (FooView*) MyViewController.view
NSLog(@"Leg is a class of %@",NSStringFromClass([myView class])); // Get UIView here.
The ivar of MyViewController is a UIView type.
I get a UIView result, any idea why this happens?
Casting is a compile-time operation. It doesn’t magically change the type of the object. It’s mostly for compile time checking and a little runtime magic.
myViewis still aUIView, not aFooView.These two lines in your question should give you your answer:
All Objective-C objects have a pointer called
isathat points to an object of typeClass. Casting does not change that pointer, nor does it change the size of the object.If you want to use a
FooView, you need to change the property type in your controller and if you’re using Interface Builder, change the type of the UIView in the inspector on the right.