如何解码 objective c 中的特殊字符

How to decode special characters in objective c

我是 iphone 开发和开发演示应用程序的新手,因为我得到了一个像 "abc%26def" 这样的字符串,我想将这种类型的文本解码到实际测试中,我知道这是一个非常基本的问题,但我仍然无法从 link 找到任何解决方案,所以任何朋友都可以帮助我吗?

我的代码如下

 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"];
                }

使用这个:

NSString *name = [dataDictionary valueForKey:NAME_KEY];

而不是:

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

更新

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

我认为这肯定有效

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);

如果您还需要更多内容,请发表评论,我会修改答案。