在 Swift 中调用实例方法错误消息没有完全匹配 [英] No exact matches in call to instance method error message in Swift

查看:35
本文介绍了在 Swift 中调用实例方法错误消息没有完全匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我以前从来没有得到这个,这个错误信息在 Swift 中是什么意思:

I never get this before, What is the meaning of this error message in Swift:

No exact matches in call to instance method 'dataTask(with:completionHandler:)'

这是我的代码块:

var request: NSMutableURLRequest? = nil
let task = URLSession.shared.dataTask(
            with: request,
            completionHandler: { data, response, error in
                DispatchQueue.main.async(execute: {
                  /// ...
                })
            })
        task.resume()

错误报告

通过feedbackassistant.apple.com 报道:

Bug Report

Reported via feedbackassistant.apple.com:

推荐答案

为什么 Xcode 大喊大叫?

也许消息文本看起来有点不言自明,但仅仅因为 Xcode 没有准确地指向参数本身,所以第一次有点难以想象.

Why Xcode Yelling?

Maybe message text seems a little bit self-explanatory but just because Xcode does not exactly point the parameter itself, a little bit hard to figurate for the first time.

Xcode 大喊大叫,因为该方法希望在方法调用中看到确切的参数类型,就这么简单.

Xcode yelling because the method wants to see exact parameter types on the method call, that easy.

var request: URLRequest? = nil

let task = URLSession.shared.dataTask(
            with: request!,
            completionHandler: { data, response, error in
                DispatchQueue.main.async(execute: {

                })
            })
        task.resume()

刚刚使用了 URLRequest 而不是 NSMutableURLRequest.

假设这是您的用户界面:

Let's assume this is your UI:

        ZStack() {
            Image(systemName: "photo")
                .resizable()
                .aspectRatio(contentMode: .fit)
                .background(Color.green)
                .foregroundColor(Color.white)
                .cornerRadius(12)
            Text(getToday())
                .font(.headline)
            }
        }

这是您在 Text(...) 中调用的方法:

And this is the method that you're calling in the Text(...):

    func getToday() -> Any?
    {
        let now = Date()
        let calendar = Calendar.current
        let components = calendar.dateComponents([.day], from: now)
        return components.day

    }

在上面的示例中,解决方案是将 Any? 更改为 String 类型.

In the example above solution would be changing Any? to a String type.

这是在方法调用中使用错误类型的一般错误消息.这就是为什么我加在这里是为了帮助别人.

This is a general error message for using the wrong type in the method calls. That's why I added here to help others.

我希望这个回答对你们中的一些人有所帮助.

I hope this answer will help some of you guys.

最好的.

这篇关于在 Swift 中调用实例方法错误消息没有完全匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆