簡體   English   中英

如何正確編碼和解碼字符串?

[英]How to encode and decode the string properly?

我從Web服務獲得字典,並且該字典包含已編碼的鍵值對。 我使用了一些代碼來解碼此鍵值。

代號 1:

[NSString stringWithUTF8String:[[dict valueForKey:@"desc"] cStringUsingEncoding:[NSString defaultCStringEncoding]]];

但是,當我運行此代碼時,它會崩潰並出現錯誤[NSString stringWithUTF8String:]: NULL cString'

調試時,字典包含鍵和值,還有包含值的“ desc”鍵。

另一方面,我有一個運行良好的代碼。

代號 2:

[NSString stringWithFormat:@"%@",[dict valueForKey:@"desc"]];

但是,在我的應用程序中幾乎所有地方,我都使用了1號代碼,並且效果很好。

現在我完全困惑了我這個錯誤[NSString stringWithUTF8String:]: NULL cString'當我檢查字典中是否包含鍵和值時,出現了[NSString stringWithUTF8String:]: NULL cString'

為什么要從NSString創建C-String,然后再從C-String創建(反向) NSString

為什么不簡單

NSString *desc = dict[@"desc"];

並且-與往常一樣- 請勿使用valueForKey除非您可以解釋為什么您明確需要KVC。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM