覆盖类变量,或者至少覆盖 Objective-C / cocoa 中的变量类型

发布于 2024-07-30 08:03:27 字数 674 浏览 3 评论 0原文

我的困境如下:

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

走过海棠暮 2024-08-06 08:03:27

请注意您的术语。 无法覆盖属性 - UIImageView 仍将拥有自己的 image 属性,不会被替换。 您正在尝试覆盖访问器-image-setImage:)。

你是对的,选角是更好的途径。 但你的例子有一个问题:该强制转换是非法的。 当您执行[[UIImage alloc] init]时,您正在创建一个UIImage类型的对象。 没有什么可以改变这一点,包括你的演员阵容。 强制转换是告诉编译器(在您的情况下)“我知道您认为这个 UIImage* 是一个 UIImage,但它实际上是一个 MLImage。” 在这种情况下,这是一个谎言,因为它确实是一个 UIImage - 您就在那里分配了它。

不过,您可以做的是将实际的MLImage粘贴到UIImageViewimage属性中。 例如:

MLImage *myImage = [[MLImage alloc] init];
myView.image = myImage;

然后在其他地方:

MLImage *myImage = (MLImage *)(myView.image);

您将得到您的自定义图像对象。 但它首先必须是一个 MLImage

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:).

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.

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;

And then somewhere else:

MLImage *myImage = (MLImage *)(myView.image);

And you'll get back your custom image object. But it has to have been an MLImage in the first place.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文