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

前端 未结 3 2009
旧时难觅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 03:02

    Some possibilities to examine include whether or not data actually contains any bytes, and whether or not it's actually a valid UTF-8 string. Assuming this is an http request/response that you're handling, check the Content-type, in particular the charset property. If the data's encoded as something other than text , or it's not UTF-8 format, it won't convert to a string.

提交回复
热议问题