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

Hello ! So what happens here is that there will be a first loop that just returns the first result and then stops. The idea is rather to collect all the results in an array and then return the result after the loop. But I have an error "No exact matches in call to instance method 'append' " can you help me ?

    func fetchAllFavorites() -> [Favorite] {

        let urls : [URL] = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
        var results: [Favorite] = []
        do {
            for url in urls {
                let favoriteItem = try Data(contentsOf: url)
                let item = try JSONDecoder().decode(Favorite.self, from: favoriteItem)
                results.append(favoriteItem)
                return results
            }
        } catch {
            recordErrorIfNeeded(error)
        }
    }

2      

results is an array of Favorite

favoriteItem is of the type Data

You can't add a Data type to an array that holds Favorite types

use append(item) instead

2      

ok it's good ^^thk you

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!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

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.