2016-02-04 26 views
1

我是新手iphone發展和工作的演示應用程序,因爲我得到像「abc%26def」字符串,我想解碼這種類型的文本到實際測試,我知道它非常基本的問題,但我仍然無法從鏈接中找到任何解決方案,所以Anybuddy可以爲我提供幫助嗎?如何解碼目標中的特殊字符c

我的代碼如下

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

http://stackoverflow.com/questions/4669132/how-to-decode-utf8-string-in-iphone它對你有用嗎? –

+0

請不要在詞典中使用'valueForKey'。使用'objectForKey'或'dataDictionary [NAME_KEY]'。 'valueForKey'不會做你認爲它做的事。 – Sulthan

回答

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

評論工作,如果你需要比這更多的東西,我會修改答案。

0

使用本:

NSString *name = [dataDictionary valueForKey:NAME_KEY]; 

代替:

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

更新

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

它給了我這樣的結果像這樣的雪茄%26makwan –

+0

平均名稱是給你jigar%26makwan on [dataDictionary valueForKey:NAME_KEY]? – KDeogharkar

+0

是的確實frend.thing是,我從遠程服務器得到這respnse,我想解碼他們到實際的字符,意味着「%26」應該轉換爲「&」一個「%20」的白色空間... etc –