【问题标题】:Parse the string and append the resultant values解析字符串并附加结果值
【发布时间】:2012-05-01 10:57:04
【问题描述】:

如何解析下面的字符串

{
City = "New York";
Country = "United States";
CountryCode = us;
}

并将 "" 内的值附加在一起,并省略字符串的其余部分。我需要将修改后的字符串设为“New York, United States”。

CFStringRef address = ABMultiValueCopyValueAtIndex(multiValue, identifier);

当我将CFStringRef 转换为NSString 时,我得到了上面记录的表单。我将如何从字符串中检索城市/国家/地区值

【问题讨论】:

  • 你从哪里得到的字符串?为什么不使用标准数据格式,例如 JSON?让一切变得更容易。
  • 你的代码应该有这个字符串的字典对象。因此,请将您的代码显示到必要的部分,这将对您和其他人有所帮助..
  • @JeanPaulScott 查看我的回答(和我的编辑)。

标签: iphone objective-c nsstring


【解决方案1】:

如果您从网络接收此数据字符串(可能是 JSON),您可以像这样处理数据(iOS 5):

- (void)processData:(NSData *)responseData {
        NSError* error;
        NSDictionary* json = [NSJSONSerialization 
            JSONObjectWithData:responseData
            options:kNilOptions 
            error:&error];
        NSString* city = [[json objectForKey:@"Address"] objectForKey:@"City"];
        NSString* country = [[json objectForKey:@"Address"] objectForKey:@"Country"];
        NSString* result = [city stringByAppendingFormat:@", %@",country];
        NSLog(@"%@", result); //New York, United States
    }

相反,如果这个字符串是一个字典表示,正确的格式应该是这样的:

NSString *str=@"Address = {" 
                @"City = \"New York\";"
                @"Country = \"United States\";"    
                @"CountryCode = us; };";

所以如果你真的想从 NSString 传递到 NSDictionary 你可以这样使用NSPropartyListSerialization

NSError* error;
NSData *dat=[str dataUsingEncoding:NSUTF8StringEncoding];
NSPropertyListFormat plistFormat;
NSDictionary *temp = [NSPropertyListSerialization propertyListWithData:dat options:NSPropertyListImmutable format:&plistFormat error:&error];
NSString* city = [[temp objectForKey:@"Address"] objectForKey:@"City"];
NSString* country = [[temp objectForKey:@"Address"] objectForKey:@"Country"];
NSString* result = [city stringByAppendingFormat:@", %@",country];
NSLog(@"%@",result);

编辑(根据您更新的问题):

您发布的是字典,而不是数组。字典由键值标识的一组元素组成。数组由一组由索引标识的元素组成。因此,如果数组中的元素是字符串,则必须对每个字符串进行解析。这通常不是最好的方法,正如@FelixKling 所说,您还应该使用标准格式,如 json、xml 等。

【讨论】:

  • 在代码行NSData *dat=[str dataUsingEncoding:NSUTF8StringEncoding]; 我得到这个异常-[__NSCFDictionary dataUsingEncoding:]: unrecognized selector sent to instance 0xdd7f700 2012-05-01 17:53:54.589 smartJoinder[4773:15803] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFDictionary dataUsingEncoding:]: unrecognized selector sent to instance 0xdd7f700'
  • 继续....*** First throw call stack: (0x19b0022 0x1e47cd6 0x19b1cbd 0x1916ed0 0x1916cb2 0x64a78 0x9b6a1e 0x9b6d11 0x9c88fd 0x9c8aef 0x9c8dbb 0x9c985f 0x9c9e06 0x9c9a24 0xb4e65 0x7b85c5 0x7cfcc4 0x7d3e08 0x9805c5 0x9807fa 0x121585d 0x1984936 0x19843d7 0x18e7790 0x18e6d84 0x18e6c9b 0x276d7d8 0x276d88a 0x8ef626 0x24ad 0x2425) terminate called throwing an exception
  • 我刚刚在我的机器上进行了测试,它工作正常,你确定你以正确的格式作为字符串而不是字典传递str
  • 我得到的字符串是 CFStringRef CFStringRef address = ABMultiValueCopyValueAtIndex(multiValue, identifier); 当我直接记录它或将它转换为 NSString 时,我得到了上面的日志。
  • 日志告诉您,您在字典而不是字符串上调用 dataUsingEncoding(字典没有该方法),请确保您在该方法中传递的对象类型。但是,我给你的那些例子是可能的,不一定直接适用于你的问题。祝你好运:)
猜你喜欢
  • 2022-01-03
  • 1970-01-01
  • 2023-03-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-20
  • 1970-01-01
相关资源
最近更新 更多