【问题标题】:'String' does not conform to protocol 'CollectionType' Error in Swift 2.0'String' 不符合 Swift 2.0 中的协议 'CollectionType' 错误
【发布时间】:2015-07-27 15:41:52
【问题描述】:

我刚刚下载了 XCode Beta 7 并收到错误“类型 'String' 不符合协议 '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)")
                    }
                }
            }

【问题讨论】:

  • 哪一行给出了错误?
  • @PhillipMills if (count(type) > 0) && (image != nil) {
  • 请参阅stackoverflow.com/questions/24037711/get-the-length-of-a-string 尤其是关于 Swift 2 已接受答案更改的说明。
  • @PhillipMills,谢谢!但是,我仍然对如何实现这一点有点困惑(我是编码应用程序的菜鸟,我几天前才开始学习)。
  • 按照那个答案——我没试过——建议是:if (type.characters.count > 0) && (image != nil) {

标签: error-handling swift2 xcode7


【解决方案1】:

改成:

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)")
                }
            }
        }

【讨论】:

    【解决方案2】:

    只是关于从哪里开始了解此问题的建议...https://developer.apple.com/swift/blog/?id=30 Swift 2 中的字符串

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-09-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多