Im trying to read a plist that contains an Array of Ints .
This is my first time using them , and i can read them in fine , but the plist doesn't update when i write to it.
here's my code for the reading and writing..
class FavouritesManager {
var myArray:NSMutableArray = [0]
func loadDataPlist(){
let paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true) as NSArray
let documentsDirectory = paths.objectAtIndex(0)as NSString
let path = documentsDirectory.stringByAppendingPathComponent("FavouriteIndex.plist")
let fileManager = NSFileManager.defaultManager()
if(!fileManager.fileExistsAtPath(path))
{
let bundle = NSBundle.mainBundle().pathForResource("FavouriteIndex", ofType: "plist")
fileManager.copyItemAtPath(bundle!, toPath: path, error:nil)
}
myArray = NSMutableArray(contentsOfFile: path)!
println(myArray)
}
func writeToPlist(indexValue:Int) {
if let path = NSBundle.mainBundle().pathForResource("FavouriteIndex", ofType: "plist"){
myArray.addObject(indexValue)
myArray.addObject(indexValue+5)
myArray.addObject(indexValue+10)
myArray.addObject(indexValue+15)
myArray.writeToFile(path, atomically: true)
}
}
The purpose of this code is to store the index of the tableViewCell that has been favourited so that I can display that row in my favourite tableViewControllwe
BTW - my plist looks like this...
Thanks !
Your problem is that you are loading the plist into your documents directory in loadDataPlist(), but you are still pulling the plist from it's original location in your writeToPlist(indexValue:) function. So you are trying to write to a plist at a readonly location. Change your write function to this:
func writeToPlist(indexValue:Int) {
var paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as String
var path = paths.stringByAppendingPathComponent("MyPlist.plist")
if var plistArray = NSMutableArray(contentsOfFile: path) {
plistArray.addObject(indexValue)
plistArray.addObject(indexValue+5)
plistArray.addObject(indexValue+10)
plistArray.addObject(indexValue+15)
plistArray.writeToFile(path, atomically: false)
}
}
Note that instead of adding the values to myArray I'm only writing to the plist. You'll have to decide if you want to maintain the two arrays (the local variable myArray and the array in the plist) depending on what you're using this for. Either way, this should fix your problem.
Related
I am trying to write to a plist in xcode. What I've written works, except the plist file doesn't change. I've tried a few different implementations of this, and reaching the same result.
Here is the code:
func saveGameData() {
let BedroomFloorKey = "BedroomFloor"
let BedroomWallKey = "BedroomWall"
var bedroomFloorID: AnyObject = 101 as AnyObject
var bedroomWallID: AnyObject = 101 as AnyObject
let paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true) as NSArray
let documentsDirectory = paths.object(at: 0) as! NSString
let path = documentsDirectory.appendingPathComponent("room.plist")
print("PATH", path)
let dict: NSMutableDictionary = ["XInitializerItem": "DoNotEverChangeMe"]
//saving values
dict.setObject(bedroomFloorID, forKey: BedroomFloorKey as NSCopying)
dict.setObject(bedroomWallID, forKey: BedroomWallKey as NSCopying)
//...
//writing to GameData.plist
dict.write(toFile: path, atomically: false)
let resultDictionary = NSMutableDictionary(contentsOfFile: path)
print("Saved GameData.plist file is --> \(resultDictionary?.description ?? "")")
}
My plist is in the main directory of my xcode project, same folder as were the ViewController is.
Thanks
You cannot write into the application bundle, for obvious reasons the bundle is read-only.
Your code writes the plist into the Documents directory in the container of the application. If you have a default Property List file in the application bundle copy it on the first launch of the app into the Documents directory.
However the code looks like a ugly literal translation of Objective-C code. This is a native Swift version
func saveGameData() throws {
let bedroomFloorKey = "BedroomFloor"
let bedroomWallKey = "BedroomWall"
let bedroomFloorID = 101
let bedroomWallID = 101
let documentsDirectory = try FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false)
let url = documentsDirectory.appendingPathComponent("room.plist")
print("PATH", url)
var dict : [String:Any] = ["XInitializerItem": "DoNotEverChangeMe"]
//saving values
dict[bedroomFloorKey] = bedroomFloorID
dict[bedroomWallKey] = bedroomWallID
//...
//writing to GameData.plist
let data = try PropertyListSerialization.data(fromPropertyList: dict, format: .xml, options: 0)
try data.write(to: url)
print("Saved GameData.plist file is --> \(dict)")
}
There is no need to reread the data. If no error is thrown the plist has been written successfully.
You create a plist file inside documents
let path = documentsDirectory.appendingPathComponent("room.plist")
which isn't same as the file located in your main bundle ( same level as ViewController.swift ) which won't accept any write as bundle is signed with the app and can't accept any change
I had written a code when i was working with swift 1.2 which its function is loading and saving the data from plist. it was working fine with swift 1.2 but now that im working on swift 2. the code still loads the value but it doesnt save the value.
i run the application on device and not the simulator.
you can see the codes below:
func loadGameData() {
let path = NSBundle.mainBundle().pathForResource("GameData", ofType: "plist")!
let myDict = NSDictionary(contentsOfFile: path)
if let dict = myDict {
highscore = dict.objectForKey("highscore")!
} else {
print("WARNING: Couldn't create dictionary from GameData.plist! Default values will be used!")
}
}
func saveGameData() {
let paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true) as NSArray
let documentsDirectory = paths.objectAtIndex(0) as! NSString
let path = documentsDirectory.stringByAppendingPathComponent("GameData.plist")
let dict: NSMutableDictionary = ["XInitializerItem": "DoNotEverChangeMe"]
dict.setObject(highscore.integerValue, forKey: "highscore")
//writing to GameData.plist
dict.writeToFile(path, atomically: true)
let resultDictionary = NSMutableDictionary(contentsOfFile: path)
print("Saved GameData.plist file is --> \(resultDictionary?.description)")
}
the console message after saving the code is:
Saved GameData.plist file is --> Optional("{\n XInitializerItem = DoNotEverChangeMe;\n highscore = 25;\n}")
does anyone know a different code which works with swift 2 ? this one worked fine with the previous versions.
Tnx for the help
Problem is you are reading from Main bundle while writing into Documents directly.
Reading Path: NSBundle.mainBundle().pathForResource("GameData", ofType: "plist")!
Writing Path: NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true) as NSArray
To fix this, change your reader code like:
func loadGameData() {
let paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true) as NSArray
let documentsDirectory = paths.objectAtIndex(0) as! NSString
let path = documentsDirectory.stringByAppendingPathComponent("GameData.plist")
let myDict = NSDictionary(contentsOfFile: path)
if let dict = myDict {
highscore = dict.objectForKey("highscore")!
} else {
print("WARNING: Couldn't create dictionary from GameData.plist! Default values will be used!")
}
}
Ok, Managed to put this code together, created a plist file with Xcode which I copied {using iTunes} to the app folder, it doesn't work...
import Foundation
class getBeaconConfiguration {
var myBeaconsDict: NSDictionary?
func getBeacons() {
let documentsPath : AnyObject = NSSearchPathForDirectoriesInDomains(.DocumentDirectory,.UserDomainMask,true)[0]
let sourcePath:NSString = documentsPath.stringByAppendingString("/beacons.plist")
let dict = NSDictionary(contentsOfFile: sourcePath as String)
print("dict",dict)
}
}
The path is good, and the plist is there, but the NSDictionary returned is a null pointer it seems. How do you do this in swift 2.0.
Get Create a URL to the file and then create a dictionary from it.
let filename = "beacons.plist"
guard
let fileURL = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask).first?.URLByAppendingPathComponent(filename)
else { fatalError("Unable to get file") }
let dict = NSDictionary(contentsOfURL: fileURL)
A little different in Swift 2.0, String no longer has stringByAppendingPathComponent, and you should be using this method when working with file paths.
let documentsPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as NSString
let sourcePath = documentsPath.stringByAppendingPathComponent("beacons.plist")
let dictionary = NSDictionary(contentsOfFile: sourcePath as String)
print("dict: \(dictionary)")
I need help to read and write data to a remote plist file in my iOS application with Swift.
I can read and save data in local but not with a remote server.
Here, my code to read in local.
Variables
var VintiInizialiID: AnyObject!
var PersiInizialiID: AnyObject!
var CampionatoID: AnyObject!
var coefficientetorneoID: AnyObject!
loadPlistData()
func loadPlistData() {
var VintiInizialiKey = "VintiIniziali"
var PersiInizialiKey = "PersiIniziali"
var TutorialKey = "Tutorial"
var coefficientetorneoKey = "CoefficienteTorneo"
var CampionatoKey = "Campionato"
// getting path to database.plist
let paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true) as NSArray
let documentsDirectory = paths[0] as! String
let path = documentsDirectory.stringByAppendingPathComponent("database.plist")
let fileManager = NSFileManager.defaultManager()
//check if file exists
if(!fileManager.fileExistsAtPath(path)) {
// If it doesn't, copy it from the default file in the Bundle
if let bundlePath = NSBundle.mainBundle().pathForResource("database", ofType: "plist") {
let resultDictionary = NSMutableDictionary(contentsOfFile: bundlePath)
println("Bundle database.plist file is --> \(resultDictionary?.description)")
fileManager.copyItemAtPath(bundlePath, toPath: path, error: nil)
println("copy")
} else {
println("database.plist not found. Please, make sure it is part of the bundle.")
}
} else {
println("database.plist already exits at path.")
// use this to delete file from documents directory
//fileManager.removeItemAtPath(path, error: nil)
}
let resultDictionary = NSMutableDictionary(contentsOfFile: path)
println("Loaded database.plist file is --> \(resultDictionary?.description)")
var myDict = NSDictionary(contentsOfFile: path)
if let dict = myDict {
//loading values
VintiInizialiID = dict.objectForKey(VintiInizialiKey)!
PersiInizialiID = dict.objectForKey(PersiInizialiKey)!
CampionatoID = dict.objectForKey(CampionatoKey)!
coefficientetorneoID = dict.objectForKey(coefficientetorneoKey)!
//...
} else {
println("WARNING: Couldn't create dictionary from GameData.plist! Default values will be used!")
}
}
And Finally SavePlistData()
func Saveplistdata() {
let paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true) as NSArray
let documentsDirectory = paths.objectAtIndex(0)as! NSString
let path = documentsDirectory.stringByAppendingPathComponent("database.plist")
var dict: NSMutableDictionary = ["XInitializerItem": "DoNotEverChangeMe"]
//saving values
dict.setObject(VintiInizialiID, forKey: "VintiIniziali")
dict.setObject(PersiInizialiID, forKey: "PersiIniziali")
dict.setObject(CampionatoID, forKey: "Campionato")
dict.setObject(coefficientetorneoID, forKey: "CoefficienteTorneo")
//...
//writing to database.plist
dict.writeToFile(path, atomically: false)
let resultDictionary = NSMutableDictionary(contentsOfFile: path)
// println("Saved database.plist file is --> \(resultDictionary?.description)")
}
No, there isn't a native way to read and write to an external .plist using just Swift without downloading the file, making changes and re-uploading it. Alternatively, you'd need to set up your own API on a server in order to carry out the read / write actions for you.
As #Scott H stated in the comments, theres a better way to do this:
If you want to go this route, download the file locally, change it
locally, and then upload to the server. However, there are many
alternatives available to you for remote configuration like CloudKit,
Parse, or similar.
Learn more about 3rd party options:
CloudKit
Parse
NSMutableDictionary(contentsOfFile: bundlePath)
Use contentsOfURL instead.
Im trying to read and load data from a pList file. I have stored the data as NSArray in the file. But i couldn't read it. Below is the code that I have used.
var documentsDirectory = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as NSString
var path : NSString = documentsDirectory.stringByAppendingPathComponent("imagesPList.pList")
var data = NSMutableArray(contentsOfFile: path)
I took help from this source
Please help. Thanks in advance.
Try This.
let path = NSBundle.mainBundle().pathForResource("Property", ofType: "plist")
let dict = NSDictionary(contentsOfFile: path!)
Try this:
let settings = NSDictionary(contentsOfFile:"Root.plist")
if let preferences = settings.objectForKey("PreferenceSpecifiers"){
//do something ...
}