如何解码目标c中的特殊字符

时间:2016-02-04 12:54:31

标签: ios objective-c decode encode

我是iphone开发和演示应用程序的新手,因为我得到一个像“abc%26def”的字符串,我想解码这种类型的文本到实际测试,我知道它的基本问题但仍然我找不到链接的任何解决方案,那么anybuddy请帮助我吗?

我的代码如下

 if(i%3==0)
                {
                    [dataDictionary setValue:[dataDictionary valueForKey:TRIPPER__KEY] forKey:CONTENT_KEY];
                    [dataDictionary setValue:CELL_TEXTFIELD_TRIPPER_COUNT forKey:@"cellIdentifier"];
                }
                else if(i%3==1)
                {

                    //*name = [(NSString *)self stringByReplacingOccurrencesOfString:@"+" withString:@" "];
                    NSString *name = [[dataDictionary valueForKey:NAME_KEY] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
                    [dataDictionary setValue:name forKey:CONTENT_KEY];
                    [dataDictionary setValue:CELL_TEXTFIELD_TRIPPER_NAME forKey:@"cellIdentifier"];
                    [dataDictionary setValue:@"Triper Name" forKey:@"placeHolder"];
                }

2 个答案:

答案 0 :(得分:1)

我认为这肯定会有用

NSString *str = @"abc%26def";   //put any string that you want
NSCharacterSet *setToKeep = [NSCharacterSet characterSetWithCharactersInString:@"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"];
NSCharacterSet *setToRemove = [setToKeep invertedSet]; //inverted set will have all other remaining symbol, char etc
NSString *newString =[[str componentsSeparatedByCharactersInSet:setToRemove]componentsJoinedByString:@""]; //this is the decoded string
NSLog(@"%@",newString);

如果你需要更多的东西,我会修改答案。

答案 1 :(得分:0)

使用此:

NSString *name = [dataDictionary valueForKey:NAME_KEY];

代替:

NSString *name = [[dataDictionary valueForKey:NAME_KEY] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

<强>更新

[[dataDictionary valueForKey:NAME_KEY] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];