简体   繁体   English

将视频保存到NSUserDefault

[英]Saving Videos to NSUserDefault

我想将视频文件保存在NSUserDefaults中。我知道有一种方法可以通过UserDefault中的setURL保存。如何在数组中添加URL?

Add URLs in an array, and save the array in NSUserDefaults 在数组中添加URL,并将数组保存在NSUserDefaults中

NSArray *urlArray = @[url1, url2, url3];
    [[NSUserDefaults standardUserDefaults] setObject:urlArray forKey:@"UrlArrayKey"];
    [[NSUserDefaults standardUserDefaults] synchronize];
//store urls in NSUserDefaults
NSArray *arrayURLsToStore = [NSArray arrayWithObjects:@"link1", "link2", nil];

[[NSUserDefaults standardUserDefaults] setObject:arrayURLsToStore forKey:@"URLs"];
[[NSUserDefaults standardUserDefaults] synchronize];

//get urls from NSUserDefaults
NSArray *arrURLsRetrived = [[NSUserDefaults standardUserDefaults] objectForKey:@"URLs"];
NSString *urlString = [NSString stringWithFormat:[arrURLsRetrived objectAtIndex:0]];
NSURL *youURL = [NSURL URLWithString:[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

//now you can use URL1    

The user defaults are a property list. 用户默认值为属性列表。 According to the specifications only a few types build property lists. 根据规范,只有少数几种类型可以建立属性列表。

  • NSArray
  • NSDictionary
  • NSString
  • NSNumber
  • NSData
  • NSDate

Collections (array dictionary) are property lists, if and only if all of their members are property lists. 集合(数组字典)是属性列表,当且仅当它们的所有成员都是属性列表时。

As you can see from the link, NSURL is no property list. 从链接可以看到, NSURL不是属性列表。 But you can convert an instance of NSURL into a instance of NSString (URL string) at write time …: 但是您可以在写入时将NSURL的实例转换为NSString的实例(URL字符串)……:

NSString *URLString = [URL absoluteString];

… and store this into the array (which stays being a property list, because instances of NSString are property lists). …并将其存储到数组中(由于NSString实例是属性列表,因此它仍然是属性列表)。

At read time you can take out the string from the array and convert it back into an instance of NSURL : 在读取时,您可以从数组中取出字符串,然后将其转换回NSURL的实例:

NSURL* URL = [NSURL UrlWithString:URLString];

Another way is to convert the instance of NSURL into an instance of NSData and back. 另一种方法是将NSURL实例转换为NSData实例并返回。 This is possible, because NSURL supports NS ( Secure ) Coding . 这是可能的,因为NSURL支持NSSecureCoding

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM