如何从远程plist文件中读取和保存数据

时间:2015-08-30 09:25:52

标签: ios swift plist

我需要帮助,使用Swift在我的iOS应用程序中读取和写入数据到远程plist文件。 我可以在本地读取和保存数据,但不能使用远程服务器。

这里,我的代码是在本地阅读。

变量

    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!")
    }
}

最后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)")
}

2 个答案:

答案 0 :(得分:5)

不,在没有下载文件的情况下,使用Swift无法以读取到外部.plist,进行更改和重新上传它。或者,您需要在服务器上设置自己的API,以便为您执行读/写操作。

正如@Scott H在评论中所述,这是一个更好的方法:

  

如果您想要使用此路线,请在本地下载文件,进行更改   本地,然后上传到服务器。但是,有很多   可用于远程配置的替代方案,如CloudKit,   解析或类似。

详细了解第三方选项:

答案 1 :(得分:-2)

NSMutableDictionary(contentsOfFile: bundlePath)

改为使用contentsOfURL