收到错误:- 无法将 'NotificationItem' 类型的值转换为闭包结果类型 'RTVNotification'



--------RTV通知------------------

import Foundation
///Notification Info
public class RTVNotification: Codable {
public let id: String
public let title: String
public let comment: String
public let urlString: String
public let supportedDevice: String
public let publishStartDateString: String
required public init(from decoder: Decoder) throws {
id = try decoder.decode(CodingKeys.id)
title = try decoder.decode(CodingKeys.title)
comment = try decoder.decode(CodingKeys.comment)
urlString = try decoder.decode(CodingKeys.urlString)
supportedDevice = try decoder.decode(CodingKeys.supportedDevice)
publishStartDateString = try decoder.decode(CodingKeys.publishStartDateString)
}
public func encode(to encoder: Encoder) throws {}
}
// MARK: - CodingKeys
private extension RTVNotification {
enum CodingKeys: String, CodingKey {
case id = "id"
case title = "name"
case comment = "comment"
case urlString = "url"
case supportedDevice = "supported_device"
case publishStartDateString = "show_started_at"
}
}

----------RTV信息列表-------------------------

import Foundation
// MARK: InformationList
public class RTVInformationList: Codable {
public let offset: Int
public let count: Int
public let maxCount: Int
public let notifications: [RTVNotification]
required public init(from decoder: Decoder) throws {
offset = try decoder.decodeInt(CodingKeys.offset)
count = try decoder.decodeInt(CodingKeys.count)
maxCount = try decoder.decodeInt(CodingKeys.maxCount)
notifications = (try? decoder.decode(CodingKeys.notifications)) ?? []
}
public func encode(to encoder: Encoder) throws {}
}
// MARK: - CodingKeys
private extension RTVInformationList {
enum CodingKeys: String, CodingKey {
case offset = "offset"
case count = "count"
case maxCount = "max_count"
case notifications = "information_list"
}
}

---------------通知项目------------------

public class NotificationItem: Codable {
public let id: String
public let title: String
public let comment: String
public let urlString: String
public let supportedDevice: String
public var publishStartDateString: String
init(id: String,
title: String,
comment: String,
urlString: String,
supportedDevice: String,
publishStartDateString: String) {
self.id = id
self.title = title
self.comment = comment
self.urlString = urlString
self.supportedDevice = supportedDevice
self.publishStartDateString = publishStartDateString
}
}
extension NotificationItem {
static func instantiate(with notification: RTVNotification) -> NotificationItem {
NotificationItem(
id: notification.id,
title: notification.title,
comment: notification.comment,
urlString: notification.urlString,
supportedDevice: notification.supportedDevice,
publishStartDateString: notification.publishStartDateString)
}
}

----------------设置视图模型--------------------

public class SettingsViewModel: ViewModel {
var item = [NotificationItem]()
public var fetchedNotifications: Driver<[RTVNotification]> = .empty()
public var apiErrorEvents: Driver<RTVAPIError> = .empty()
public var notificationCount: Driver<Int> = .empty()
public func bindNotificationEvents(with trigger: Driver<Void>) {
let webService: Driver<RTVInformationListWebService> = trigger
.map { RTVInformationListParameters() }
.webService()
let result = webService.request()
apiErrorEvents = Driver.merge(apiErrorEvents, result.error())
notificationCount = result.success().map {$0.informationList.maxCount }
fetchedNotifications = result.success()
.map {$0.informationList.notifications}
---->       .map {$0.map {NotificationItem.instantiate(with: $0)}}
}
}

获取错误:-无法将"NotificationItem"类型的值转换为"RTVNotification"类型的闭包结果我不知道如何转换这些类型。我是编程新手

---->       .map {$0.map {NotificationItem.instantiate(with: $0)}}

fetchedNotifications变量已声明为类型:Driver<[RTV通知]>。$0.map{NotificationItem.instante(with:$0(}现在表示它应该是Driver<[通知项目]>。这行不通。

如果结果应该是Driver<[NotificationItem]>,然后创建一个具有正确类型的新变量。

下面的简短示例:

let numbers: [Int] = [5,6,5]
var newNumbers: [Int] = []
/// This will provide you with an error of: 
/// Cannot convert value of type 'String' to closure result type 'Int'
newNumbers = numbers.map {
return "($0)"
}
// correctNewNumbers is now converted correctly to a [String] 
let correctNewNumbers = numbers.map {
return "($0)"
}

最新更新