将String存储为逗号分隔值

时间:2016-04-26 13:13:19

标签: ios objective-c nsstring nsdictionary

我按如下方式迭代NSDictionary,如果它包含字符串htttp,则将值分配给字符串。

for(id key in photos){

                     //NSLog(@"key=%@ value=%@", key, [photos objectForKey:key]);
    if ([[[photos objectForKey:key] stringByAppendingString: key] containsString:@"http"]){
    photoUrl = [[photos objectForKey:key] stringByAppendingString: key];
                     }
    NSLog(@"etf %@",photoUrl);

 }
  1. 如何创建逗号分隔的网址并将其分配给photoUrl。例如,nsdictionary可能包含多个url,因此我想将所有url连接为逗号分隔值并稍后将其分配给photoUrl?
  2. 如何在以后检索这些逗号分隔值?

2 个答案:

答案 0 :(得分:1)

如果您从某个阵列获得

NSString *joinedComponents = [array componentsJoinedByString:@","];

否则你可以遍历一些自己的集合

NSMutableString * string1 = [[NSMutableString alloc] initWithString:@"this is my string"];

[string1 appendString:@" with more strings attached"];

同样,你可以稍后将它们分开

NSArray *items = [string1 componentsSeparatedByString:@","];

将键值保存在字符串中并制作您的网址。

答案 1 :(得分:1)

在循环中创建一个数组,

NSArray *urls = [NSMutableArray array];
for (id key in photos) {
  NSString *photoUrl = [[photos[key] stringByAppendingString: key]
  if (photoUrl containsString:@"http"]){
    urls appendItem[photoUrl];
  }
}

然后您可以使用

创建CSV字符串
NSString *csvUrls = [urls componentsJoinedByString:@","];

并用

将其分解
NSArray *urls = [csvUrls componentsSeparatedByString:@","];