NSUrl 不作为绝对字符串存储在 NSUserDefault 中

发布于 2024-12-04 14:22:07 字数 1804 浏览 1 评论 0原文

我指的是 上一篇文章 在 NSUserDefault 中存储 iPod 库的 NSURL。但应用程序关闭后,它不会存储在 NSUserDefault 中。

我正在使用其他 NSString 存储在 NSUserDefaults 中,这是完美完成的,但是当我将 NSUrl 存储为绝对字符串时..它不存储该值。

可能是什么原因?

编辑

我用来保存 NSUserDefault 值的以下代码:

currentItem = [collection.items objectAtIndex:songCount];
songURL = [currentItem valueForProperty:MPMediaItemPropertyAssetURL];
[[NSUserDefaults standardUserDefaults] setObject:[currentItem valueForProperty:MPMediaItemPropertyTitle] forKey:@"songTitle"];
[[NSUserDefaults standardUserDefaults] setObject:[songURL absoluteString] forKey:@"songURL"];
avPlayer = [[AVPlayer alloc] initWithURL:songURL];
NSLog(@"songTitle: %@  songURL : %@",[[NSUserDefaults standardUserDefaults]objectForKey:@"songTitle"], songURL);

当我尝试保存 NSURL 时出现以下错误:

2011-09-13 18:47:23.258 Tabata Timer[933:707] songURL : ipod-library://item/item.mp3?id=-3715406019015217536
2011-09-13 18:47:23.258 Tabata Timer[933:707] *** -[NSUserDefaults setObject:forKey:]: Attempt to insert non-property value 'ipod-library://item/item.mp3?id=-3715406019015217536' of class 'NSURL'.
2011-09-13 18:47:23.260 Tabata Timer[933:707] songTitle : Ghajini
2011-09-13 18:47:24.860 Tabata Timer[933:707] *** -[NSUserDefaults setObject:forKey:]: Attempt to insert non-property value 'ipod-library://item/item.mp3?id=-3715406019015217536' of class 'NSURL'.
2011-09-13 18:47:24.963 Tabata Timer[933:707] songURL : ipod-library://item/item.mp3?id=-3715406019015217536
warning: Unable to read symbols for /Developer/Platforms/iPhoneOS.platform/DeviceSupport/4.3.5 (8L1)/Symbols/Developer/usr/lib/libXcodeDebuggerSupport.dylib (file not found).

I am referring Previous Post for storing NSURL of iPod Library in NSUserDefault. But it is not stored in NSUserDefault after application is closed.

I am using other NSStrings to store in NSUserDefaults which is perfectly done, But when i store NSUrl as absolute string.. it does not stores the value.

What could be the reason??

EDIT

Following code i am using to save NSUserDefault Value:

currentItem = [collection.items objectAtIndex:songCount];
songURL = [currentItem valueForProperty:MPMediaItemPropertyAssetURL];
[[NSUserDefaults standardUserDefaults] setObject:[currentItem valueForProperty:MPMediaItemPropertyTitle] forKey:@"songTitle"];
[[NSUserDefaults standardUserDefaults] setObject:[songURL absoluteString] forKey:@"songURL"];
avPlayer = [[AVPlayer alloc] initWithURL:songURL];
NSLog(@"songTitle: %@  songURL : %@",[[NSUserDefaults standardUserDefaults]objectForKey:@"songTitle"], songURL);

Following error comes when i try to save the NSURL:

2011-09-13 18:47:23.258 Tabata Timer[933:707] songURL : ipod-library://item/item.mp3?id=-3715406019015217536
2011-09-13 18:47:23.258 Tabata Timer[933:707] *** -[NSUserDefaults setObject:forKey:]: Attempt to insert non-property value 'ipod-library://item/item.mp3?id=-3715406019015217536' of class 'NSURL'.
2011-09-13 18:47:23.260 Tabata Timer[933:707] songTitle : Ghajini
2011-09-13 18:47:24.860 Tabata Timer[933:707] *** -[NSUserDefaults setObject:forKey:]: Attempt to insert non-property value 'ipod-library://item/item.mp3?id=-3715406019015217536' of class 'NSURL'.
2011-09-13 18:47:24.963 Tabata Timer[933:707] songURL : ipod-library://item/item.mp3?id=-3715406019015217536
warning: Unable to read symbols for /Developer/Platforms/iPhoneOS.platform/DeviceSupport/4.3.5 (8L1)/Symbols/Developer/usr/lib/libXcodeDebuggerSupport.dylib (file not found).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

等数载,海棠开 2024-12-11 14:22:07

如果你想存储 NSURL 那么为什么要把它转换成字符串并保存为字符串。它会增加你的工作努力。

你可以试试这个:

NSUserDefaults *defaults=[NSUserDefaults standardUserDefaults];     
[defaults setURL:[NSURL URLWithString:@"http://www.google.com"] forKey:@"urlValue"];
[defaults synchronize];

NSURL *url=[defaults URLForKey:@"urlValue"];
NSLog(@"%@",url);

If you want to store NSURL then why are you converting it into string and saving it as string. It will increase your work effort.

You can try this:

NSUserDefaults *defaults=[NSUserDefaults standardUserDefaults];     
[defaults setURL:[NSURL URLWithString:@"http://www.google.com"] forKey:@"urlValue"];
[defaults synchronize];

NSURL *url=[defaults URLForKey:@"urlValue"];
NSLog(@"%@",url);
給妳壹絲溫柔 2024-12-11 14:22:07

我能想到的唯一可以解释的事情是,该 URL 至少部分是随机生成的,因此在您的应用程序删除对其的引用后无法加载。你说的“关闭”是什么意思?有背景吗?还是真的终止了?

您也可能应该将其保存为 string:yourURLString forKey:@"URL" 而不是对象。这可能会让应用程序感到困惑。尝试使用 NSLog 输出 URL 字符串,看看会得到什么。让我知道它是什么。这会澄清事情。

The only thing I can think of that would explain it is that the URL is at least partially randomly generated and thus fails to load after you app removes reference to it. What do you mean, "closed"? Backgrounded? Or really terminated?

It could also be that you should be saving it as a string:yourURLString forKey:@"URL" instead of an object. It could be confusing the app. Try NSLog to output the URL string and see what you get. Let me know what it is. That will clarify things.

风轻花落早 2024-12-11 14:22:07

您的字符串应该声明属性...然后尝试将其存储在 NSUserDefaults

your string should have property declared...then try to store it in NSUserDefaults

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文