ios - 当 objectforkey 存在时,Object For Key 返回 Null

标签 ios ios7 for-loop nsarray nsdictionary

我正在运行一个 for 循环,它找不到键的对象。我尝试放入一个 if 语句以查看它是否会产生任何不同的结果并且它没有记录

这是我的 DescriptionDictionary(它的日志)

{
    (
    "Apple's big event: Here's what to expect",
    "WeChat helps Apple rack up bonus points in China",
    "China Mobile Wants Apple's iPhone 6 to be a Blowout Success",
    "What Will Apple Announce On September 9th? Software and Hardware Predictions For The iPhone 6 Event",
    "Apple seeks old magic with new products",
    "Five ways Apple's iPhone 6, iWatch launches may transform its business model",
    "Apple's Plan To Kill PayPal Has Finally Become Clear (AAPL)",
    "Apple's mojo on the line at unveiling",
    "Will Apple",
    "Apple adds new Flyover Tours Before the Big Launch for iOS 8 and OS X Yosemite ",
    "Apple: watching for new directions",
    "Apple Looks to Fashion World for iWatch Rollout",
    "Apple website to live stream iPhone 6 launch event only for Apple devices",
    "Is Apple planning a catwalk show for its iPhone/iWatch event?"
) =     (
    "After months of anticipation, the tech industry event of 2014 is upon us: Apple's product launch day. The tech giant has said almost nothing publicly about what's in store Tuesday, but industry observers have some ideas. Apple (AAPL, Tech30) is widely ...",
    "BEIJING (Reuters) - Apple Inc has a lot for which to thank people like Deng. A Beijing-based quality analyst, she gave only her surname as she's embarrassed by how much money she spends playing mobile games on WeChat, a hugely popular messaging app ...",
    "Apple Store in Pudong Shanghai. Source: Apple. Earlier this week the world's largest mobile carrier, China Mobile (NYSE: CHL ) , launched a pre-order site for the new, not-yet-released Apple (NASDAQ: AAPL ) iPhones. It wasn't the first time the carrier has ...",
    "For fans and followers, journalists and analysts, to pop culture experts, celebrities, and late night chat show hosts, this Tuesday is going to be just like Christmas Day. While Apple has not said what will be announced at its September 9th event ...",
    "With its highly awaited product launch this week, Apple is aiming for a new ",
    "Apple is building a giant white cube outside of the Flint Center in Cupertino, CA. James Martin/CNET Apple's launch of the larger screen iPhone 6 and preview of iWatch or a similar wearable will aim to silence critics who contend the company's innovation ...",
    "Apple has been quietly putting together a plan to blow open the mobile-payments industry, making major deals with credit-card companies in a move that could threaten the dominance of PayPal and other mobile-payment companies. These deals could make Apple ...",
    "SAN FRANCISCO ",
    "Just this past week we released our tour of Motorola",
    "Apple knows they need to work on Maps. Improving their product started with faster turnarounds for updates and correcting errors and it continues with 3D flyover tours for more an more cities. A flyover tour will not make the Maps app more useful, but it ...",
    "San Francisco (AFP) - Apple's mystery unveiling on Tuesday is expected to be a watershed moment for the California giant -- and the entire tech industry. Here are key things to watch for: - Can Tim Cook step up? - Chief executive Tim Cook will seek to ...",
    "As intrigue builds around the mystery wearable product Apple is rumored to be launching next week, a new thread has been added to the story: high fashion. According to a new report, fashion journalists have been invited to Apple's Sept. 9 event, a move ...",
    "The Apple launch event is the most teased event for a while now, and this made the Apple to want every Apple users to watch the video by streaming the event on the Apple website. Apple wants each and everyone on this globe to watch the launch event of the ...",
    "There's a mysterious white building being built at the site of Apple's Tuesday event. Fashion bloggers have been invited. Ergo? Could there be a glorious catwalk lurking inside Apple's mystery cube? James Martin/CNET I took all my clothes to the dry ..."
);
}

这是我的 key 组

"Apple's big event: Here's what to expect",
"WeChat helps Apple rack up bonus points in China",
"What Will Apple Announce On September 9th? Software and Hardware Predictions For The iPhone 6 Event",
"Apple's Plan To Kill PayPal Has Finally Become Clear (AAPL)",
"China Mobile Wants Apple's iPhone 6 to be a Blowout Success",
"Apple's mojo on the line at unveiling",
"Five ways Apple's iPhone 6, iWatch launches may transform its business model",
"Apple seeks old magic with new products",
"Apple: watching for new directions",
"Apple Looks to Fashion World for iWatch Rollout",
"Will Apple",
"Apple adds new Flyover Tours Before the Big Launch for iOS 8 and OS X Yosemite ",
"Is Apple planning a catwalk show for its iPhone/iWatch event?",
"Apple Hires Australia"

当我运行这个 for 循环时,它总是不记录

for(NSString *string in _titleArray) {
    if([_descriptionDict objectForKey:string]) {
        NSLog(@"YES");
    } else {
        NSLog(@"NO");
    }
}

编辑 由于第一个答案知道字典是这样的,问题继续。我如何把它变成一个普通的字典

************编辑*********** 看来我根据答案正确地形成了它们,这就是它的形成方式

    descForTitles = [NSDictionary dictionaryWithObject:descriptions forKey:titleArr];

    NSLog(@"Description Dict:%@", descForTitles);

但我还是以同样的方式记录下来

最佳答案

看起来您的原始字典是一个大数组到另一个大数组的映射。字典应该这样设置

@{@"key": @"object",
  @"otherKey": @"otherObject"};

它会像这样记录:

{
key = object;
otherKey = otherObject;
}

括号表明您在映射的每一侧都有一个数组。

你有更多像这样的东西:

@{@[@"key", @"otherKey"]:
  @[@"object", @"otherObject"]};

像这样的日志,看起来像你有的。

{
(
    key,
    otherKey
) =     (
    object,
    otherObject
);
}

如果您一开始就以数组的形式获取这些数据,您可以考虑使用 NSDictionary 类方法 [NSDictionary dictionaryWithObjects: forKeys:],它将两个数组作为其参数并将正确地构建一个将一个与另一个相关联的字典。

关于ios - 当 objectforkey 存在时,Object For Key 返回 Null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25715568/

相关文章:

ios - 如何用字符串数组中的 %20 填充空字符串 ios

iOS 7 : UITableView shows under status bar

ios - 使用 UIImageView 调整 UIImage

快速切换语句

ios - NSXMLParser didStartElement 仅具有特定属性

objective-c - 如何从自定义操作进入删除确认状态 UITableViewCell?

JavaScript - 大索引值导致无限循环 - 不确定为什么?

c++ - 每个 ( in ) 和 for ( : )? 之间有什么区别

ios - 对 UIViewController 的某些部分启用 UserInteraction

ios - 建立网络连接后,我可以让 iOS 7 在后台发送图片吗?