1

Can someone please help me with this. I saved my data into Parse.com into column with type array (example: ["11:30","12:45,"13:02"], just some list of some times as string). I have tried to get this data with swift:

var take: NSMutableArray!

var query = PFQuery(className: "test")
query.getObjectInBackgroundWithId("QZ6Y8Oljc5"){
   (testData: PFObject!, error: NSError!) -> Void in
      if (error == nil){
         take = testData["workday"]
         println(take)    
       }
       else{
         println(error)
        }
}

the problem is that i get only json array type:

(
   "11:30",
   "12:45,
   "13:02"
)

How can I convert it into NSArray so it could be like:

var myArray =  ["11:30","12:45,"13:02"]

Thank you for any suggestions because I tried every method I found here, but without any results.

1 Answer 1

1

The problem with JSON data is that it is it's own array that has to be sifted and groomed. normally people would end up using huge nested IF statements which ends up looking messy. Luckily, someone created a code that sifts through JSON data and gives you back usable types (Int, Arrays, Strings) by use of a massive switch table.

https://github.com/SwiftyJSON/SwiftyJSON

Look it up, it should help. Once you have it implemented you can call it by typing..

let json = JSON(Data : JSONData)

then to sift through, you use substrings.. (depending on the data, you match it with a string or int)

let firstIndex = json["workday"]

//Int
let firstIndexOfWorkDay = json["workday"][0]

//String
let firstIndexOfWorkDay = json["workday"]["time"]

and so on... however, you will need to cast it once you singled out the data

let firstIndexOfWorkDay = json["workday"][0].valueOfFloat

//printing it would give 11:30

although personally I use ".description" .. since sometimes when I sift through all the array, its a mix of types.

let firstIndexOfWorkDay = json["workday"][0].description

println(firstIndexOfWorkDay)

//would literally give "11:30" including the quotation marks

then I use string methods to trim the quotations then cast it to whatever type I need. But it's up to your creativity once you figure out how it works

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you! That helped me a lot! I'm now getting an array of integers like [1,2,3], so I need to figure how can I convert it into array of string like ["1","2","3"] and this is it. If you have any sugestion I will be greatful.
no problem! just use the ".description" and it will print out literal values as strings. If it contains a bool it will print out "True", if it contains an integer, it will print out "1" and so on. But of course you can always sift through the integer array and convert each one to string.
It looks like this is not NSArray, it is still JSON. I have tried this ` let json = JSON(take)` and then var myArray = json.array! and with println(myArray) it prints [1,2,3] but in JSON format
I fixed it with var myArray = json.arrayObject as Array<String>!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.