1

From a php i am requesting a responce. The response should be an array [String] however, swift is seeing this response as a String.

How can I convert a String to [String] ?

PHP:

print_r(json_encode($arrayUser));
// Print Out ["aaa","bbb","ccc","ddd"]

SWIFT 2.0:

    let infoArray = responseValue
        // Reads ["aaa","bbb","ccc","ddd"]

     print(infoArray[1])
     // error: Ambiguous use of 'subscript'

Why is Swift seeing the array as a string? And how can I convert it to an array [String]?

I have also tried:

let infoArray = responseValue as! [String]
//warning: Cast from 'String' to unrelated type '[String]' always fails
SNos
  • 3,430
  • 5
  • 42
  • 92
  • 1
    This return came from a PHP file, right? Swift will get this return as a string, you should use a JSON plugin for swift to manipulate this information. e.g. [SwiftyJSON](https://github.com/SwiftyJSON/SwiftyJSON) – Felipe Umpierre Nov 21 '15 at 20:10
  • I'm positive that your infoArray isn't actually array of strings, it's just the raw string value you receive from your php backend. you need to parse it. you can use SwiftyJSON as @Felipe Umpierre suggested. – Peyman Nov 22 '15 at 04:41
  • I was getting '.responseString' instead '.responceJSON' – SNos Nov 22 '15 at 11:00

1 Answers1

-1

well, given it's an array of strings and not primitives, perhaps this would work, as suggested by Nate Cook:

let infoArray = responseValue.map { $0.copy() }

UPDATED (following @Eric D. comments):

if responseValue is [String] then:

let infoArray:[String] = responseValue.map { $0.copy() as! String }

if responseValue is String then:

let infoArray = Array(responseValue.characters)

The other suggestion would be to iterate over responseValue and append each element into a new array of strings. But performance might be an issue.

Community
  • 1
  • 1
ppp
  • 713
  • 1
  • 8
  • 23
  • This is not what Nate is saying (his example with `copy` was for NSMutableArray). And this is not an answer to OP's question anyway. – Eric Aya Dec 02 '15 at 14:24
  • 1
    thx for the feedback @EricD. Maybe I misunderstood. Care to elaborate? – ppp Dec 02 '15 at 14:29
  • No problem. `.copy()` is for copying a reference array, like an NSArray. Here OP has a Swift array (value, not reference). So your answer would be wrong, but it's also not an answer to OP's question anyway, which was `Why is Swift seeing the array as a string? And how can I convert it to an array [String]?`. :) – Eric Aya Dec 02 '15 at 14:30
  • I hear you. Array is a struct in Swift. But wouldn't my updated code compile and produce a [String]? – ppp Dec 02 '15 at 14:45
  • Well... what's the point? If `responseValue` is already an array of Strings, then your code does nothing but add overhead without changing anything, and if it's not an array but a String, as in the question, then your code doesn't compile and wouldn't help anyway. So... your answer is still wrong, sorry. :/ – Eric Aya Dec 02 '15 at 14:48
  • I'm sorry but I will stop there. As I explained before, using `.copy()` here makes no sense. And `Array(responseValue.characters)` would *not* give the expected result *at all*. – Eric Aya Dec 02 '15 at 15:07