gpt4 book ai didi

ios - 如何修复 : Use of undeclared type 'URL' & Use of unresolved identifier 'DispatchQueue' in Swift?

转载 作者:行者123 更新时间:2023-11-28 16:00:09 25 4
gpt4 key购买 nike

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

关于ios - 如何修复 : Use of undeclared type 'URL' & Use of unresolved identifier 'DispatchQueue' in Swift?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41199430/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com