gpt4 book ai didi

macos - 从 NSPasteBoard 存储和检索自定义对象

转载 作者:行者123 更新时间:2023-12-03 16:16:18 27 4
gpt4 key购买 nike

我有一个属于此类的对象,其中包含以下声明:

标题

@interface MyClassObject : NSObject <NSCopying, NSPasteboardWriting, NSPasteboardReading>

@property (nonatomic, strong) NSArray *children;
@property (nonatomic, assign) NSInteger type;
@property (nonatomic, strong) NSString *name;
@property (nonatomic, strong) id node;

children 保存此类的其他对象,node 保存可以是 NSViewNSImageView 的对象。

我希望能够将这种类型的对象复制到 NSPasteboard 或从 NSPasteboard 粘贴。

我用谷歌搜索过,但解释很模糊。

我应该怎样做才能使此类可复制/可读到 NSPasteboard,或者换句话说,使其符合 NSPasteboardWritingNSPasteboardReading 协议(protocol)?

我对这是如何完成的一无所知,并且像往常一样,Apple 文档并没有什么是相同的。

最佳答案

这是完整的解决方案。

首先要做的就是使类也符合 NSCoding 。在这种情况下,类声明将是

@interface MyClassObject : NSObject <NSCopying, NSPasteboardWriting, NSPasteboardReading, NSCoding>

为了使其与 NSCoding 兼容,您必须实现 encodeWithCoder:initWithCoder:... 在我的例子中:

- (void)encodeWithCoder:(NSCoder *)coder {

[coder encodeObject:@(self.type) forKey:@"type"];
[coder encodeObject:self.name forKey:@"name"];
// converting the node to NSData...
// the object contained in node must be compatible with NSCoding
NSData *nodeData = [NSKeyedArchiver archivedDataWithRootObject:self.node];
[coder encodeObject:nodeData forKey:@"node"];

NSData *childrenData = [NSKeyedArchiver archivedDataWithRootObject:self.children];
[coder encodeObject:childrenData forKey:@"children"];

}

- (id)initWithCoder:(NSCoder *)coder {

self = [super init];

if (self) {

_type = [[coder decodeObjectForKey:@"type"] integerValue];
_name = [coder decodeObjectForKey:@"name"];

NSData *nodeData = [coder decodeObjectForKey:@"node"];
_efeito = [NSKeyedUnarchiver unarchiveObjectWithData:nodeData];

NSData *childrenData = [coder decodeObjectForKey:@"children"];
_children = [NSKeyedUnarchiver unarchiveObjectWithData:childrenData];
_parent = nil; // I want this to be nil when the object is recreated
}

要使该类与 NSPasteboard 一起使用,您必须添加以下 4 个方法:

-(id)initWithPasteboardPropertyList:(id)propertyList ofType:(NSString *)type {
return [NSKeyedUnarchiver unarchiveObjectWithData:propertyList];
}


+(NSArray *)readableTypesForPasteboard:(NSPasteboard *)pasteboard {
// I am using the bundleID as a type
return @[[[NSBundle mainBundle] bundleIdentifier]];
}

- (NSArray *)writableTypesForPasteboard:(NSPasteboard *)pasteboard {
// I am using the bundleID as a type
return @[[[NSBundle mainBundle] bundleIdentifier]];
}


- (id)pasteboardPropertyListForType:(NSString *)type {
// I am using the bundleID as a type
if(![type isEqualToString:[[NSBundle mainBundle] bundleIdentifier]]) {
return nil;
}

return [NSKeyedArchiver archivedDataWithRootObject:self];
}

然后,在您实现添加的复制和粘贴的类上:

- (void)copy:(id)sender {

NSPasteboard *pasteBoard = [NSPasteboard generalPasteboard];
[pasteBoard clearContents];

NSArray *copiedObjects = @[theObjectYouWantToCopyToThePasteboard];
[pasteBoard writeObjects:copiedObjects];

}

- (void)paste:sender {

NSPasteboard *pasteboard = [NSPasteboard generalPasteboard];
NSArray *classArray = @[[LayerObject class]];
NSDictionary *options = [NSDictionary dictionary];

BOOL ok = [pasteboard canReadItemWithDataConformingToTypes:@[[[NSBundle mainBundle] bundleIdentifier]]];

if (ok) {
NSArray *objectsToPaste = [pasteboard readObjectsForClasses:classArray options:options];
MyObjectClass *object = [objectsToPaste objectAtIndex:0];

// object is the one you have copied to the pasteboard
// now you can do whatever you want with it.
// add the code here.
}
}

关于macos - 从 NSPasteBoard 存储和检索自定义对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28656562/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com