在NSUserDefaults中保存ALAsset URL [英] Saving ALAsset URL in NSUserDefaults

查看:122
本文介绍了在NSUserDefaults中保存ALAsset URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我的 ALAsset网址保存在 NSMutableArray

"ALAsset - Type:Photo, URLs:assets-library://asset/asset.JPG?id=119A0D2D-C267-4B69-A200-59890B2B0FE5&ex‌​t=JPG", 
"ALAsset - Type:Photo, URLs:assets-library://asset/asset.JPG?id=92A7A24F-D54B-496E-B250-542BBE37BE8C&ex‌​t=JPG", 
"ALAsset - Type:Photo, URLs:assets-library://asset/asset.JPG?id=77AC7205-68E6-4062-B80C-FC288DF96F24&ex‌​t=JPG

我无法在 NSUserDefaults NSMutableArray c>由于它有错误请注意,属性列表中的字典和数组也必须只包含属性值。
我正在考虑使用它:

I wasnt able to save NSMutableArray in NSUserDefaults due to it having an error Note that dictionaries and arrays in property lists must also contain only property values. Im thinking of using this :

            - (void)encodeWithCoder:(NSCoder *)encoder {
                //Encode properties, other class variables, etc
                [encoder encodeObject:self.selectedPhotos forKey:@"selectedPhotos"];
            }

            - (id)initWithCoder:(NSCoder *)decoder {
                if((self = [super init])) {
                    //decode properties, other class vars
                    self.selectedPhotos = [decoder decodeObjectForKey:@"selectedPhotos"];
                }
                return self;
            }

然后使用以下代码保存并检索它:

then save and retrieve it with this code:

            - (void)saveCustomObject:(MyCustomObject *)obj {
                NSData *myEncodedObject = [NSKeyedArchiver archivedDataWithRootObject:obj];
                NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
                [defaults setObject:myEncodedObject forKey:@"myEncodedObjectKey"];
            }

            - (MyCustomObject *)loadCustomObjectWithKey:(NSString *)key {
                NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
                NSData *myEncodedObject = [defaults objectForKey:key];
                MyCustomObject *obj = (MyCustomObject *)[NSKeyedUnarchiver unarchiveObjectWithData: myEncodedObject];
                return obj;
            }

但是我不知道怎么回事,我的代码仍然崩溃了。不知道怎么做。我无法将其保存在NSUserDefaults中。希望有人帮忙。真的有这个问题了一段时间。希望有人引导我走正确的保存路径并从 NSUserDefaults 以正确的方式检索它。然后回到 NSMutableArray

But I somehow dont quite get it, still crashes in my code. Dont know how. And I wasnt able to save it in NSUserDefaults. Hope someone help. Really been having problem with this a while. Hope someone guide me on the right path of saving and retrieving it the right way from NSUserDefaults. Then back to a NSMutableArray.

推荐答案

NSUserDefaults 仅将一组受限制的类作为对象。请参阅文档。您必须注意在字典中存储这些类型的值(NSData,NSString,NSNumber,NSDate,NSArray或NSDictionary,当然它是递归应用的)。

The NSUserDefaults only takes a restricted set of classes as objects. See the documentation. You must take care only to store values of these types (NSData, NSString, NSNumber, NSDate, NSArray, or NSDictionary, and of course it applies recursively) in the dictionary.

要将URL存储在 NSUserDefaults 中,请将它们存储为字符串,然后将其作为URL读回。如果你需要使用当前格式的字典,你可能需要在保存之前对其进行转换。

To store the URLs in the NSUserDefaults, store them as strings, then read them back as URLs. If you need to have the dictionary in the current format, you may have to transform it before saving it.

- (void) saveMyUrls
{
    NSMutableArray* urls = [NSMutableArray arrayWithCapacity:self.myUrls.count];
    for(NSURL* url in self.myUrls) {
        [urls addObject:[url absoluteString]];
    }
    [[NSUserDefaults standardUserDefaults] setObject:urls forKey:@"myUrls"];
}


- (void) loadUrls
{
    NSArray* urls = [[NSUserDefaults standardUserDefaults] objectForKey:@"myUrls"];
    self.myUrls = [NSMutableArray arrayWithCapacity:urls.count];
    for(NSString* urlString in urls) {
        [self.myUrls addObject:[NSURL URLWithString:urlString]];
    }
    [[NSUserDefaults standardUserDefaults] setObject:urls forKey:@"myUrls"];
}

如果你需要保存的信息不仅仅是网址,那么让我们说一个用户 - 指定标签,您可以将对象另存为 NSDictionary ,例如

If you need to save more information than just the URL, let's say a user-specified label, you could save the object as a NSDictionary instead, e.g.

- (void) saveMyUrlsWithLabels
{
    NSMutableArray* objs = [NSMutableArray arrayWithCapacity:self.myObjects.count];
    for(MyObject* obj in self.myObjects) {
        [objs addObject:[NSDictionary dictionaryWithKeys:@"url", @"label"
            forObjects:obj.url.absoluteString, obj.userSpecifiedLabel];
    }
    [[NSUserDefaults standardUserDefaults] setObject:objs forKey:@"myObjects"];
}

这篇关于在NSUserDefaults中保存ALAsset URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆