简体   繁体   English

如何将符合NSCoding的对象克隆到其子类中?

[英]How can I clone a NSCoding compliant object into a subclass of it?

I have a custom subclass of NSPopUpButtonCell so that I can overwrite its drawBezelWithFrame:inView: method. 我有一个NSPopUpButtonCell的自定义子类,以便可以覆盖其drawBezelWithFrame:inView:方法。

Currently, I have to create a fresh instance using initTextCell:pullsDown: and then copy all its properties by hand. 当前,我必须使用initTextCell:pullsDown:创建一个新实例,然后手动复制其所有属性。 That's rather tedious and error-prone as I may be missing some properties. 这相当乏味且容易出错,因为我可能会缺少一些属性。

I wonder if I can use initWithCoder: for this task instead. 我想知道是否可以使用initWithCoder:代替此任务。 I imagine I should be able to file the data from the existing NSPopUpButtonCell instance into an NSCoder object, eg into NSKeyedArchiver , and then file that data back into my NSPopUpButtonCell subclass. 我想我应该能够将现有NSPopUpButtonCell实例中的数据NSPopUpButtonCellNSCoder对象中,例如,导入NSKeyedArchiver ,然后将该数据NSPopUpButtonCell回我的NSPopUpButtonCell子类中。 But I can't figure out how to accomplish that. 但是我不知道该怎么做。

If all your subclass does is override methods, ie it does not add additional fields, then you can swizzle the class of your original NSPopupButtonCell , or a copy of it, so it becomes and instance of your subclass. 如果您的子类所做的全部是重写方法,即它没有添加其他字段,那么您可以修改原始NSPopupButtonCell的类或它的副本,使其成为您的子类的实例。

Apple implements KVO using this technique, in that case using a dynamically generated subclass. Apple使用此技术实现KVO,在这种情况下,使用动态生成的子类。 If your case the subclass is static, making it a lot easier, the outline of the code is: 如果您的情况是子类是静态的,这使它变得容易得多,则代码概述为:

object_setClass(<an NSPopupButtonCell instance>,
                [<your subclass> class]);

If you can safely change the class on the original instance then this involves no object creation or copying at all. 如果可以安全地更改原始实例上的类,则根本不涉及对象创建或复制。

Remember you can only do this if the subclass changes behaviour only . 请记住, 仅当子类仅更改行为时才可以执行此操作

For another explanation see this answer . 有关其他说明,请参见此答案

HTH 高温超导

Looks like I just used the wrong functions before. 看来我以前使用过错误的功能。 The solition is pretty straight-forward. 隔离非常简单。

This code duplicates the properties of a NSPopUpButtonCell into a subclass of it, so that I can overwrite its draw... methods: 此代码将NSPopUpButtonCell的属性复制到其子类中,以便我可以覆盖其draw ...方法:

@interface MyPopup : NSPopUpButton
@end

@implementation MyPopup
- (instancetype)initWithCoder:(NSCoder *)coder {
    self = [super initWithCoder:coder];
    if (self) {
        // Set up the archiver
        NSMutableData *data = [NSMutableData data];
        NSKeyedArchiver *arch = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
        // Archive the cell's properties
        [self.cell encodeWithCoder:arch];
        [arch finishEncoding];
        // Set up the unarchiver
        NSKeyedUnarchiver *ua = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
        // Create a subclass of the cell's class, using the same properties
        MyCell *newCell = [[MyCell alloc] initWithCoder:ua];
        // Assign the new subclass object as the NSButton's cell.
        self.cell = newCell;
    }
    return self;
}

Another, cleaner, way to use a custom NSButtonCell subclass is shown here: Using full width of NSPopupButton for text, no arrows 此处显示了另一种使用自定义NSButtonCell子类的更简洁的方法使用NSPopupButton的全角文本,没有箭头

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将子类添加到NSTableCellView? - How can I add subclass to NSTableCellView? 如何将自定义NSWindow子类转换为自定义NSPanel? - How can I make a custom NSWindow subclass into a custom NSPanel? 如何在OS X上安装SSL证书,以便从Github克隆项目? - How do I install SSL certificates on OS X so I can clone projects from Github? 如何为面向查看器的应用程序创建NSDocument(子类)实例? - How can I create NSDocument (subclass) instances for a viewer-oriented app? 如何使用正确的公钥权限克隆我的 git 存储库? - How can I clone my git repository with the correct public key permission? 如何在API结果中调用子类? - How do I call a subclass in an API result? 我似乎无法解决valueForUndefinedKey:]的错误:whereto类与键值编码不兼容 - I can't seem to resolve error for valueForUndefinedKey:]: whereto class is not key value coding-compliant 我可以在NSOperation子类中使用MOC的performBlock:吗? - Can I use MOC's performBlock: inside an NSOperation subclass? 为什么我不能克隆 Git 中的任何存储库? - Why can't I clone any repository in Git? 如何使对象成为窗口的委托? - How can I make an object a delegate of a window?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM