从字符串中获取特定值

时间:2014-06-09 12:04:20

标签: ios objective-c ios7

在我的应用程序中我正在使用ArcGIS。在那个我想搜索一个地方,并得到与该名称相关的地方。现在我想解析那些地方的所有地址并将它们放在表格视图中。键入地址后,按下搜索按钮后,我将获得地图上的所有标记。现在我试图获得地方的完整地址但我无法做到这一点。我想得到" place_addr"来自以下字符串的值可以任何一个请帮助我如何在ios中获得该值

AGSFindLocationResult name=Disneyland, extent=AGSEnvelope: xmin = -13129988.221853, ymin =       4000701.846518, xmax = -13124422.247314, ymax = 4007401.106877, spatial reference: [AGSSpatialReference: wkid = 102100, wkt = null], graphic=geometry: AGSPoint: x = -13127204.667522, y = 4004050.640580, spatial reference: [AGSSpatialReference: wkid = 102100, wkt = null], symbol: { AGSPictureMarkerSymbol: imageName: BluePushpin.png, width: 36.000000, height: 36.000000 },       attributes: {
AddBldg = "";
AddNum = "";
AddNumFrom = "";
AddNumTo = "";
"Addr_type" = POI;
City = Anaheim;
Country = USA;
DisplayX = "-117.923687";
DisplayY = "33.815467";
Distance = 0;
LangCode = "";
"Loc_name" = "Gaz.WorldGazetteer.POI2";
"Match_addr" = Disneyland;
Nbrhd = "";
Phone = "(714)781-4565";
PlaceName = Disneyland;
"Place_addr" = "1313 S Disneyland Dr Anaheim, CA 92802";
Postal = "";
PostalExt = "";
Rank = "2.5";
Region = California;
Score = 100;
Side = "";
StAddr = "";
StDir = "";
StName = "";
StPreDir = "";
StPreType = "";
StType = "";
Subregion = Orange;
Type = "Amusement Park";
URL = "";
X = "-117.923687";
Xmax = "-117.898691";
Xmin = "-117.948691";
Y = "33.815467";
Ymax = "33.84047000000002";
Ymin = "33.79047";
 },  visible: 1

2 个答案:

答案 0 :(得分:0)

如果您回来的确是一个字符串,那么您应该查看字符串搜索方法,例如rangeOfString。

这将返回包含您的标签“place_addr”的NSRange。

然后你想要从那一点向前搜索分号,使用相关方法rangeOfString:options:range:,搜索字符串的特定范围。

最后,您需要计算内容的范围,即在“place_addr”的NSRange结尾之后的5个字符,以及最终分号,但不包括最终分号。

然后你使用substringWithRange来提取你在上面计算的范围的字符串。

试一试,如果遇到麻烦就回来。

答案 1 :(得分:0)

请尝试以下代码

- (NSArray *)findKey:(NSString *)key inString:(NSString *)allString
{
    NSMutableArray *result = [[NSMutableArray alloc] initWithCapacity:1];
    NSArray *splitArray = [allString componentsSeparatedByString:@";"];

    for (NSString *s in splitArray) {
        NSArray * keyValueArry = [s componentsSeparatedByString:@"="];

        if([keyValueArry count] > 1)
        {
            NSString *_key = [keyValueArry objectAtIndex:0];
            NSString *_value = [keyValueArry objectAtIndex:1];
            if([_key isEqualToString:key])
            {
                [result addObject:_value];
            }
        }
    }
    return result;
}

其中key是place_addr,allString是你的样本字符串。