参数类型“customClass.Type"不符合预期类型“NSItemProviderWriting" [英] Argument type 'customClass.Type' does not conform to expected type 'NSItemProviderWriting'

查看:33
本文介绍了参数类型“customClass.Type"不符合预期类型“NSItemProviderWriting"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

iOS 11.x Swift 4

iOS 11.x Swift 4

尝试实现一个自定义类以使用新的拖放协议,并且需要一些超级编码器的帮助.我创建了这个类.

Trying to implement a custom class for using the new drop and drag protocol and need some super-coder help. I created this class.

import UIKit
import MobileCoreServices

class CustomClass: NSObject, NSItemProviderWriting, NSItemProviderReading {

var image2D:Data?

static var readableTypeIdentifiersForItemProvider = [kUTTypeData as String]

static func object(withItemProviderData data: Data, typeIdentifier: String) throws -> Self {
    return try self.init(itemProviderData: data, typeidentifier: kUTTypeData as String)
}

required init(itemProviderData data: Data, typeidentifier: String) throws {
    super.init() 
    image2D = data
}

static var writableTypeIdentifiersForItemProvider = [kUTTypeData as String]

func loadData(withTypeIdentifier typeIdentifier: String, forItemProviderCompletionHandler completionHandler: @escaping (Data?, Error?) -> Void) -> Progress? {
    let data2E = image2D
    completionHandler(data2E, nil)
    return nil
}

}

它编译,看起来好吗?然后我用这个调用引用它.

It compiles, looks ok? Then I reference it with this call.

func dragInteraction(_ interaction: UIDragInteraction, itemsForBeginning session: UIDragSession) -> [UIDragItem] {
         let itemProvider = NSItemProvider(object: CustomClass)
        let dragItem = UIDragItem(itemProvider: itemProvider)
        return [dragItem]
    }

我收到错误消息...参数类型CustomClass.Type"不符合预期类型NSItemProviderWriting"...

And I got the error message ... Argument type 'CustomClass.Type' does not conform to expected type 'NSItemProviderWriting'...

但除此之外,似乎无法在这里、那里或任何地方找到更多关于如何继续前进的线索.

But beyond that cannot seem to find any more clues here, there or anywhere as to how to move forward on this.

我实现了其中一个人的一个侧面,它奏效了......

A side point I implemented one of these guys, it worked...

  itemProvider.registerDataRepresentation(forTypeIdentifier: kUTTypeJPEG as String, visibility: .all)

我实现了其中一个,它也很有效......

And I implemented one of these guys, it worked too...

itemProvider.registerFileRepresentation(forTypeIdentifier: kUTTypeJPEG as String, fileOptions: [.openInPlace], visibility: .all)

所以我在想,代码不可能是错的……当然……

So I am thinking, the code cannot be THAT wrong... surely...

推荐答案

错误信息是正确的,你的行:

The error message is correct and your line:

let itemProvider = NSItemProvider(object: customClass)

由于所述原因不正确.object 参数需要一个符合 NSItemProviderWriting 协议的类的实例.但是你传入的是一个实际的类,而不是类的实例.

is incorrect for the reason stated. The object parameter is expecting an instance of some class that conforms to the NSItemProviderWriting protocol. But you are passing in an actual class, not an instance of the class.

customClass 替换为您的 customClass 的实际实例.如果此方法在您的 customClass 中,则传递 self.

Replace customClass with an actual instance of your customClass. If this method is inside your customClass, then pass self.

let itemProvider = NSItemProvider(object: self)

顺便说一句 - 如果您遵循标准命名约定,这将不那么令人困惑.类和结构名称应以大写字母开头.变量和方法名称以小写字母开头.所以你的 customClass 应该被命名为 CustomClass.

BTW - this would be less confusing if your followed standard naming conventions. Class and struct names should begin with uppercase letters. Variable and method names start with lowercase letters. So your customClass should be named CustomClass.

这篇关于参数类型“customClass.Type"不符合预期类型“NSItemProviderWriting"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆