2014-07-11 11 views
5

में प्लेलिस्ट फ़ाइल को लिखें मेरे पास एक नमूना प्लिस्ट-फ़ाइल है, 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 है। इसमें एप्लिकेशन बनाने वाले सभी बदलाव शामिल हैं। मैं क्या गलत कर रहा हूं? प्रोजेक्ट फ़ोल्डर (प्रोजेक्ट नेविगेटर) में स्थित फ़ाइल में सभी बदलावों को मैं कैसे देख सकता हूं?

+2

जांच वापसी 'writeToFile' का मूल्य, और आप इसे में विफल रहा है देखेंगे। क्योंकि मुख्य बंडल में फ़ाइलें –

+1

संभवतः डुप्लिकेट [मेरे ऐप बंडल में प्लस करने के लिए एनएस डिक्शनरी लिखना] हैं (http://stackoverflow.com/questions/2502193/writing-nsdictionary-to-plist-in-my-app-bundle) –

+2

(इसलिए आईओएस अनुप्रयोगों में सभी सामान्य त्रुटियों को एक बार उद्देश्य-सी का उपयोग करके बनाया गया था, अब स्विफ्ट का उपयोग कर फिर से बनाया गया है :-) –

उत्तर

0

हाँ, यह हल समस्या - Swift - Read plist

लेकिन इस परियोजना फ़ोल्डर में plist-फ़ाइलों को केवल संपत्ति की तरह की स्थिति के लिए उपयोग करता है .....

0

आप पर कितनी जल्दी या plist पर लिखने वापस देखो तो plist में पहले से ही विद्यमान कुंजी

var path = NSBundle.mainBundle().pathForResource("GameParam", ofType: "plist") 
    if var dict = NSMutableDictionary(contentsOfFile: path!) { 
     ToolKit.log(dict) 
     dict.setValue("blablabla", forKey: "keyInPlist") 

     dict.writeToFile(path!, atomically: false) 
    } 
0

ज्यादातर लोगों को, कुछ की एक सूची संग्रहीत करना चाहते हैं तो यहां ऐसा करने के तरीके, भी पर मेरे हिस्से का है, यहाँ मैं plist फ़ाइल की प्रतिलिपि नहीं है, मैं बस इसे बनाने के । वास्तविक बचत/लोड हो रहा है, काफी Rebeloper

xcode 7 बीटा से जवाब देने के लिए इसी तरह की है स्विफ्ट 2,0

डॉक्स से

func SaveItemFavorites(items : Array<ItemFavorite>) -> Bool 
{ 
    let paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true) as NSArray 
    let docuDir = paths.firstObject as! String 
    let path = docuDir.stringByAppendingPathComponent(ItemFavoritesFilePath) 
    let filemanager = NSFileManager.defaultManager() 

    let array = NSMutableArray()   

    for var i = 0 ; i < items.count ; i++ 
    { 
     let dict = NSMutableDictionary() 
     let ItemCode = items[i].ItemCode as NSString 

     dict.setObject(ItemCode, forKey: "ItemCode") 
     //add any aditional.. 
     array[i] = dict 
    } 

    let favoritesDictionary = NSDictionary(object: array, forKey: "favorites") 
    //check if file exists 
    if(!filemanager.fileExistsAtPath(path)) 
    { 
     let created = filemanager.createFileAtPath(path, contents: nil, attributes: nil) 
     if(created) 
     { 
      let succeeded = favoritesDictionary.writeToFile(path, atomically: true) 
      return succeeded 
     } 
     return false 
    } 
    else 
    { 
     let succeeded = notificationDictionary.writeToFile(path, atomically: true) 
     return succeeded 
    } 
} 

लिटिल टिप्पणी की बचत:

NSDictionary।writeToFile (पथ: atomically :)

इस विधि रिकर्सिवली मान्य करता है कि सभी निहित वस्तुओं संपत्ति सूची वस्तुओं (NSData के उदाहरण, NSDate, NSNumber, NSString, NSArray, या NSDictionary) फ़ाइल बाहर लिखने से पहले, और रिटर्न हैं नहीं यदि सभी ऑब्जेक्ट्स संपत्ति सूची ऑब्जेक्ट्स नहीं हैं, क्योंकि परिणामी फ़ाइल मान्य संपत्ति सूची नहीं होगी।

तो तुम dict.SetObject() पर सेट जो कुछ ऊपर उल्लेख किया प्रकारों में से एक होना चाहिए।

लोड हो रहा है

private let ItemFavoritesFilePath = "ItemFavorites.plist" 

func LoadItemFavorites() -> Array<ItemFavorite> 
{ 
    let paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true) as NSArray 
    let docuDir = paths.firstObject as! String 
    let path = docuDir.stringByAppendingPathComponent(ItemFavoritesFilePath) 
    let dict = NSDictionary(contentsOfFile: path) 
    let dictitems : AnyObject? = dict?.objectForKey("favorites") 

    var favoriteItemsList = Array<ItemFavorite>() 

    if let arrayitems = dictitems as? NSArray 
    { 
     for var i = 0;i<arrayitems.count;i++ 
     { 
      if let itemDict = arrayitems[i] as? NSDictionary 
      { 
       let ItemCode = itemDict.objectForKey("ItemCode") as? String 
       //get any additional 

       let ItemFavorite = ItemFavorite(item: ItemCode) 
       favoriteItemsList.append(ItemFavorite) 
      } 
     } 
    } 

    return favoriteItemsList 
}