iOS Swift Nsmutabledata Has No Member Appendstring

iOS swift NSMutableData has no member appendString

As @dan points out in the comments, that method is part of the tutorial you’re looking at. It’s easy enough in Swift without defining a custom method, though.

First, don’t use NSMutableData; instead, use the new Data struct, which will be either mutable or immutable depending on whether you use var or let:

var body = Data()

Then to append UTF-8 bytes:

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

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

If you want the exact behavior of the tutorial, here’s how to translate its method to 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 wouldn’t recommend this code, though, for two reasons. First, the lossy conversion means that your app may silently discard important data. Second, the force unwrap (data! instead of data) means that in case of encoding trouble, your app will crash instead of displaying a useful error.

swift3 - uploading image to webserver

You cannot initialize Data like this:

Data("foo")

What you can do is:

Data("foo".utf8)

Like in the link you provided. (You are missing the .utf8)

EDIT: Yes the guy said he didn't want to use that code, but hear me out, this version of the extension doesn't allow lossy connection, and as long as you write anything inside the string, data will never be nil.

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

Usage:

body.appendString("foo")

Just initialize body like this:

var body = NSMutableData()

And return like this

return body as Data

iOS: Swift 2 to Swift 3: Convert Strings to Data

Solution looks like:

func createBodyWithParameters(parameters: [String: String]?, filePathKey: String?, imageDataKey: NSData, boundary: String) -> NSData {

let body = NSMutableData()

if parameters != nil {
for (key, value) in parameters! {
body.appendString(string: "--\(boundary)\r\n")
body.appendString(string: "Content-Disposition: form-data; name=\"\(key)\"\r\n\r\n")
body.appendString(string: "\(value)\r\n")
}
}

let filename = "logo.jpg"
let fileNo2 = imageView.image;
print("fileNo2")
let mimetype = "image/jpg"

body.appendString(string: "--\(boundary)\r\n")
body.appendString(string: "Content-Disposition: form-data; name=\"\(filePathKey!)\"; filename=\"\(filename)\"\r\n")
body.appendString(string: "Content-Type: \(mimetype)\r\n\r\n")
body.append(imageDataKey as Data)
body.appendString(string: "\r\n")

body.appendString(string: "--\(boundary)--\r\n")

return body
}

extension NSMutableData {

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

NSURLRequest?' does not have a member named 'URL' - Swift

request is an optional property on UIWebView:

var request: NSURLRequest? { get }

also stringByAddingPercentEscapesUsingEncoding returns an optional:

func stringByAddingPercentEscapesUsingEncoding(_ encoding: UInt) -> String?

What you need is to make user of optional binding in a few places:

if let toShorten = webView.request?.URL.absoluteString {

if let encodedURL = toShorten.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding) {

let urlString = "http://api.x.co/Squeeze.svc/text/\(GoDaddyAccountKey)?url=\(encodedURL)"
shortURLData = NSMutableData()

if let firstrequest = NSURL(string: urlString) { // If a method can return a nil, don't force unwrap it
let request = NSURLRequest(URL:first request)
shortenURLConnection = NSURLConnection(request:request, delegate:self)
shortenButton.enabled = false
}

}
}

See Apple's docs on optional chaining for details

See Apple's docs for NSURL class



Related Topics



Leave a reply



Submit