UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

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      

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out more

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.