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

3.8k views Asked by At

I worked on a Swift project in Xcode. I struggled to fix all errors, but I still have 2, keeping my project stuck, as you can see in the code below: @Error1 and @Error2. I hope you could help me! Thank you in advance!

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).

2

There are 2 answers

0
Razvan Julian On BEST ANSWER

Trying to struggle with this piece of code in Swift 2.2 (using Xcode 7) is kinda weird... After you fixed one, another comes out. Switching to Swift 3 (in Xcode 8) makes everything fast and clean.

3
3stud1ant3 On

I think you can try the following:

  1. Use NSURL instead of URL.

  2. Use:

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

    instead of:

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