gpt4 book ai didi

swift - 出现错误 :-Cannot convert value of type 'NotificationItem' to closure result type 'RTVNotification'

转载 作者:行者123 更新时间:2023-11-30 10:33:04 32 4
gpt4 key购买 nike

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

---------------- 设置 View 模型--------------------

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<[RTVNotification]>。 $0.map {NotificationItem.instantiate(with: $0)} 现在表示它应该是 Driver<[NotificationItem]> 类型。这行不通。

如果结果应为 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)"
}

关于swift - 出现错误 :-Cannot convert value of type 'NotificationItem' to closure result type 'RTVNotification' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58680640/

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