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

Gia*_*rsi 10 plist ios swift

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

在这里,我的代码在本地读取.

变量

    var VintiInizialiID: AnyObject!
    var PersiInizialiID: AnyObject!
    var CampionatoID: AnyObject!
    var coefficientetorneoID: AnyObject!
Run Code Online (Sandbox Code Playgroud)

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!")
    }
}
Run Code Online (Sandbox Code Playgroud)

最后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)")
}
Run Code Online (Sandbox Code Playgroud)

Dan*_*ieu 5

不,没有本地方式只使用Swift 读取写入外部.plist而不下载文件,进行更改并重新上传.或者,您需要在服务器上设置自己的API,以便为您执行读/写操作.

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

如果要使用此路由,请在本地下载文件,在本地更改,然后上载到服务器.但是,您可以使用许多替代方案进行远程配置,例如CloudKit,Parse或类似工具.

详细了解第三方选项: