在 Swift 中 - 表达式类型在没有更多上下文的情况下不明确
In Swift - Type of expression is ambiguous without more context
func resetInfoPopUp() {
if let resetInfo = self.viewModel.getResetInfo()
{
self.presentNotificationWithoutActionPopUpDelegate(title: resetInfo.title,
message: resetInfo.description,
infoImage: resetInfo.imageFileName,
delegate: self)
}
}
我收到此错误 “表达式类型不明确,没有更多上下文”
这是我从 ViewController class.
调用的函数
func presentNotificationWithoutActionPopUpDelegate(title: NSAttributedString,
message: NSAttributedString,
infoImage: UIImage? = nil,
delegate: PopUpActionDelegate? = nil) {
let notificationViewController = NotificationViewWithoutActionViewController(title: title,
message: message, infoImage: infoImage)
notificationViewController.gotItActionDelegate = delegate
self.present(notificationViewController, animated: false, completion: nil)
}
NotificationViewWithoutActionViewController
是一个 viewcontroller class 声明弹出函数的地方。
@IBAction func acknowledgeAction() {
gotItActionDelegate?.gotItButtonAction()
self.dismiss(animated: true, completion: nil)
}
gotItButtonAction
是协议中定义的函数。
Check the image
struct ResetInfo
{
var title: String
var description: String
var imageFileName: UIImage
}
您的问题是您向 presentNotificationWithoutActionPopUpDelegate
函数传递了错误的参数。 String
与 NSAttributedString
不同。
要解决此问题,您可以这样做:
// Extension for convenient String conversion to NSAttributedString
extension String {
var attributed: NSAttributedString {
NSAttributedString(string: self)
}
}
并像这样使用它:
let info = ResetInfo(...)
presentNotificationWithoutActionPopUpDelegate(
title: info.title.attributed,
message: info.description.attributed,
...
)
func resetInfoPopUp() {
if let resetInfo = self.viewModel.getResetInfo()
{
self.presentNotificationWithoutActionPopUpDelegate(title: resetInfo.title,
message: resetInfo.description,
infoImage: resetInfo.imageFileName,
delegate: self)
}
}
我收到此错误 “表达式类型不明确,没有更多上下文”
这是我从 ViewController class.
调用的函数func presentNotificationWithoutActionPopUpDelegate(title: NSAttributedString,
message: NSAttributedString,
infoImage: UIImage? = nil,
delegate: PopUpActionDelegate? = nil) {
let notificationViewController = NotificationViewWithoutActionViewController(title: title,
message: message, infoImage: infoImage)
notificationViewController.gotItActionDelegate = delegate
self.present(notificationViewController, animated: false, completion: nil)
}
NotificationViewWithoutActionViewController
是一个 viewcontroller class 声明弹出函数的地方。
@IBAction func acknowledgeAction() {
gotItActionDelegate?.gotItButtonAction()
self.dismiss(animated: true, completion: nil)
}
gotItButtonAction
是协议中定义的函数。
Check the image
struct ResetInfo
{
var title: String
var description: String
var imageFileName: UIImage
}
您的问题是您向 presentNotificationWithoutActionPopUpDelegate
函数传递了错误的参数。 String
与 NSAttributedString
不同。
要解决此问题,您可以这样做:
// Extension for convenient String conversion to NSAttributedString
extension String {
var attributed: NSAttributedString {
NSAttributedString(string: self)
}
}
并像这样使用它:
let info = ResetInfo(...)
presentNotificationWithoutActionPopUpDelegate(
title: info.title.attributed,
message: info.description.attributed,
...
)