IOS swift NSMutableData doesn't have appendString member

Hello, I am new to swift and I am following a tutorial and creating the same code to upload an image. I am using swift 3 now and it seems that NSMutableData () no longer has an appendString method available , what can I do as a replacement? The following tutorial is here http://swiftdeveloperblog.com/image-upload-example/ and my code is this

func createBodyWithParameters(parameters: [String: String]?, filePathKey: String?, imageDataKey: NSData, boundary: String) -> NSData { let body = NSMutableData() if parameters != nil { for (key, value) in parameters! { body.("--\(boundary)\r\n") body.appendString("Content-Disposition: form-data; name=\"\(key)\"\r\n\r\n") body.appendString("\(value)\r\n") } } let filename = "user-profile.jpg" let mimetype = "image/jpg" body.appendString(options: <#T##NSData.Base64EncodingOptions#>)("--\(boundary)\r\n") body.appendString("Content-Disposition: form-data; name=\"\(filePathKey!)\"; filename=\"\(filename)\"\r\n") body.appendString("Content-Type: \(mimetype)\r\n\r\n") body.appendString("\r\n") body.appendString("--\(boundary)--\r\n") return body } 

Again the problem is with appendString as I get the error message:

value of type NSMutableData has no member appendString

I was looking for work around, but could not find it, and I have an append method, but it does not accept a string.

+6
source share
3 answers

As @dan notes in the comments, this method is part of the tutorial you are looking at. Its ease in Swift without defining a custom method.

First, do not use NSMutableData ; instead, use a new Data structure that will be either mutable or immutable depending on whether you use var or let :

 var body = Data() 

Then add the bytes of UTF-8:

 body.append(Data("foo".utf8)) 

(There are other String methods for other encodings if you need something other than UTF-8.)

If you need the exact behavior of a tutorial, then how to translate its method in Swift 3:

 extension Data { mutating func append(string: String) { let data = string.data( using: String.Encoding.utf8, allowLossyConversion: true) append(data!) } } … body.append("foo") 

I would not recommend this code for two reasons. First, lossy conversion means your application can silently discard important data. Secondly, a power reversal ( data! Instead of Data ) means that in case of an encoding error, your application will crash, and not display a useful error.

+11
source
 extension NSMutableData { func appendString(_ string: String) { let data = string.data(using: String.Encoding.utf8, allowLossyConversion: true) append(data!) } } 

This work with Me Swift 3

+2
source

I solved this problem in swift 4 by adding the following lines after the ViewController class:

 extension NSMutableData { func appendString(string: String) { let data = string.data(using: String.Encoding.utf8, allowLossyConversion: true) append(data!) } } 

I am referring to the Viewcontroller class, the class created when adding the ViewController to your project. an example of the following class:

  class ViewController2: UIViewController ,UINavigationControllerDelegate , UIImagePickerControllerDelegate{ // my code } 
0
source

Source: https://habr.com/ru/post/1013229/


All Articles