2016-12-17 104 views
0

我曾在Xcode的Swift項目中工作過。我努力修復所有的錯誤,但我仍然有2,保持我的項目卡住,你可以看到下面的代碼:@ Error1 and @ Error2。 我希望你能幫助我!先謝謝你!如何解決:在Swift中使用未聲明的類型'URL'和使用未解析標識符'DispatchQueue'?

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

這些類型是在Swift ** 3 **中引入的。你使用哪個版本? –

+0

@MartinR我使用Apple Swift 2.2版。 –

回答

0

試圖在夫特2.2(使用的Xcode 7)這一段代碼鬥爭是有點兒怪異......固定之後,另一個出來。 切換到Swift 3(在Xcode 8)使一切都快速和乾淨。

1

我想你可以嘗試以下方法:

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

** @ Error1 **:如果使用'NSURL'而不是'URL',則會在另外兩處導致錯誤:'if url.absoluteString(「youtube.com」)|| url.absoluteString.contains(「youtu.be」){'和'existingItems.append(url.absoluteString)'。 ** @ Error2 **:更改語法我猜測它的工作原理,但是我仍然得到:_Cannot將類型'Double'的值轉換爲期望的參數類型'UInt64'_。 –

+0

@RazvanJulian你能告訴我在案件1和案件2中的錯誤嗎?我已經更新了我的答案,請檢查 – 3stud1ant3

+0

我剛剛更新了我的問題。請看一下。 –