出现错误:无法将类型'NotificationItem'的值转换为关闭结果类型'RTVNotification'

时间:2019-11-03 13:31:07

标签: swift rx-swift

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

--------------- NotificationItem ----------------------

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'我不理解如何转换类型。我是编程新手

1 个答案:

答案 0 :(得分:0)

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