ios中将NSString类型转化为NSMutableDictionary类型

例如:NSString=@"id=123&name=csdn&age=1&content=what&";

-(NSMutableDictionary *)changeStringToDictionary:(NSString *)string{

  NSMutableDictionary *returnDic=[[[NSMutableDictionary  alloc]  init]  autorelease];

    

    NSArray *parmArray=[stringcomponentsSeparatedByString:@"&"];//将字符串切割为数组

    NSLog(@"count:%d",parmArray.count);//count=4,格式是:[id=123,name=csdn,age=1,content=what],其中id,name,age,content对应的下标分别是0,1,2,3


    for (NSString *sStringinparmArray) {//遍历parmArray里面的每一个元素

        

        NSArray *sArray=[sString componentsSeparatedByString:@"="];//id=3被拆成数组:[id,3];id和3对应的下标分别为0,1

        [returnDic setObject:[sArray objectAtIndex:1] forKey:[sArrayobjectAtIndex:0]];//组成键值对

        

    }

    return returnDic;

    

}


你可能感兴趣的:(IOS)