【问题标题】:ObjectForKey - How to check if ObjectForKey has @"String"ObjectForKey - 如何检查 ObjectForKey 是否有 @"String"
【发布时间】:2012-05-24 12:57:37
【问题描述】:

我有这条线...

 if(![[[array objectAtIndex:indexPath.row] objectForKey:@"special_trip"] isEqualToString:@""]) {

一切都很酷.. 但是我的 special_trip 有很多信息,所以我想仅以具有字符串 Airport in 的结果为例:1:Airport, Packet

我只想获取那些在整个字符串中包含 Airport 的结果。

提前致谢。

编辑:抱歉造成混淆。

【问题讨论】:

  • 我可能会误会,但如果你想检查的话,你不能直接做isEqualToString:@"Airport"吗?
  • if([[[array objectAtIndex:indexPath.row] objectForKey:@"special_trip"]){//has string}else{//no value}。提供 [[[array objectAtIndex:indexPath.row] objectForKey:@"special_trip"] 输出以查找您的字符串
  • 艾米是我做的,但是special_trip可能有这个:1:机场,包2:机场3:机场,包,信
  • 谢谢大家.. 我这样做了: if([[[array objectAtIndex:indexPath.row] objectForKey:@"special_trip"] rangeOfString:@"port"].location != NSNotFound) {

标签: iphone xcode ios5 xcode4


【解决方案1】:

使用这个:

if([myString rangeOfString:@"Airport"].location != NSNotFound ) {
    // myString contains Airport
}

【讨论】:

  • 我开始有了这个想法……如果我得到结果,我会告诉你的。谢谢
  • 最后它像这样结束: if([[[array objectAtIndex:indexPath.row] objectForKey:@"special_trip"] rangeOfString:@"port"].location != NSNotFound) { 谢谢你非常喜欢。
【解决方案2】:

我认为你正在寻找函数

NSArray *filteredArray = [myArray filteredArrayUsingPredicate:predicate];

看看Predicate Programming Guide

【讨论】:

  • 谢谢哥们,我找到了答案。但我读到了一些新东西。真的很感激。
【解决方案3】:

如果我没听错.....你为什么不使用这条简单的线-

if([[[array objectAtIndex:indexPath.row] objectForKey:@"special_trip"] isEqualToString:@"Airport"])
{
   //
}

如果我错了:-(请告诉我到底是什么问题。

谢谢!

【讨论】:

  • 问题是关键专程有很多信息。像这样:1:机场,包
  • if([[[array objectAtIndex:indexPath.row] objectForKey:@"special_trip"] rangeOfString:@"port"].location != NSNotFound) { 这是有效的 :)
  • 那又怎样 - if([[[array objectAtIndex:indexPath.row] objectForKey:@"special_trip"] isEqualToString:@"Airport"]) 这是过滤字符串......如果你只想要@“Airport”这肯定会工作......恭喜你解决了你的问题...... :-)
  • 如示例: 1:Airport,Packet 是一个完整的字符串,其值为@"Airport,Packet"。不只是Airport,因此语句通过导致它不等于Airport。
  • 哦好的.......现在你的问题已经完成了......对不起,我没有得到你,你没有在你的问题中写下完整的细节。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-02-16
  • 1970-01-01
  • 2013-04-16
  • 2012-03-06
相关资源
最近更新 更多