Casting to specify unknown object type?
Posted
by fuzzygoat
on Stack Overflow
See other posts from Stack Overflow
or by fuzzygoat
Published on 2010-06-16T15:49:07Z
Indexed on
2010/06/16
15:52 UTC
Read the original article
Hit count: 287
In the following code I have a view object that is an instance of UIScrollView, if I run the code below I get warnings saying that "UIView might not respond to -setContentSize etc."
UIImage *image = [UIImage imageNamed:@"Snowy_UK.jpg"];
imageView = [[UIImageView alloc] initWithImage:image];
[[self view] addSubview:imageView];
[[self view] setContentSize:[image size]];
[[self view] setMaximumZoomScale:2.0];
[[self view] setMinimumZoomScale: [[self view] bounds].size.width / [image size].width];
I have checked the type of the object and [self view]
is indeed a UIScrollView
. I am guessing that this is just the compiler making a bad guess as to the type and the solution is simply to cast the object to the correct type manually, am I getting this right?
UIScrollView *scrollView = (UIScrollView *)[self view];
UIImage *image = [UIImage imageNamed:@"Snowy_UK.jpg"];
imageView = [[UIImageView alloc] initWithImage:image];
[[self view] addSubview:imageView];
[scrollView setContentSize:[image size]];
[scrollView setMaximumZoomScale:2.0];
[scrollView setMinimumZoomScale: [scrollView bounds].size.width / [image size].width];
cheers Gary.
© Stack Overflow or respective owner