error-handling - 'String' 不符合 Swift 2.0 中的协议(protocol) 'CollectionType' 错误

标签 error-handling swift2 xcode7

我刚刚下载了 XCode Beta 7 并收到错误“类型 'String' 不符合协议(protocol) 'CollectionType'”。这是我第一次尝试编码,所以我不知道如何解决这个问题。谢谢!!!

//the Pasteboard is nil if full access is not granted
            let pbWrapped: UIPasteboard? = UIPasteboard.generalPasteboard()
            if let pb = pbWrapped {
                var type = UIPasteboardTypeListImage[0] as! String
                if (count(type) > 0) && (image != nil) {
                    pb.setData(UIImagePNGRepresentation(image!)!, forPasteboardType: type)
                    var readDataWrapped: NSData? = pb.dataForPasteboardType(type)
                    if let readData = readDataWrapped {
                        var readImage = UIImage(data: readData, scale: 2)
                        print("\(image) == \(pb.image) == \(readImage)")
                    }
                }
            }

最佳答案

将其更改为:

type.characters.count

然后您的代码应为:
//the Pasteboard is nil if full access is not granted
        let pbWrapped: UIPasteboard? = UIPasteboard.generalPasteboard()
        if let pb = pbWrapped {
            var type = UIPasteboardTypeListImage[0] as! String
            if (type.characters.count > 0) && (image != nil) {
                pb.setData(UIImagePNGRepresentation(image!)!, forPasteboardType: type)
                var readDataWrapped: NSData? = pb.dataForPasteboardType(type)
                if let readData = readDataWrapped {
                    var readImage = UIImage(data: readData, scale: 2)
                    print("\(image) == \(pb.image) == \(readImage)")
                }
            }
        }

关于error-handling - 'String' 不符合 Swift 2.0 中的协议(protocol) 'CollectionType' 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31657451/

相关文章:

ios - 如何在 BarCharts IOS 图表中创建多个数据集

json - 如何将数据序列化为 JSON 对象

swift - HTTP 加载失败(kCFStreamErrorDomainSSL,-9806(IOS9)

xcode - 从 Xcode 7 中的覆盖率统计数据中排除代码

vba - 为什么某些 VBA 错误不触发错误处理?

r - 如何在条形图误差条中合并SE而不是SD?另外,如何更改x轴组的顺序

Swift:使用已知类型语法的闭包

app-store-connect - 从 iTunesconnect 移除 tvOS 构建

c# - DebuggerDisplay 在运行时解析为字符串

error-handling - 在Swift 2.1中,如何获取抛出的NSError?