-1

Hey guys so I create a NSMutableArray send it to my mysql server convert it to base64, then when the app reads the data, it decodes the base64 code into a string format. Now im trying to convert the string back into an NSMutableArray. I cant seem to get it work heres the code that converts it to a string.

let string = String(data: (Data(base64Encoded:((data?.value(forKey: "14112017") as! NSArray)[0] as! NSDictionary)["data"] as! String)!), encoding: .utf8)!
rmaddy
  • 314,917
  • 42
  • 532
  • 579
Daniel Cisneros
  • 53
  • 1
  • 10
  • 1
    Why are you using NSArray and NSDictionary in Swift? – rmaddy Nov 17 '17 at 01:07
  • the server returns the information in the format like this: { "11152017": { "day": "1", "month": "2", "year": "3", "data": "MzEzNDEzNDE0NDE0" } },{ "11152017": { "day": "1", "month": "2", "year": "3", "data": "MzEzNDEzNDE0NDE0" } } – Daniel Cisneros Nov 17 '17 at 01:10
  • 1
    You misunderstand my comment. Don't use `NSArray` in Swift. Use a Swift array. Don't use NSDictionary in Swift. Use a Swift dictionary. – rmaddy Nov 17 '17 at 01:12
  • 1
    So the server response looks like a JSON dictionary – Leo Dabus Nov 17 '17 at 01:13
  • so what im doing is calling for the key (11152017) for example, then getting [0] from it which is wierd but returns (day, month,year,data) but all im interested in is the data which is base64 code, so I call ["data"] to retrieve it, converting it to a string, then converting that data into a string which is my mutablearray but in string format – Daniel Cisneros Nov 17 '17 at 01:13
  • yeah the server response is a json which I have no problem reading, its just the part where my NSMutableArray is stored on the server in base64 which when read I have trouble converting it back to a NSMutableArray – Daniel Cisneros Nov 17 '17 at 01:14
  • and @rmaddy I dont quite understand, ive been using NSArray, but thats not where the problem is at. Thanks ill look into using swift array – Daniel Cisneros Nov 17 '17 at 01:15
  • 1
    Besides using Swift types, do yourself a huge favor and split the line of code posted in your question into about five lines of code. Break out each little intermediate result into its own line. Then you can actually debug the code and find exactly where your issue is. – rmaddy Nov 17 '17 at 01:17
  • @rmaddy well I had already done that, I actually put it all in one line on purpose after making sure it worked. Theres no problem in the code I posted, Im just trying to figure out if theres a way to convert the String back into an Array, I converted the Array into a string and sent it to the server, Now im trying to convert that string back into an array. My code I posted just gets the information from the server (theres a lot of other stuff sent) but I got it all parsed out down to this, just cant convert the string back Into an Array – Daniel Cisneros Nov 17 '17 at 02:46
  • string = "(((0,0,0,0,0,0,0),(0,0,0,0,0,0,0)),{steps = 0; weight = 0;})" but its in string format need as NSMutableArray Type – Daniel Cisneros Nov 17 '17 at 02:50
  • 1
    @DanielCisneros don't use `String(describing:)` to send your data to your server. You should convert your array to JSON to send it to your sql server – Leo Dabus Nov 17 '17 at 03:22
  • `let array = [[0,0,0,0,0,0,0],[0,0,0,0,0,0,0]]` `let jsonData = try! JSONSerialization.data(withJSONObject: array)` // 33 bytes `print(String(data: jsonData, encoding: .utf8)!)` `let loadedArray = (try? JSONSerialization.jsonObject(with: jsonData)) as? [[Int]] ?? []` `print(loadedArray)` – Leo Dabus Nov 17 '17 at 03:32
  • You are a life saver Leo! So I did convert it to jsonstring sent it to my server, and read it, then just JSONSerialization to convert it back to a swift array, after hours! – Daniel Cisneros Nov 17 '17 at 09:14

2 Answers2

0

So the answer to my question ended up converting my array to a JSONstring first which I did by using:

let jsonData: Data? = try? JSONSerialization.data(withJSONObject: UniversalArray)
let jsonString = String(data: jsonData!, encoding: .utf8)

then when retreiving it I get back my jsonString, however it has "\" which I replace using:

let cleanJsonString = myData.replacingOccurrences(of: "\\", with: "")

then to finally finish it off I just send this cleanJsonString to this function:

func convertToDictionary(text: String) -> Any? {

    if let data = text.data(using: .utf8) {
        do {
            return try JSONSerialization.jsonObject(with: data, options: [])
        } catch {
            print(error.localizedDescription)
        }
    }

    return nil

}

when calling this function I used this:

 let array = convertToDictionary(text: myMutableArray) as? [AnyObject]

Thanks everyone for the amazing support, heres the answer to this bogus mess I created.

Daniel Cisneros
  • 53
  • 1
  • 10
  • You took the method from https://stackoverflow.com/a/30480777/2227743 which is ok but you forgot to change its name (you are not converting to a dictionary but to Any). A detail, but correct naming is important. ;) – Eric Aya Nov 19 '17 at 17:39
0

There are 2 parts to this: Serializing your array for transmission to your server, and then deserializing it from the server.

We need to see the code you use to serialize your array first. (The code that converts it to a string.) Based on what you post, it appears to be in JSON format. If so you may be able to skip the base64 encoding step, depending on what you're doing with the data. JSON is a good format for transmission to remote servers. You don't typically base64 encode JSON before transmission.

If you are receiving JSON that was then base64 encoded, you'll need to un-encode it back to data, and then use the JSONSerializer class to convert the JSON back to objects like arrays or dictionaries:

let data = Data(base64Encoded: base64DataFromServer)
guard let object = try? JSONSerialization.jsonObject(with: data) else {
  return nil
}

(Note that the JSON you posted contains a dictionary as the top-level object, not an array.)

Duncan C
  • 128,072
  • 22
  • 173
  • 272