I have written this code to change plist file:
var check = false
let path = NSBundle.mainBundle().pathForResource("se.feomedia.qkrussia", ofType: "plist")
let dict = NSMutableDictionary(contentsOfFile : path!)!
for (key, value) in dict {
if String(key as NSString).hasPrefix("GAME") {
for k in dict.objectForKey(key) as Array<NSDictionary> {
for (g, var d) in k {
if String(g as NSString) == "index" {
check = true
}
if String(g as NSString).hasPrefix("backup") || (String(g as NSString).hasPrefix("question") && check) {
println(g)
let u = d as NSDictionary
println(2)
for (e, var h) in u {
if String(e as NSString).hasPrefix("correct") {
h = "someText"
}
}
d = u
}
else {
if String(g as NSString) == "correct" {
d = "someText"
}
}
}
check = false
}
}
}
dict.writeToFile("/Users/nik/Desktop/correct.plist", atomically: true)
The plist file contains the following:
<dict>
<key>CURRENT_USER_ID</key>
<string>6751073888894976</string>
<key>FBAccessTokenInformationKeyUUID</key>
<string>2AB33590-09AC-4B76-9E57-42005E9CF9B7</string>
<key>GAME_4808886523527168</key>
<array>
<dict>
<key>cat_id</key>
<string>19</string>
<key>cat_name</key>
<string>Знаменитости и СМИ</string>
<key>correct</key>
<string>Макс и Эмме</string>
<key>q_id</key>
<integer>6296760553046016</integer>
<key>question</key>
<string>Как зовут детей Дженифер Лопез?</string>
<key>timestamp</key>
<string>2014-07-28 14:18:18</string>
<key>wrong1</key>
<string>Макс и Эббот</string>
<key>wrong2</key>
<string>Дженни и Марк</string>
<key>wrong3</key>
<string>Селена и Джастин</string>
</dict>
<dict>
<key>cat_id</key>
<string>19</string>
<key>cat_name</key>
<string>Знаменитости и СМИ</string>
<key>correct</key>
<string>2008 г.</string>
<key>q_id</key>
<integer>4765042876612608</integer>
<key>question</key>
<string>В каком году Дима Билан победил на Евровидении?</string>
<key>timestamp</key>
<string>2014-07-28 14:18:18</string>
<key>wrong1</key>
<string>2006 г.</string>
<key>wrong2</key>
<string>2007 г.</string>
<key>wrong3</key>
<string>2010 г.</string>
</dict>
<dict>
<key>cat_id</key>
<string>19</string>
<key>cat_name</key>
<string>Знаменитости и СМИ</string>
<key>correct</key>
<string>Лампочка</string>
<key>q_id</key>
<integer>6690225728258048</integer>
<key>question</key>
<string>Любимый объект художника Васи Ложкина на его собственных картинах?</string>
<key>timestamp</key>
<string>2015-01-07 12:08:45</string>
<key>wrong1</key>
<string>Кот</string>
<key>wrong2</key>
<string>Рамка</string>
<key>wrong3</key>
<string>Мона Лиза</string>
</dict>
And much more. The plist is written correctly, everything works, reading from plist works perfectly, but the correct.plist file is absolutely the same as the real file, so nothing changed. I have no idea why this doesn't work.
EDIT: Also dictionaries might contain 3 elements : 2 dictionaries (question and backupQuestion and element index with integer)
The root problem is that when you write:
for (g, var d) in k {
you are no longer operating on the dictionary by changing d
. d
is a fresh new variable, which you then assign a new value to. It does not change the outer variable, and when it goes out of scope at the end of the for loop, any changes are discarded.
To actually change the dictionary, the best strategy is to get out the element you want to alter, amend it, then replace the value in the dictionary. Something like this:
if let path = NSBundle.mainBundle().pathForResource("se.feomedia.qkrussia", ofType: "plist"),
dict = NSDictionary(contentsOfFile : path)
{
for (key, value) in dict {
// note, "var items = " creates a COPY of the values
if let key = key as? String where key.hasPrefix("GAME"),
var items = value as? [NSDictionary]
{
for idx in indices(items) {
//perform some logic on each items[idx] value,
// e.g. checking for things with
// if items[idx]["index"] != nil, let questionValue = items[idx]["question"]
// then updating values in-place, e.g.:
// items[idx]["correct"] = "whatever"
}
// then _replace_ the original value in the dictionary
dict.setValue(items, forKey: key)
}
}
dict.writeToFile("/Users/nik/Desktop/correct.plist", atomically: true)
}
Note, the above code relies on features in Swift 1.2, which is now released for production in the app store. I’d strongly recommend upgrading to it, as it has a lot of features that make this kind of code easier.