覆盖类变量,或者至少覆盖 Objective-C / cocoa 中的变量类型
我的困境如下:
我有一个 UIImage 的自定义子类(我添加了一些序列化方法,即 initWithCoder、encodeWithCoder),并且我想将我的自定义子类作为变量应用于 UIImageView,或者至少是 UIImageView 的子类。
如您所知,UIImageView 有一个名为“image”的变量,其类型为 UIImage。 我想用我的子类覆盖这个变量。
我的目标是拥有一个 UIimageView,它将响应 archivedDataWithRootObject,而不会因将encodeWithCoder 消息发送到变量图像而崩溃。 如果有更明智的方法可以实现这一目标,我愿意接受建议。
谢谢你!
[编辑] 我认为一种可能的途径是通过某种类型的转换...但是:
UIImage *image = [[UIImage alloc] init];
MLImage *mlImage = [[MLImage alloc] init];
mlIamge = (MLImage*)image;
当我在第二行执行后将鼠标悬停在 mlImage 上时,我可以看到它是 MLImage 类型。 然而,即使进行了强制转换,第三行执行后 mlImage 也会变成 UIImage。 我需要做什么才能将图像类型更改为 MLImage? [/编辑]
My dilemma is as follows:
I have a custom subclass of UIImage (I've added some serialization methods i.e. initWithCoder, encodeWithCoder), and I would like to apply my custom subclass as a variable to a UIImageView, or at least a subclass of UIImageView.
As you are aware, UIImageView has a variable called "image", that is of type UIImage. I'd like to override this variable with my subclass.
My goal is to have a UIimageView that will respond to archivedDataWithRootObject without crashing with the encodeWithCoder message is sent to the variable image. If there is a smarter way to get there, I'm open to suggestions.
Thank you!
[edit] I think one possible route is through some type of casting...However:
UIImage *image = [[UIImage alloc] init];
MLImage *mlImage = [[MLImage alloc] init];
mlIamge = (MLImage*)image;
When I mouse-over mlImage after the second line executes, I can see that it is of type MLImage. However, even with the cast, after the third line executes mlImage becomes a UIImage. What do I need to do to change the type of image to MLImage?
[/edit]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
请注意您的术语。 无法覆盖属性 -
UIImageView
仍将拥有自己的image
属性,不会被替换。 您正在尝试覆盖访问器(-image
和-setImage:
)。你是对的,选角是更好的途径。 但你的例子有一个问题:该强制转换是非法的。 当您执行
[[UIImage alloc] init]
时,您正在创建一个UIImage
类型的对象。 没有什么可以改变这一点,包括你的演员阵容。 强制转换是告诉编译器(在您的情况下)“我知道您认为这个UIImage*
是一个UIImage
,但它实际上是一个MLImage
。” 在这种情况下,这是一个谎言,因为它确实是一个UIImage
- 您就在那里分配了它。不过,您可以做的是将实际的
MLImage
粘贴到UIImageView
的image
属性中。 例如:然后在其他地方:
您将得到您的自定义图像对象。 但它首先必须是一个
MLImage
。Be careful with your terminology. It's not possible to override a property—the
UIImageView
will still have its ownimage
property, it won't be replaced. You're trying to override the accessors (-image
and-setImage:
).And you're right, casting is a better route. But your example has a problem: that cast is illegal. When you do
[[UIImage alloc] init]
, you're creating an object of typeUIImage
. Nothing is going to change that, including your cast. Casting is a way of telling the compiler (in your case) "I know you think that thisUIImage*
is, well, aUIImage
, but it's really aMLImage
." In this case, that's a lie, since it really is aUIImage
—you allocated it right there.What you can do, though, is stick an actual
MLImage
into theimage
property of aUIImageView
. For example:And then somewhere else:
And you'll get back your custom image object. But it has to have been an
MLImage
in the first place.