我已经在我的应用程序中实现了cocoapod DKImagePickerController,我想使用内置函数来记住应用程序重启时的选择:

    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

最佳答案

对于任何想要这样做的人:

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")
}

10-08 05:33