में प्लेलिस्ट फ़ाइल को लिखें मेरे पास एक नमूना प्लिस्ट-फ़ाइल है, favCities.plist। यह मेरा नमूना कोड है:स्विफ्ट
override func viewDidLoad() {
super.viewDidLoad()
let path = NSBundle.mainBundle().pathForResource("favCities", ofType: "plist")
var plistArray = NSArray(contentsOfFile: path) as [Dictionary<String, String>]
var addDic: Dictionary = ["ZMV": "TEST", "Name": "TEST", "Country": "TEST"]
plistArray += addDic
(plistArray as NSArray).writeToFile(path, atomically: true)
var plistArray2 = NSArray(contentsOfFile: path)
for tempDict1 in plistArray2 {
var tempDict2: NSDictionary = tempDict1 as NSDictionary
var cityName: String = tempDict2.valueForKey("Name") as String
var cityZMV: String = tempDict2.valueForKey("ZMV") as String
var cityCountry: String = tempDict2.valueForKey("Country") as String
println("City: \(cityName), ZMV: \(cityZMV), Country: \(cityCountry)")
}
पहली नज़र में, सबकुछ अच्छी तरह से काम करता है। उत्पादन इस तरह दिखता है:
City: Moscow, ZMV: 00000.1.27612, Country: RU
City: New York, ZMV: 10001.5.99999, Country: US
City: TEST, ZMV: TEST, Country: TEST
लेकिन जब मैं अनुप्रयोग बाधा डालते हैं, मुझे लगता है कि मेरी फाइल favCities.plist नहीं बदला है। अभी भी दो मान हैं। ये मान - City: TEST, ZMV: TEST, Country: TEST
- जोड़े नहीं गए थे। अगर मैंने एप्लिकेशन को पुनरारंभ किया है, तो फिर मैं आउटपुट की 3 लाइनों को देखता हूं, हालांकि 4 होना चाहिए।
क्या गलत है?
UPDATED:
मैं इस के लिए कोड बदल दिया गया था:
override func viewDidLoad() {
super.viewDidLoad()
let fileManager = (NSFileManager.defaultManager())
let directorys : [String]? = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory,NSSearchPathDomainMask.AllDomainsMask, true) as? [String]
if (directorys! != nil){
let directories:[String] = directorys!;
let dictionary = directories[0];
let plistfile = "favCities.plist"
let plistpath = dictionary.stringByAppendingPathComponent(plistfile);
println("\(plistpath)")
var plistArray = NSArray(contentsOfFile: plistpath) as [Dictionary<String, String>]
var addDic: Dictionary = ["ZMV": "TEST", "Name": "TEST", "Country": "TEST"]
plistArray += addDic
(plistArray as NSArray).writeToFile(plistpath, atomically: false)
var plistArray2 = NSArray(contentsOfFile: plistpath)
for tempDict1 in plistArray2 {
var tempDict2: NSDictionary = tempDict1 as NSDictionary
var cityName: String = tempDict2.valueForKey("Name") as String
var cityZMV: String = tempDict2.valueForKey("ZMV") as String
var cityCountry: String = tempDict2.valueForKey("Country") as String
println("City: \(cityName), ZMV: \(cityZMV), Country: \(cityCountry)")
}
}
else {
println("ERROR!")
}
}
अब जब आप उत्पादन बढ़ जाती है में पंक्तियों की संख्या एप्लिकेशन को चलाने: लेकिन
City: Moscow, ZMV: 00000.1.27612, Country: RU
City: New York, ZMV: 10001.5.99999, Country: US
City: TEST, ZMV: TEST, Country: TEST
City: TEST, ZMV: TEST, Country: TEST
........
! अगर फ़ाइल फ़ोल्डर favCities.plist देखें, जो प्रोजेक्ट फ़ोल्डर (एक्सकोड में प्रोजेक्ट नेविगेटर) में स्थित है, यह अभी भी अपरिवर्तित बनी हुई है - दो लाइनें हैं!
पथ, जो चर plistpath
में संग्रहित है साथ चलना है - /Users/admin/Library/Developer/CoreSimulator/Devices/55FD9B7F-78F6-47E2-9874-AF30A21CD4A6/data/Containers/Data/Application/DEE6C3C8-6A44-4255-9A87-2CEF6082A63A/Documents/
तो फिर वहाँ एक और फ़ाइल favCities.plist है। इसमें एप्लिकेशन बनाने वाले सभी बदलाव शामिल हैं। मैं क्या गलत कर रहा हूं? प्रोजेक्ट फ़ोल्डर (प्रोजेक्ट नेविगेटर) में स्थित फ़ाइल में सभी बदलावों को मैं कैसे देख सकता हूं?
जांच वापसी 'writeToFile' का मूल्य, और आप इसे में विफल रहा है देखेंगे। क्योंकि मुख्य बंडल में फ़ाइलें –
संभवतः डुप्लिकेट [मेरे ऐप बंडल में प्लस करने के लिए एनएस डिक्शनरी लिखना] हैं (http://stackoverflow.com/questions/2502193/writing-nsdictionary-to-plist-in-my-app-bundle) –
(इसलिए आईओएस अनुप्रयोगों में सभी सामान्य त्रुटियों को एक बार उद्देश्य-सी का उपयोग करके बनाया गया था, अब स्विफ्ट का उपयोग कर फिर से बनाया गया है :-) –