Получение ошибки: -Не удалось преобразовать значение типа «NotificationItem» в закрывающий тип результата «RTVNotification». - PullRequest
0 голосов
/ 03 ноября 2019

--------- Уведомление 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 InformationList -----------------------------

   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 голосов
/ 03 ноября 2019
---->       .map {$0.map {NotificationItem.instantiate(with: $0)}}

переменная fetchedNotifications уже объявлена ​​как тип: Driver <[RTVNotification]>. $ 0.map {NotificationItem.instantiate (с: $ 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)"
}
...