Conversion error NSData in NSString

2

I have the following code to convert the body of a post request into a string

let body: NSData = self.createBodyWithParameters(paramString, filePathKey: "image_name", imageDataKey: imageData!, boundary: boundary, nameParam: "dparam_string_post")
let bodyString: String = NSString(data: body, encoding: NSUTF8StringEncoding) as! String

I get an error on the second line:

fatal error: unexpectedly found nil while unwrapping an Optional value

And the variable has content, I have made a print and it has shown me the content

EDITED I have also tried with:

let cadena = String(data: body, encoding: NSUTF8StringEncoding)

But it returns me nil

    
asked by 22.08.2016 в 08:58
source

1 answer

2

Do it like this:

let newStr : String? = String(data: data, encoding: NSUTF8StringEncoding)

And be careful because the variable is an optional variable. I mean it's a String type? No String.

    
answered by 23.08.2016 в 11:41