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

Please help, Unable to write into plist file.

Forums > Swift

@NoOne  

I have hardcoded some data into plist beforehand. I am able to read the data from plist, but not able to write it!! (observation: and whenever i print the url, it is different every time, why is it like this?) Here is the code which I am using to get data and save data from plist.

func savePropertyList(_ plist: [String:Any])
    {
        let path = Bundle.main.path(forResource: "PL", ofType: "plist")!
        let url = URL(fileURLWithPath: path)
        do{
            let data = try PropertyListSerialization.data(fromPropertyList: plist, format: .xml, options: 0)
            try data.write(to: url)
        }
        catch{
            print(error.localizedDescription)
        }
    }

    func loadPropertyList() -> [String:Any]?
    {
        let path = Bundle.main.path(forResource: "PL", ofType: "plist")!
        let url = URL(fileURLWithPath: path)
        do{
            let data = try Data(contentsOf: url)
            guard let dict = try PropertyListSerialization.propertyList(from: data, format: nil) as? [String:Any] else{
                return [:]
            }
            return dict
        }
        catch{
            print(error.localizedDescription)
        }
        return [:]
    }

2      

An app's Bundle is read-only. If you want to modify a file in the Bundle, ou would need to write it out to a different location, like the Documents directory or Application Support folder.

2      

@NoOne  

@jmamensshow do i select the correct repository in xcode?

2      

@JainamRaval I don't think that @jmamenss is a real user, and that their message is spam. They are promoting a product for PC not Apple (iOS, macOS,…)

@roosterboy has already given you the answer, and nothing to do with repositories.

First of all, are you even using a repository. You can check in Xcode. Load your project, and in the Menu click Source Control. If everything is greyed out except for New Git Repositories and Clone you are not even using a repository.

If you wish to use a repository, which I would advise you to do, then Git / GitHub is a industry standard and popular one. There are others if you wish to use them instead.

There is a lot of material and help out there for Git (it is very powerful), and to help you understand the basics you can have a look at these series of 6 Git explanation videos.

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.