如何访问 NSMutableDictionary 的键?
NSMutableDictionary *bookmarks = [NSMutableDictionary dictionary];
[bookmarks setObject:[NSURL URLWithString:(NSString *) @"http://stanford.edu"] forKey:
@"Stanford University"];
[bookmarks setObject:[NSURL URLWithString:(NSString *) @"http://apple.com"] forKey:
@"Apple"];
[bookmarks setObject:[NSURL URLWithString:(NSString *) @"http://itunes.stanford.edu"] forKey:
@"Stanford on iTunesU"];
[bookmarks setObject:[NSURL URLWithString:(NSString *) @"http://stanfordshop.com"] forKey:
@"Stanford Mall"];
NSEnumerator *browser = [bookmarks keyEnumerator];
id each;
NSURL *url;
while ((each = [browser nextObject])) {
url = [bookmarks objectForKey:(NSString *)each];
NSLog('%@'", url);
我意识到斯坦福大学的 iPhone 编程技术已经被打败了(我确信)。 我只是在弄清楚如何打印带有 URL 的密钥时遇到了一些麻烦。 我也不知道如何只打印以斯坦福开头的密钥。 我知道这是 NSString 的一种方法。
NSMutableDictionary *bookmarks = [NSMutableDictionary dictionary];
[bookmarks setObject:[NSURL URLWithString:(NSString *) @"http://stanford.edu"] forKey:
@"Stanford University"];
[bookmarks setObject:[NSURL URLWithString:(NSString *) @"http://apple.com"] forKey:
@"Apple"];
[bookmarks setObject:[NSURL URLWithString:(NSString *) @"http://itunes.stanford.edu"] forKey:
@"Stanford on iTunesU"];
[bookmarks setObject:[NSURL URLWithString:(NSString *) @"http://stanfordshop.com"] forKey:
@"Stanford Mall"];
NSEnumerator *browser = [bookmarks keyEnumerator];
id each;
NSURL *url;
while ((each = [browser nextObject])) {
url = [bookmarks objectForKey:(NSString *)each];
NSLog('%@'", url);
I realize Stanford's iPhone programming stuff has been beat to death (I'm sure). I'm just having a little trouble finding out how I would print the keys with the URLs. I also can't figure out how to only print keys that start with Stanford. I know it's a method of NSString though.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在某些情况下,过滤集合是通过“通过测试”系列方法最清楚地完成的,尽管在这个简单的情况下看起来更复杂。
In some situations, filtering collections is done most clearly with the ‘passing test’ family of methods, although it looks more complicated in this simple case.