WWDC22 SALE: Save 50% on all my Swift books and bundles! >>

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 [:]
    }

   

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.

   

@NoOne  

@jmamensshow do i select the correct repository in xcode?

   

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

   

Hacking with Swift is sponsored by Emerge

SPONSORED Optimize your app’s startup time, binary size, and overall performance using Emerge’s advanced app optimization and monitoring tools. Reliably measure app size, speed up your app's startup time with Emerge's Launch Booster, and much more. Emerge is actively used by many of the top mobile development teams in the world.

Find out more

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

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.