2013-12-20 41 views
0

不知道我要出错的地方。我有一个最多包含3个对象的数组。我想检查是否有任何数组对象继续使用0,&,如果是,则将它们格式化为NSString。如果没有,我想包括在索引0的对象。想要检查一个数组是否包含全部或只包含某些对象 - iOS

在正确的方向一个点将是伟大的!

// add annotation 
MKPointAnnotation *point = [MKPointAnnotation new]; 
point.coordinate = (CLLocationCoordinate2D){[self.eventPlan.location_lat doubleValue], [self.eventPlan.location_lng doubleValue]}; 
NSArray *locationA = [self.eventPlan.location_address componentsSeparatedByString:@", "]; 
point.title = locationA[0]; 

if ([locationA containsObject:locationA[1]]) { 
    point.subtitle = [NSString stringWithFormat:@"%@, %@", locationA[1], locationA[2]]; 
} else { 
    point.subtitle = [NSString stringWithFormat:@"%@", locationA[1]]; 
} 

[mapView addAnnotation:point]; 
+0

更新:有没有一种方法来检查我的数组没有延伸超过索引[0]? –

+0

locationA.count会告诉你有多少物体,是你问的? – rdelmar

回答

0

如果你知道,只能有最多3条记录数组中,你可以做一些幼稚的,如:

switch([locationA count]) 
{ 
    case 0: 
     ... 
     break; 
    case 1: 
     ... 
     break 
    case 2: 
     ... 
     break; 
    case 3: 
     ... 
     break; 
} 

然后你根据有多少所需要的。

你的代码对我来说是什么样的,你只是在“,”的第一个实例中断了你的字符串。另一个简单的方法是找到第一个分隔符的范围,然后将该字符串剪裁为两个子字符串。

NSRange range = [string rangeOfString:@", "]; 
int locationInString = range.location; 
if(locationInString != NSNotFound) 
{ 
    point.title = [string substringToIndex:locationInString]; 
    point.subtitle = [string substringFromIndex:locationInString + 2]; 
} 
else 
    point.title = string; 

如果字幕是零,那么你知道你没有这部分字符串。

相关问题