By Fab


2018-06-13 14:18:10 8 Comments

I'm trying to return a value (string) from php to IOS application:

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). But the check fails.

1 comments

@Martin R 2018-06-13 14:40:28

String(...) gives me Optional("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.

The solution is (compare Does swift have a trim method on String?):

let trimmed = string.trimmingCharacters(in: .whitespacesAndNewlines)
print(trimmed == "1") // true

Related Questions

Sponsored Content

18 Answered Questions

[SOLVED] Reference — What does this symbol mean in PHP?

8 Answered Questions

[SOLVED] Swift performance: sorting arrays

7 Answered Questions

[SOLVED] How does PHP 'foreach' actually work?

36 Answered Questions

[SOLVED] How do I check if a string contains a specific word?

30 Answered Questions

[SOLVED] Reference - What does this error mean in PHP?

29 Answered Questions

[SOLVED] Finding index of character in Swift String

  • 2014-06-04 04:42:23
  • Matt Wilding
  • 186655 View
  • 175 Score
  • 29 Answer
  • Tags:   string swift

28 Answered Questions

[SOLVED] How to get useful error messages in PHP?

7 Answered Questions

[SOLVED] MySQL query String contains

9 Answered Questions

[SOLVED] Can I try/catch a warning?

  • 2009-08-06 21:51:30
  • user121196
  • 157568 View
  • 279 Score
  • 9 Answer
  • Tags:   php error-handling

1 Answered Questions

[SOLVED] swift http request crash nil value

  • 2016-10-13 12:37:32
  • MattBlack
  • 58 View
  • -2 Score
  • 1 Answer
  • Tags:   swift http

Sponsored Content