如何修复:使用未声明的类型“网址”&在Swift中使用未解析的标识符'DispatchQueue'?

时间:2016-12-17 14:11:19

标签: ios swift grand-central-dispatch nsurl nscoding

我在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 '使用未声明的类型URL'。

     

请注意,content中的if let url = **content** as? URL {...}声明为:let content: NSSecureCoding?

     

更新:如果我将URL更改为NSURL,我会再收到2个错误:

     

@ Error1.1 if url.absoluteString("youtube.com") || url.absoluteString.contains("youtu.be") {...}):无法调用非函数类型String 的值。

     

@ Error1.2 existingItems.append(url.absoluteString)): AnyObject?类型的值没有成员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 '使用未解析的标识符DispatchQueue'

     

更新:如果我改变了   DispatchQueue.main.asyncAfter(deadline: dispatch_time_t() + Double(Int64(3 * Double(NSEC_PER_SEC))) / Double(NSEC_PER_SEC)) {...}   到   dispatch_after(dispatch_time_t(), Int64((3 * Double(NSEC_PER_SEC)) / Double(NSEC_PER_SEC)) ) {...},我仍然收到错误:参数类型Int64不符合预期类型dispatch_queue_t(又名OS_dispatch_queue

2 个答案:

答案 0 :(得分:1)

我认为您可以尝试以下方法:

  1. 使用NSURL代替网址。

  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)) {`
    

答案 1 :(得分:0)

试图在 Swift 2.2 (使用 Xcode 7 )中使用这段代码时有点奇怪......修复后,另一个出现了。 切换到 Swift 3 (在 Xcode 8 中)可以使一切变得快速而干净。