iOS swift NSMutableData没有成员appendString [英] iOS swift NSMutableData has no member appendString
问题描述
你好,我是Swift的新手,我正在学习一个教程并创建相同的代码以上传图片.我现在使用的是swift 3,看来 NSMutableData()不再具有 appendString 方法可用,我该怎么做?我正在关注的教程在这里 http://swiftdeveloperblog.com/image-upload-example/而我的代码就是这个
Hello I am new to swift and I am following a tutorial and creating the same code in order to Upload an image . I am now using swift 3 and it seems like NSMutableData() no longer has the appendString method available what can I do as a substitute ? The tutorial I am following 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
}
再次出现问题是 appendString ,因为我收到了错误消息:
Again the issue is with the appendString as I am getting the error:
值没有成员appendString
我一直在寻找解决方法,但没有找到任何解决方法,并且我有可用的 append 方法,但是它没有String.
I been searching for work arounds but have not found any and I have the append method available but it does not take a String.
推荐答案
正如@dan在评论中指出的那样,该方法是您正在查看的教程的一部分.不过,在Swift中无需定义自定义方法就足够了.
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.
首先,不要使用NSMutableData
;而是使用新的Data
结构,该结构是可变的还是不可变的,具体取决于您使用的是var
还是let
:
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()
然后追加UTF-8字节:
Then to append UTF-8 bytes:
body.append(Data("foo".utf8))
(如果您需要使用UTF-8以外的其他编码,还有其他String
种方法用于其他编码.)
(There are other String
methods for other encodings if you need something other than UTF-8.)
如果您想了解本教程的确切行为,请按照以下方法将其方法转换为Swift 3:
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")
不过,出于两个原因,我不建议您使用此代码.首先,有损转换意味着您的应用可能会静默丢弃重要数据.其次,强制解包(data!
而不是data
)意味着在编码出现问题时,您的应用将崩溃,而不显示有用的错误.
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.
这篇关于iOS swift NSMutableData没有成员appendString的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!