NSDictionary 和 NSURL 的 Objective-C 问题
我是 Objective-C 的新手,我不知道为什么这段代码不起作用:
NSMutableDictionary *bookmarks = [NSMutableDictionary dictionaryWithCapacity:(NSUInteger) 4];
[bookmarks setObject:@"Stanford University" forKey:[NSURL URLWithString:(NSString *) @"http://www.stanford.edu"]];
[bookmarks setObject:@"Apple" forKey:[NSURL URLWithString:(NSString *) @"http://www.apple.com"]];
[bookmarks setObject:@"Berkeley" forKey:[NSURL URLWithString:(NSString *) @"http://www.berkeley.edu"]];
[bookmarks setObject:@"CS193P" forKey:[NSURL URLWithString:(NSString *) @"http://cs193p.stanford.edu"]];
NSEnumerator *browser = [bookmarks keyEnumerator];
id each;
NSURL *url;
while ((each = [browser nextObject])) {
url = [browser valueForKey:(NSString *)each];
NSLog(@"%@", [url absoluteURL]);
}
我得到的错误是:
2009-06-29 11:25:22.844 WhatATool[2102:10b] *** -[NSURL length]: unrecognized selector sent to instance 0x1072c0
2009-06-29 11:25:22.845 WhatATool[2102:10b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSURL length]: unrecognized selector sent to instance 0x1072c0'
任何帮助将不胜感激。 谢谢。
I am new to Objective-C and I have no clue why this code is not working:
NSMutableDictionary *bookmarks = [NSMutableDictionary dictionaryWithCapacity:(NSUInteger) 4];
[bookmarks setObject:@"Stanford University" forKey:[NSURL URLWithString:(NSString *) @"http://www.stanford.edu"]];
[bookmarks setObject:@"Apple" forKey:[NSURL URLWithString:(NSString *) @"http://www.apple.com"]];
[bookmarks setObject:@"Berkeley" forKey:[NSURL URLWithString:(NSString *) @"http://www.berkeley.edu"]];
[bookmarks setObject:@"CS193P" forKey:[NSURL URLWithString:(NSString *) @"http://cs193p.stanford.edu"]];
NSEnumerator *browser = [bookmarks keyEnumerator];
id each;
NSURL *url;
while ((each = [browser nextObject])) {
url = [browser valueForKey:(NSString *)each];
NSLog(@"%@", [url absoluteURL]);
}
The error I get is:
2009-06-29 11:25:22.844 WhatATool[2102:10b] *** -[NSURL length]: unrecognized selector sent to instance 0x1072c0
2009-06-29 11:25:22.845 WhatATool[2102:10b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSURL length]: unrecognized selector sent to instance 0x1072c0'
Any help would be appreciated.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
键是用来访问字典中的对象的。 您在代码中调用
-[bookmarks setObject:... forKey...]
的每一行都颠倒了键和对象。 您的情况出现问题是因为您尝试将 NSURL 对象视为 NSString 对象 -[bookmarks objectForKey:(NSString *)each]
- 并且字典试图获取假定字符串的长度通过调用-length
,它存在于 NSString 但不存在于 NSURL 中。如果您总是使用相同的对象构造字典,请考虑为
NSDictionary
使用更紧凑的“varargs”构造函数。 (请注意,最后一个逗号分隔的参数必须为零 - 请参阅 相关文档)您还可以使用-[NSDictionary DictionaryWithObjects:forKeys:]
< /a> 有两个 NSArray 对象,包含 URL 和书签名称。 (顺便说一句,将字符串文字转换为NSString*
来创建 NSURL 是完全没有必要的。)此外,您还有一个尚未提及的潜伏错误:
browser
是一个
NSEnumerator
,但您调用valueForKey:
就好像它是一个NSDictionary
,而不是书签
。 对于无法识别的选择器来说,这也会导致类似的崩溃。 (即使对于字典对象,您也应该调用-objectForKey:
代替;-valueForKey:
主要用于 Cocoa Bindings,并执行额外的工作你不需要。我意识到这很令人困惑,因为我们用“键值对”来思考,但确实如此......)最后,您还可以稍微简化枚举代码。 (
NSDictionary
上的 for-in 循环枚举其键,就像-keyEnumerator
一样。)以下是我建议执行最后一部分的方法:
A key is what you use to access an object in the dictionary. You have your keys and objects reversed on every line you call
-[bookmarks setObject:... forKey...]
in the code. The problem in your case arises because you're trying to treat NSURL objects as NSString objects —[bookmarks objectForKey:(NSString *)each]
— and the dictionary is trying to get the length of the supposed string by calling-length
, which exists for NSString but not for NSURL.If you're always constructing a dictionary with the same objects, consider using a more compact "varargs" constructor for
NSDictionary
. (Note that the last comma-separated argument must be nil — see the related documentation) You can also use-[NSDictionary dictionaryWithObjects:forKeys:]
with two NSArray objects, containing the URLs and the bookmark names. (Incidentally, casting string literals asNSString*
to create an NSURL is completely unnecessary.)In addition, you have a lurking error that hasn't been mentioned yet:
browser
is anNSEnumerator
, but you're callingvalueForKey:
as if it were anNSDictionary
, rather than onbookmarks
. That will cause a similar crash for an unrecognized selector, too. (Even for dictionary objects, you should call-objectForKey:
instead;-valueForKey:
is used mostly for/by Cocoa Bindings, and does extra work you don't need. I realize it's confusing since we think in terms of "key-value pairs", but there it is...)Finally, you can also simplify the enumeration code somewhat. (A for-in loop on an
NSDictionary
enumerates its keys just like-keyEnumerator
would.)Here's how I'd suggest doing the last part:
您已将书签字典初始化反转:
应该是:
另外,还有一种更简单的方法来进行枚举:
您还可以通过 [bookmarks allValues] 进行枚举以直接获取 URL 对象,而不是先取出键(尽管您可能很想由于某些其他原因而丢失的钥匙)。
You have the bookmarks dictionary intiialization reversed:
Should be:
Also, there's a simpler way to do enumeration:
You could also enumerate through [bookmarks allValues] to get at the URL object directly instead of getting the keys out first (though you may well want the keys for some other reason).
我认为您在尝试获取 URL 时在 while 循环中引用了错误的变量(并且我认为您应该使用
objectForKey:
选择器来获取 NSDictionary 的值,而不是valueForKey:
):I think you're referring to the wrong variable in your while loop when trying to get the URL (and I think you should be using the
objectForKey:
selector to get a value of an NSDictionary, notvalueForKey:
):我正在通过相同的在线课程,发现上面的答案似乎都不起作用,因为 NSEnumerator 可能不会响应 objectForKey 或者因为不支持特定类型的枚举(与 OSX i 相比,可能不在 iPhone SDK 中)不知道)。 我让它像魅力一样工作,如下......
I'm working through the same online classes and found that none of the answers above seem to work either because NSEnumerator may not respond to objectForKey or because the particular type of enumeration is not supported (perhaps not in the iPhone SDK as compared to OSX i don't know). I got it to work like a charm as follows though...