2016-09-14 33 views
0

我已經實現了cocoapod DKImagePickerController在我的應用程序,我想使用內置的功能,記得重新啓動應用程序的選擇:DKImagePickerController:如何DKAsset數組保存到NSUserDefaults的

pickerController.selectedAssets = selAssets[sender] 

然而,我似乎無法解決如何保存[[DKasset]](DKasset數組數組)。

我已經試過:

func getUserSettings() 
{ 
    selAssets = NSKeyedUnarchiver.unarchiveObjectWithFile(fileInDocumentsDirectory("assets")) as! [[DKAsset]] 
} 

func setUserSettings() 
{ 
    NSKeyedArchiver.archiveRootObject(selAssets, toFile: self.fileInDocumentsDirectory("assets")) 
} 

但getUserSettings()不填充可用的對象數組。我也嘗試過使用NSUserDefaults的objectForKey,但這似乎與DKAsset不兼容。 DKAsset類是NSObject的一個子類。

如何保存和加載這個數組?

可可豆莢: https://cocoapods.org/pods/DKImagePickerController

回答

0

誰想要做到這一點:

func loadFromNSDefaults() 
{ 
    if UserDefaults.standard.stringArray(forKey: "selection") != nil 
    { 
     assetIdentifier = UserDefaults.standard.stringArray(forKey: "selection")! 
    } 
    fetchAsset.removeAll() 
    selAssets.removeAll() 
    let options = PHFetchOptions() 
    for i in 0..<assetIdentifier.count 
    { 
     print(i) 
     fetchAsset.append(PHAsset.fetchAssets(withLocalIdentifiers: assetIdentifier, options: options).object(at: i)) 
     selAssets.append(DKAsset.init(originalAsset: fetchAsset[i])) 
    } 
} 

func saveToNSDefaults(assets: [DKAsset]) 
{ 
    assetIdentifier.removeAll() 
    for i in 0..<assets.count 
    { 
     assetIdentifier.append((assets[i].originalAsset?.localIdentifier)!) 
     print(assets[i].originalAsset?.localIdentifier) 
    } 
    UserDefaults.standard.setValue(assetIdentifier, forKey: "selection") 
}