[英]Failing in comparing returned value with string
I'm trying to return a value (string) from php to IOS application: 我正在尝试从php返回值(字符串)到IOS应用程序:
echo "1";
This is the swift code: 这是快速的代码:
let returnedData = NSString(data: unwrappedData, encoding: String.Encoding.utf8.rawValue) as! String
print(returnedData)
if returnedData == "1" {
... something
}
The print function shows the correct value (that is, 1). 打印功能显示正确的值(即1)。 But the check fails.
但是检查失败。
String(...) gives me Optional("1\\n\\n")
字符串(...)给我可选(“ 1 \\ n \\ n”)
That indicates that the output is followed by two newline characters, as in this example: 这表明输出后跟两个换行符,如本例所示:
let data = Data(bytes: [0x31, 0x0a, 0x0a])
if let string = String(data: data, encoding: .utf8) {
print(string) // 1
print(string.debugDescription) // "1\n\n"
print(string == "1") // false
}
debugDescription
is a very useful method to detect “invisible” string characters. debugDescription
是检测“不可见”字符串字符的非常有用的方法。
The solution is (compare Does swift have a trim method on String? ): 解决方案是(比较swift在String上是否有trim方法? ):
let trimmed = string.trimmingCharacters(in: .whitespacesAndNewlines)
print(trimmed == "1") // true
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.