如何访问 NSMutableDictionary 的键?

发布于 2024-07-28 22:11:57 字数 829 浏览 10 评论 0原文

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

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

发布评论

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

评论(2

海夕 2024-08-04 22:11:57
for (NSString * key in [bookmarks allKeys])
{
  if ([key hasPrefix:@"Stanford"])
  {
    NSLog(key);
  }
}
for (NSString * key in [bookmarks allKeys])
{
  if ([key hasPrefix:@"Stanford"])
  {
    NSLog(key);
  }
}
清晰传感 2024-08-04 22:11:57

在某些情况下,过滤集合是通过“通过测试”系列方法最清楚地完成的,尽管在这个简单的情况下看起来更复杂。

NSArray *keys = [bookmarks allKeys];
NSArray *stanfordKeys = [keys objectsAtIndexes:[keys indexesOfObjectsPassingTest:^BOOL(NSString *key, NSUInteger idx, BOOL *stop) {
    return [key hasPrefix:@"Stanford"];
}]];
NSLog(@"%@", stanfordKeys);

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.

NSArray *keys = [bookmarks allKeys];
NSArray *stanfordKeys = [keys objectsAtIndexes:[keys indexesOfObjectsPassingTest:^BOOL(NSString *key, NSUInteger idx, BOOL *stop) {
    return [key hasPrefix:@"Stanford"];
}]];
NSLog(@"%@", stanfordKeys);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文