如何修复:在 Swift 中使用未声明的类型 'URL' 和使用未解析的标识符 'DispatchQueue'?

How to fix: Use of undeclared type 'URL' & Use of unresolved identifier 'DispatchQueue' in Swift?

我在 Xcode 从事 Swift 项目。我努力修复所有错误,但我仍然有 2 个错误,让我的项目卡住了,正如您在下面的代码中看到的:@Error1@Error2. 我希望你能帮助我!提前致谢!

override func viewDidLoad() {
    super.viewDidLoad()

    //Getting the URL of the item
    for item in self.extensionContext!.inputItems {
        if let item = item as? NSExtensionItem {
            for itemProvider in item.attachments! {
                //Going through each item in each input item
                if let itemProvider = itemProvider as? NSItemProvider {
                    if itemProvider.hasItemConformingToTypeIdentifier(kUTTypeURL as String) {
                        //If the item contains a URL 
                        itemProvider.loadItemForTypeIdentifier(kUTTypeURL as String, options: nil, completionHandler: {(content, error) -> Void in

                         dispatch_async(dispatch_get_main_queue()){

                                if let url = content as? URL /*@Error1*/ {

                                    if url.absoluteString("youtube.com") || url.absoluteString.contains("youtu.be") { //@Error1.1
                                        self.setTitleOfTextView("Video")

                                        //Just in case the app isn't running in the background, write the URL to the shared NSUserDefaults
                                        var existingItems = Constants.sharedDefaults.valueForKey(Constants.videosToAdd)// sharedDefaults.value(forKey: Constants.videosToAdd) as! [String]
                                        existingItems.append(url.absoluteString) //@Errror1.2
                                        //Constants.sharedDefaults.set(existingItems, forKey: Constants.videosToAdd)
                                        //Constants.sharedDefaults.synchronize()
                NSUserDefaults.standardUserDefaults().setInteger(yourScore, forKey: "highScore")
                                         NSUserDefaults.standardUserDefaults().synchronize()

                                        //Passing URL    
                                       self.wormhole.passMessageObject(url.absoluteString as NSCoding?, identifier: "youTubeUrl")


                                    return
                                    }
                                }

                                self.setTitleOfTextView("Invalid URL.")
                            }
                        })
                    }
                }
            }
        }
    }
}

@Error1: 'Use of undeclared type URL'.

Please, note that content from if let url = **content** as? URL {...}, is declared as: let content: NSSecureCoding?.

Update: If I change the URL into NSURL I would get 2 more errors to:

@Error1.1 (if url.absoluteString("youtube.com") || url.absoluteString.contains("youtu.be") {...}): Cannot call value of non-function type String.

@Error1.2 ( existingItems.append(url.absoluteString)): Value of type AnyObject? has no member append.

func setTitleOfTextView(_ text: String) {
    self.mainLabel.text = text

    DispatchQueue.main.asyncAfter(deadline: dispatch_time_t() + Double(Int64(3 * Double(NSEC_PER_SEC))) / Double(NSEC_PER_SEC)) /*@Error2*/ {

        UIView.animate(withDuration: 0.25, animations: {
            self.view.alpha = 0
        }, completion: { completed in
            self.extensionContext?.completeRequest(returningItems: nil) { completed in
                self.dismiss(animated: true, completion: nil)
            }
        })
    }
}

@Error2: 'Use of unresolved identifier DispatchQueue'.

Update: If I change DispatchQueue.main.asyncAfter(deadline: dispatch_time_t() + Double(Int64(3 * Double(NSEC_PER_SEC))) / Double(NSEC_PER_SEC)) {...} into dispatch_after(dispatch_time_t(), Int64((3 * Double(NSEC_PER_SEC)) / Double(NSEC_PER_SEC)) ) {...}, I still get the error: Argument type Int64 does not conform to expected type dispatch_queue_t (aka OS_dispatch_queue).

我想你可以尝试以下方法:

  1. 使用 NSURL 而不是 URL。

  2. 使用:

    dispatch_after(deadline:dispatch_time_t() +Int64((3 * Double(NSEC_PER_SEC)) / Double(NSEC_PER_SEC)), dispatch_get_main_queue()) {
    

    而不是:

    DispatchQueue.main.asyncAfter(deadline: dispatch_time_t() + Double(Int64(3 * Double(NSEC_PER_SEC))) / Double(NSEC_PER_SEC)) {`
    

尝试在 Swift 2.2 中处理这段代码(使用 Xcode 7)是有点奇怪...在你修好了一个之后,另一个就出来了。 切换到 Swift 3(在 Xcode 8 中)使一切变得快速和干净。