NSDictionary 和 NSURL 的 Objective-C 问题

发布于 2024-07-25 07:26:39 字数 1171 浏览 11 评论 0原文

我是 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 技术交流群。

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

发布评论

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

评论(4

不弃不离 2024-08-01 07:26:39

是用来访问字典中的对象的。 您在代码中调用 -[bookmarks setObject:... forKey...] 的每一行都颠倒了键和对象。 您的情况出现问题是因为您尝试将 NSURL 对象视为 NSString 对象 - [bookmarks objectForKey:(NSString *)each] - 并且字典试图获取假定字符串的长度通过调用 -length ,它存在于 NSString 但不存在于 NSURL 中。

如果您总是使用相同的对象构造字典,请考虑为 NSDictionary 使用更紧凑的“varargs”构造函数。 (请注意,最后一个逗号分隔的参数必须为零 - 请参阅 相关文档)您还可以使用 -[NSDictionary DictionaryWithObjects:forKeys:]< /a> 有两个 NSArray 对象,包含 URL 和书签名称。 (顺便说一句,将字符串文字转换为 NSString* 来创建 NSURL 是完全没有必要的。)

NSDictionary *bookmarks = [NSDictionary dictionaryWithObjectsAndKeys:
                           [NSURL URLWithString:@"http://www.stanford.edu"],
                           @"Stanford University",
                           [NSURL URLWithString:@"http://www.apple.com"],
                           @"Apple",
                           [NSURL URLWithString:@"http://www.berkeley.edu"],
                           @"Berkeley",
                           [NSURL URLWithString:@"http://cs193p.stanford.edu"],
                           @"CS193P",
                           nil];

此外,您还有一个尚未提及的潜伏错误:browser是一个 NSEnumerator,但您调用 valueForKey: 就好像它是一个 NSDictionary,而不是书签。 对于无法识别的选择器来说,这也会导致类似的崩溃。 (即使对于字典对象,您也应该调用 -objectForKey: 代替;-valueForKey: 主要用于 Cocoa Bindings,并执行额外的工作你不需要。我意识到这很令人困惑,因为我们用“键值对”来思考,但确实如此......)

最后,您还可以稍微简化枚举代码。 (NSDictionary 上的 for-in 循环枚举其键,就像 -keyEnumerator 一样。)

以下是我建议执行最后一部分的方法:

NSURL *url
for (NSString *key in bookmarks) {
    url = [bookmarks objectForKey:key];
    NSLog(@"%@", [url absoluteURL]);
}

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 as NSString* to create an NSURL is completely unnecessary.)

NSDictionary *bookmarks = [NSDictionary dictionaryWithObjectsAndKeys:
                           [NSURL URLWithString:@"http://www.stanford.edu"],
                           @"Stanford University",
                           [NSURL URLWithString:@"http://www.apple.com"],
                           @"Apple",
                           [NSURL URLWithString:@"http://www.berkeley.edu"],
                           @"Berkeley",
                           [NSURL URLWithString:@"http://cs193p.stanford.edu"],
                           @"CS193P",
                           nil];

In addition, you have a lurking error that hasn't been mentioned yet: browser is an NSEnumerator, but you're calling valueForKey: as if it were an NSDictionary, rather than on bookmarks. 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:

NSURL *url
for (NSString *key in bookmarks) {
    url = [bookmarks objectForKey:key];
    NSLog(@"%@", [url absoluteURL]);
}
酷遇一生 2024-08-01 07:26:39

您已将书签字典初始化反转:

[bookmarks setObject:@"Stanford University" forKey:[NSURL URLWithString:(NSString *) @"http://www.stanford.edu"]];

应该是:

[bookmarks setObject:[NSURL URLWithString:(NSString *) @"http://www.stanford.edu"] forKey:@"Stanford University"];

另外,还有一种更简单的方法来进行枚举:

NSURL *url;
for ( NSString *each in [bookmarks allKeys] ) 
{
        url = [browser objectForKey:(NSString *)each];
        NSLog(@"%@", [url absoluteURL]);
}

您还可以通过 [bookmarks allValues] 进行枚举以直接获取 URL 对象,而不是先取出键(尽管您可能很想由于某些其他原因而丢失的钥匙)。

You have the bookmarks dictionary intiialization reversed:

[bookmarks setObject:@"Stanford University" forKey:[NSURL URLWithString:(NSString *) @"http://www.stanford.edu"]];

Should be:

[bookmarks setObject:[NSURL URLWithString:(NSString *) @"http://www.stanford.edu"] forKey:@"Stanford University"];

Also, there's a simpler way to do enumeration:

NSURL *url;
for ( NSString *each in [bookmarks allKeys] ) 
{
        url = [browser objectForKey:(NSString *)each];
        NSLog(@"%@", [url absoluteURL]);
}

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).

苍景流年 2024-08-01 07:26:39

我认为您在尝试获取 URL 时在 while 循环中引用了错误的变量(并且我认为您应该使用 objectForKey: 选择器来获取 NSDictionary 的值,而不是 valueForKey:):

NSEnumerator *browser = [bookmarks keyEnumerator];
id each;
NSURL *url;
while ((each = [browser nextObject])) {
    url = [bookmarks objectForKey:(NSString *)each]; // <-- bookmarks, not browser
    NSLog(@"URL = %@", url);
}

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, not valueForKey:):

NSEnumerator *browser = [bookmarks keyEnumerator];
id each;
NSURL *url;
while ((each = [browser nextObject])) {
    url = [bookmarks objectForKey:(NSString *)each]; // <-- bookmarks, not browser
    NSLog(@"URL = %@", url);
}
云胡 2024-08-01 07:26:39

我正在通过相同的在线课程,发现上面的答案似乎都不起作用,因为 NSEnumerator 可能不会响应 objectForKey 或者因为不支持特定类型的枚举(与 OSX i 相比,可能不在 iPhone SDK 中)不知道)。 我让它像魅力一样工作,如下......

NSString *aKey;
NSEnumerator *keyEnumerator = [bookmarks keyEnumerator];
NSURL *url;

while (aKey = [keyEnumerator nextObject])
{
    url = [bookmarks objectForKey:aKey];
    NSLog(@"value: %@", [url absoluteURL]);
}

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...

NSString *aKey;
NSEnumerator *keyEnumerator = [bookmarks keyEnumerator];
NSURL *url;

while (aKey = [keyEnumerator nextObject])
{
    url = [bookmarks objectForKey:aKey];
    NSLog(@"value: %@", [url absoluteURL]);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文