代码:
var postString: NSString = NSString .stringByAppendingFormat("userName=\(userName) & password = \(Password)")
为什么我收到错误:
(NSString, CVarArgType...) -> NSString' is not convertible to NSString.
答案 0 :(得分:5)
stringByAppendingFormat:
是NSString
上的实例方法,而不是类方法。因此,您无法使用NSString.stringByAppendingFormat(...)
调用它,您必须在现有字符串上调用它:
var someExistingString = "..."
var postString = someExistingString.stringByAppendingFormat("userName = %@ & password = %@", userName, password)
话虽如此,如果您只是从头开始构建一个新字符串,则不需要使用stringByAppendingFormat
,您可以直接使用Swift的String Interpolation:
var postString = "userName = \(userName) & password = \(password)"
答案 1 :(得分:0)
您收到该错误是因为您尝试在类stringByAppendingFormat
上调用实例方法NSString
。这是一个编译的例子:
var userName = "Dave"
var Password = "password"
var select = "SELECT * FROM users WHERE "
var postString: NSString = select.stringByAppendingFormat("userName=\(userName) & password = \(Password)")
注意:由于SQL Injection,你永远不想做这样的事情。