简体   繁体   English

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

[英]How to read and save data from a remote plist file

I need help to read and write data to a remote plist file in my iOS application with Swift. 我需要帮助来使用Swift在我的iOS应用程序中读取和写入数据到远程plist文件。 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() 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() 最后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. 不,没有本地方式只使用Swift 读取写入外部.plist而不下载文件,进行更改并重新上传。 Alternatively, you'd need to set up your own API on a server in order to carry out the read / write actions for you. 或者,您需要在服务器上设置自己的API,以便为您执行读/写操作。

As @Scott H stated in the comments, theres a better way to do this: 正如@Scott H在评论中所述,有一个更好的方法:

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. 但是,您可以使用许多替代方案进行远程配置,例如CloudKit,Parse或类似工具。

Learn more about 3rd party options: 详细了解第三方选项:

NSMutableDictionary(contentsOfFile: bundlePath)

请改用contentsOfURL

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM