ios - 快速 if 语句检查字符串不正确

标签 ios swift

在我的 iOS 应用程序中,我有一个像这样的简单 if 语句:

if (responseString! != "No Data")  {
  print("Data available")
}

如果我打印responseString,这就是结果:

No Data

但也会显示打印“数据可用”。 为什么?

最佳答案

您的 responseString 中可能存在空格或换行符,请尝试以下操作:

let responseString = NSString(data: data!, encoding: NSUTF8StringEncoding)?.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet())

关于ios - 快速 if 语句检查字符串不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34692517/

相关文章:

ios - swift:如何截取 AVPlayerLayer() 的屏幕截图

ios - 如何在 swift 中获取 mp3 文件的艺术作品(很重要,因为对象方法不起作用)

ios - 尝试从 URL 获取数据并获取控制台警告

ios - Paypal 移动支付 - 来自 Phonegap HTML5 IOS 应用程序

ios - 如何在 MacOS Catalyst 应用程序中获取触控板/鼠标滚轮滚动事件?

ios - 如何更改 Collection View 中单个单元格的大小?我想获取 slider 的值并使用该值来调整单元格的大小

ios - 模拟零重力风格的球员运动

iOS Line SDK登录失败: Error User Cancelled or Interrupted the login process

ios - 通过一个容器 View 传递多个 View Controller - Xcode 8,Swift 3

ios - 带有一个 Storyboard的 Xcode 通用应用程序