TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

SOLVED: No exact matches in call to instance method 'append'

Forums > SwiftUI

So i've been trying to append an element to an array and am getting the error i mentioned in the title. Everything i've read says its from a mismatch of types but I can not figure out where this mismatch is coming from.

Here is where the warning is coming from. newValue is a BTDevice? and i'm unwrapping to BTDevice so that it may be added to $connectedDevices which is an array of BTDevice

.onChange(of: listener.connectedPublished) { newValue in
                                        guard let device = newValue else {return }
                                        appState.$connectedDevices.append(device) // warning on this line

                                        doneLoading.toggle()
                                        show.toggle()
                                        let bool = isDoneSpeaker
                                        self.isDoneOnboarding = bool
                                    }

Here is the AppState

class AppState: ObservableObject {
    @Published var selectedTab: ContentViewTab = .home
    @Published var connectedDevices = [BTDevice]()
    var devices: [BTDeviceHolderInfo]? {
        get {
            var newArray = [BTDeviceHolderInfo]()
            if let data = UserDefaults.standard.data(forKey: UserDefaults.Key.identifierArray.rawValue) {
                do {
                    let decoder = JSONDecoder()
                    let array = try decoder.decode([BTDeviceHolderInfo].self, from: data)
                    newArray = array
                } catch {
                    return [BTDeviceHolderInfo]()
                }
            }
            return newArray
        }
    }
    @Published var newlyConnectedBass50: BASS50? 

}

Here is where listener.connectedPublished is set

func didConnect(to device: BTDevice) {
        self.canConnect = true 
        connectedPublished = device // RIGHT HERE
        let holderInfo = BTDeviceHolderInfo(deviceId: device.uuid, rssi: device.rssi)
        let haveSaved = self.onboardedDevices.contains { info in
            return info == holderInfo
        }
        if !haveSaved {
            do {
                // Create JSON Encoder
                let encoder = JSONEncoder()

                // Encode Note
                let data = try encoder.encode(holderInfo)

                // Write/Set Data
                UserDefaults.standard.set(data, forKey: UserDefaults.Key.identifierArray.rawValue)

            } catch {
                print("Unable to Encode Note (\(error))")
            }
        }
        if let newDevice = BASS50(device.peripheral, device.advData, device.rssi) {
            print("it's a new bass being added")
            self.bass50 = newDevice
            self.bass50?.state = .connected
        }
    }

2      

Not sure if this is the reason but give it try - remove $ as it might access a Publisher not array...

appState.connectedDevices.append(device)

2      

@ygeras

Thank you, this was the solution. I feel silly for missing it, I thought i had certainly tested that one out.

2      

Hacking with Swift is sponsored by RevenueCat.

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.