Wednesday, August 9, 2023
HomeiOS Developmentios - Anticipated to decode Array however discovered a dictionary as an...

ios – Anticipated to decode Array however discovered a dictionary as an alternative.”, underlyingError: nil


I’m new for this present venture, earlier than extra the three individuals labored on this venture. In my venture I’ve received Under Problem, I get this error each time I attempt to fetch info from it

“typeMismatch(Swift.Array, Swift.DecodingError.Context(codingPath: [], debugDescription: “Anticipated to decode Array however discovered a dictionary as an alternative.”, underlyingError: nil))”

kindly examine and let me assist to repair it.

Right here I shared my mannequin class and my coding features.

Under is present Json,

[
{
"enabled": true,
"connectionSpeed": 0,
"channel": "string",
"connectedDevices": 0,
"id": "string",
"ssid": "string",
"accessPoint": "string",
"password": "string",
"visible": true,
"isGuest": true,
"currentChannel": "string",
"channelWidth": "auto",
"frequencyBand": "2_4_GHZ",
"securityMode": "NONE",
}}]

Under is present Json Mannequin Class,

import Basis


struct WirelessNetwork: Codable, Equatable {
let id: String
var enabled: Bool
let isGuest: Bool
let isSplit: Bool
let connectedDevices: Int
var ssid: String
var password: String?
var securityMode: SecurityModeType?
var seen: Bool
let frequencyBand: FrequencyBand

let connectionSpeed: Int?
let currentChannel: String?
var autoDisable: AutoDisableTimeout?
var bandSteering: BandSteering?
var channel: String?
var channelWidth: ChannelWidth?
var wps: WPS?
let accessPoint: String?

enum CodingKeys: String, CodingKey {
    case connectionSpeed
    case currentChannel
    case id
    case isSplit
    case isGuest
    case autoDisable
    case bandSteering
    case channel
    case frequencyBand
    case channelWidth
    case enabled
    case password
    case securityMode
    case ssid
    case seen
    case wps
    case accessPoint
    case connectedDevices
}

init(connectionSpeed: Int?, currentChannel: String?, id: String, isSplit: Bool, isGuest: Bool, autoDisable: AutoDisableTimeout?, bandSteering: BandSteering?, channel: String?, frequencyBand: FrequencyBand, channelWidth: ChannelWidth?, enabled: Bool, password: String?, securityMode: SecurityModeType?, ssid: String, seen: Bool, wps: WPS?, accessPoint: String?, connectedDevices: Int) {
    self.connectionSpeed = connectionSpeed
    self.currentChannel = currentChannel
    self.id = id
    self.isSplit = isSplit
    self.isGuest = isGuest
    self.autoDisable = autoDisable
    self.bandSteering = bandSteering
    self.channel = channel
    self.frequencyBand = frequencyBand
    self.channelWidth = channelWidth
    self.enabled = enabled
    self.password = password
    self.securityMode = securityMode
    self.ssid = ssid
    self.seen = seen
    self.wps = wps
    self.accessPoint = accessPoint
    self.connectedDevices = connectedDevices
}

init(from decoder: Decoder) throws {
    let container = strive decoder.container(keyedBy: CodingKeys.self)
    connectionSpeed = strive container.decodeIfPresent(Int.self, forKey: .connectionSpeed)
    currentChannel = strive container.decodeIfPresent(String.self, forKey: .currentChannel)
    id = strive container.decode(String.self, forKey: .id)
    isGuest = strive container.decode(Bool.self, forKey: .isGuest)
    isSplit = strive container.decodeIfPresent(Bool.self, forKey: .isSplit) ?? true
    autoDisable = strive container.decodeIfPresent(AutoDisableTimeout.self, forKey: .autoDisable)
    bandSteering = strive container.decodeIfPresent(BandSteering.self, forKey: .bandSteering)
    channel = strive container.decodeIfPresent(String.self, forKey: .channel)
    frequencyBand = strive container.decode(FrequencyBand.self, forKey: .frequencyBand)
    channelWidth = strive container.decodeIfPresent(ChannelWidth.self, forKey: .channelWidth) ?? .auto
    enabled = strive container.decode(Bool.self, forKey: .enabled)
    password = strive container.decodeIfPresent(String.self, forKey: .password)
    securityMode = strive container.decodeIfPresent(SecurityModeType.self, forKey: .securityMode)
    ssid = strive container.decode(String.self, forKey: .ssid)
    seen = strive container.decode(Bool.self, forKey: .seen)
    wps = strive container.decodeIfPresent(WPS.self, forKey: .wps)
    accessPoint = strive container.decodeIfPresent(String.self, forKey: .accessPoint)
    connectedDevices = strive container.decode(Int.self, forKey: .connectedDevices)
}

}

Under I shared the Json Parsing community Supervisor class,

import Basis
import Cara

class WirelessNetworksManager: ItemFetching {

non-public let service: BackendService
inner let localRepository: LocalRepository<WirelessNetwork>
inner let remoteRepository: RemoteRepository<WirelessNetwork>

non-public let product: Product

// MARK: - Initialization

init(service: BackendService, product: Product) {
    self.service = service
    self.localRepository = LocalRepository<WirelessNetwork>()
    self.remoteRepository = RemoteRepository<WirelessNetwork>(service: service)
    
    self.product = product
}

// MARK: - Repository

func fetchItems(cached: Bool, handler: @escaping (Consequence<[WirelessNetwork], Error>) -> Void) {
    // briefly added this (examine & product variable), telco would not have help for WirelessNetworksSimpleRequest()
    let request: Request = product.sort == .telco ? WirelessNetworksRequest() : WirelessNetworksSimpleRequest()
    fetchItems(with: request, cached: cached, handler: handler)
}

// MARK: - Public

func replace(_ merchandise: WirelessNetwork, handler: @escaping (Consequence<Bool, Error>) -> Void) {
    updateItem(with: UpdateWirelessNetworkRequest(id: merchandise.id, replace: merchandise), handler: handler)
}

func replace(autoDisableTimer: AutoDisableTimeout, networkId: String, handler: @escaping (Consequence<AutoDisableTimeout, Error>) -> Void) {
    let request = UpdateAutoDisableTimerRequest(id: networkId, replace: autoDisableTimer)
    let serializer = CodableSerializer<AutoDisableTimeout>()
    service.execute(request, with: serializer) { response in
        change response {
        case .success(let autoDisableTimeOut):
            guard let autoDisableTimeOut = autoDisableTimeOut else { return }
            self.localRepository.clearCache()
            handler(.success(autoDisableTimeOut))
        case .failure(let error):
            handler(.failure(error))
        }
    }
}

func fetchDetails(id networkId: String, handler: @escaping (Consequence<WirelessNetwork, Error>) -> Void) {
    let request = WirelessNetworkDetailRequest(networkId: networkId)
    let serializer = CodableSerializer<WirelessNetwork>()
    service.execute(request, with: serializer) { response in
        change response {
        case .success(let wirelessNetwork):
            guard let wirelessNetwork = wirelessNetwork else { return }
            handler(.success(wirelessNetwork))
        case .failure(let error):
            handler(.failure(error))
        }
    }
}}

Under I shared the code for Merchandise Fetching Base class for Json Parsing.

import Basis
import Cara

protocol ItemFetching {
associatedtype T: Equatable&Codable
var localRepository: LocalRepository<T> { get }
var remoteRepository: RemoteRepository<T> { get }
func fetchItems(cached: Bool, handler: @escaping (Consequence<[T], Error>) -> Void)
}

extension ItemFetching {

func fetchItems(with request: Request, cached: Bool, handler: @escaping (Consequence<[T], Error>) -> Void) {
    if cached, let cachedItems = localRepository.fetchCachedItems() {
        handler(.success(cachedItems))
    } else {
        fetchRemoteItems(request: request, handler: handler)
    }
}

non-public func fetchRemoteItems(request: Request, handler: @escaping (Consequence<[T], Error>) -> Void) {
    remoteRepository.fetchItems(request: request) { response in
        change response {
        case .success(let objects):
            self.localRepository.updateCache(objects)
            print("*** Fetch Distant Information ***", objects)
            handler(.success(objects))
        case .failure(let error):
            handler(.failure(error))
        }
    }
}

func updateItem(with request: Request, handler: @escaping (Consequence<Bool, Error>) -> Void) {
    remoteRepository.replace(request: request) { response in
        change response {
        case .success(let consequence):
            self.localRepository.clearCache()
            print("*** Replace Distant Information is ***", consequence)
            handler(.success(consequence))
        case .failure(let error):
            print("*** Replace Distant Information error is ***", error)
            handler(.failure(error))
        }
    }
}

func createItem(with request: Request, handler: @escaping (Consequence<Bool, Error>) -> Void) {
    remoteRepository.create(request: request) { response in
        change response {
        case .success(let consequence):
            self.localRepository.clearCache()
            handler(.success(consequence))
        case .failure(let error):
            handler(.failure(error))
        }
    }
}}

Right here I shared the Up to date JSON response from Api. Now want to vary total mannequin and Merchandise fetching based mostly on this up to date one,

[
{
"networks": [
  {
    "connectedDevices": 0,
    "id": "string",
    "enabled": true,
    "visible": true,
    "isGuest": true,
    "ssid": "string",
    "bssid": "string",
    "frequency_bands": "string",
    "isBackhaul": true,
    "frequencyBand": "2_4_GHZ",
    "password": "string",
    "securityMode": "NONE",
    "isSplit": true
  }
]}]



Supply hyperlink

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments