Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

iOS dont understand how to parse from coreData to NSDictionary - Swift

So i have parsed Json through an api to CoreData. however, i do not get how to put it in an NSDictionary (it has to be NSDictionary not swift Dictionary)

In the end i want a method that returns a NSDictionary so i can parse it to a countrypicker(this was already existing part of an app)

private func fetchRemoteCountries(){

        self.apiClient.request(ApiRouting.Countries,
            networkActivity:NetworkActivity(type: .Background, showNetworkIndicator: true, disableUserInteraction: false),
            success: { [weak self] (json) -> Void in
                if let strongSelf = self {
                    if strongSelf.coreDataManager.persistCountries(json) {
// do call to method that parses it from coredate to nsdictionary
                    }
                }
            }, failure: {[weak self] (response, error) -> Void in
                if let strongSelf = self {
                    if strongSelf.countries.count == 0 {
                        strongSelf.apiClient.showAlert(error,
                            message: "error", confirm: "jeuj",
                            confirmBlock: { () -> Void in
                                strongSelf.fetchRemoteCountries()
                            }, cancelBlock: nil)
                    }
                }}
        )
    }

This is how i tried that approach

//-> NSDictionary?
    private func setupCountryRound() -> NSDictionary?{
        var pickerCountries = NSMutableDictionary()

        let countries =  coreDataManager.fetchCountries()

            for country in countries! {
                pickerCountries.setObject(country.name! , forKey: country.code!)

            }
        return pickerCountries?   // this gives error: Expected declaration
    }

please advice how to create a NSDictionary and to put those keys,values in.

thanks in advance,

Chris

Edit: figured it out, now to fix the forced things

Also, the reason i store it in coreDate is to check with every fetch, if the online data has changed, if so, i remove or add stuff to coreData

And the forced stuff with '!' i will fix once it works