从另一个 NSDictionary 创建 NSDictionary?

发布于 2024-12-17 17:48:35 字数 397 浏览 1 评论 0原文

我有一个 NSDictionary 记录以下内容:

    address = "30 East 23rd Street";
    address1 = "30 East 23rd Street";
    address2 = "";
    age = 32;
    alert = "";
    "blood_pressure_1" = "";
    "blood_pressure_2" = "";
    bmi = "";

我想创建一个新的 NSDictionary,它只包含旧 NSDictionary 中的一些键,例如 agebmi< /code> 和警报。我该怎么做?

I have an NSDictionary that logs this:

    address = "30 East 23rd Street";
    address1 = "30 East 23rd Street";
    address2 = "";
    age = 32;
    alert = "";
    "blood_pressure_1" = "";
    "blood_pressure_2" = "";
    bmi = "";

I want to create a new NSDictionary that only contains a few keys from the old one, such as just age, bmi, and alert. How can I do this?

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

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

发布评论

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

评论(4

万人眼中万个我 2024-12-24 17:48:35

这可能会更容易一些:

NSArray *keys = [NSArray arrayWithObjects:@"address", @"address1", @"address2", nil];
NSDictionary *subset = [NSDictionary dictionaryWithObjects:
    [fullDictionary objectsForKeys:keys notFoundMarker:@""]
    forKeys:keys];

This might be a little easier:

NSArray *keys = [NSArray arrayWithObjects:@"address", @"address1", @"address2", nil];
NSDictionary *subset = [NSDictionary dictionaryWithObjects:
    [fullDictionary objectsForKeys:keys notFoundMarker:@""]
    forKeys:keys];
醉城メ夜风 2024-12-24 17:48:35
NSDictionary *dictionary = [[NSDictionary alloc] initWithObjectsAndKeys:
                            [oldDict objectForKey:@"address"],  @"address",
                            [oldDict objectForKey:@"address1"], @"address1",
                            [oldDict objectForKey:@"address2"], @"address2",
                            nil];

显然,您可以根据需要更改密钥......

NSDictionary *dictionary = [[NSDictionary alloc] initWithObjectsAndKeys:
                            [oldDict objectForKey:@"address"],  @"address",
                            [oldDict objectForKey:@"address1"], @"address1",
                            [oldDict objectForKey:@"address2"], @"address2",
                            nil];

You can obviously change the keys as needed...

汐鸠 2024-12-24 17:48:35
NSMutableDictionary* d = [NSMutableDictionary dictionaryWithDictionary: bigDict];
[d removeObjectsForKeys: [NSArray arrayWithObjects: @"address", @"age", nil]];
NSMutableDictionary* d = [NSMutableDictionary dictionaryWithDictionary: bigDict];
[d removeObjectsForKeys: [NSArray arrayWithObjects: @"address", @"age", nil]];
沉溺在你眼里的海 2024-12-24 17:48:35

更容易

NSDictionary *newDictionary = [oldDictionary dictionaryWithValuesForKeys:@[@"age", @"bmi", @"alert"]];

Even easier

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