Why doesn't cast work from NSData to String? Swift

前端 未结 3 2008
旧时难觅i
旧时难觅i 2020-12-12 02:28

I have that code

func SendRequest(request: String) -> String
{
    var response = \"\"
    var (success, errmsg) = client.connect(timeout: 1)
    if succe         


        
3条回答
  •  抹茶落季
    2020-12-12 02:40

    Answering: "Sometimes, cast NSData to String doesn't work."
    Not all data is convertible to a particular string encoding. In the case:

    var str = String.init(data: bytes, encoding: NSUTF8StringEncoding)
    

    there are a great number of bytes and byte sequences that have no valid UTF-8 character encoding.

提交回复
热议问题