GO FURTHER, FASTER: Try the Swift Career Accelerator today! >>

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 Proxyman.

SPONSORED Debug 10x faster with Proxyman. Your ultimate tool to capture HTTPs requests/ responses, natively built for iPhone and macOS. You’d be surprised how much you can learn about any system by watching what it does over the network.

Try Now!

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.