覆盖类变量,或者至少在Objective-C/Cocoa中是变量类型 [英] override classes variable, or at least variable type in objective-c / cocoa
问题描述
我的困境如下:
我有一个UIImage的自定义子类(我添加了一些序列化方法,即initWithCoder,encodeWithCoder),我想将我的自定义子类作为变量应用于UIImageView,或者至少是UIImageView的子类.
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.
您知道,UIImageView有一个名为"image"的变量,其类型为UIImage.我想用我的子类覆盖此变量.
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.
我的目标是拥有一个UIimageView,它将对archivedDataWithRootObject做出响应,而不会因将encodeWithCoder消息发送到变量图像而崩溃.如果有更聪明的方法可以到达那里,我欢迎您提出建议.
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.
谢谢!
[edit]我认为一种可能的途径是通过某种类型的铸造...但是:
[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;
当我在第二行执行后将鼠标移到mlImage上时,我可以看到它的类型为MLImage.但是,即使进行了强制转换,在执行第三行之后,mlImage也会成为UIImage.我需要怎么做才能将图像类型更改为MLImage? [/edit]
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]
推荐答案
请谨慎使用术语. 覆盖属性是不可能的-UIImageView
仍将具有其自己的image
属性,不会被替换.您正在尝试覆盖访问器(-image
和-setImage:
).
Be careful with your terminology. It's not possible to override a property—the UIImageView
will still have its own image
property, it won't be replaced. You're trying to override the accessors (-image
and -setImage:
).
您是对的,投射是更好的方法.但是您的示例存在一个问题:强制转换是非法的.当您执行[[UIImage alloc] init]
时,您正在创建UIImage
类型的对象.包括您的演员在内,一切都不会改变.强制转换是一种告诉编译器的方法(以您的情况为例):我知道您认为此UIImage*
是UIImage
,但实际上是MLImage
".在这种情况下,这是一个谎言,因为它实际上是UIImage
-您在此处分配了它.
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 type UIImage
. 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 this UIImage*
is, well, a UIImage
, but it's really a MLImage
." In this case, that's a lie, since it really is a UIImage
—you allocated it right there.
您可以 进行的操作是将实际的MLImage
粘贴到UIImageView
的image
属性中.例如:
What you can do, though, is stick an actual MLImage
into the image
property of a UIImageView
. For example:
MLImage *myImage = [[MLImage alloc] init];
myView.image = myImage;
然后在其他地方:
MLImage *myImage = (MLImage *)(myView.image);
然后您将取回自定义图像对象.但这首先必须是MLImage
.
And you'll get back your custom image object. But it has to have been an MLImage
in the first place.
这篇关于覆盖类变量,或者至少在Objective-C/Cocoa中是变量类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!