1

I have to serialize an array like PHP does. This is the PHP line of code I have to translate in Swift 4:

serialize(array('p'=>2,'m'=>10,'e'=>20))

I just converted the php array in a Swift struct:

struct codeStructure: Codable {
var p: Int
var m: Int
var e: Int

init() {
    p = 2
    m = 10
    e = 20
   }
}

but I need a serialize() function that returns a string like:

a:3:{s:1:"p";i:2;s:1:"m";i:10;s:1:"e";i:20;}

Any hints? Thank you in advance.

Michele

6
  • 1
    Can I ask why you have to do it the same as PHP? (out of curiosity) Commented Jun 28, 2018 at 13:02
  • Which is your goal? Maybe exist a better solution. Commented Jun 28, 2018 at 13:19
  • There is no such function in Swift, you'd have to implement it yourself. Consider to use a more common interchange format, such as JSON. Commented Jun 28, 2018 at 13:20
  • Your solution stackoverflow.com/a/44551842/2342915, you should use the codable protocol for that Commented Jun 28, 2018 at 13:22
  • I have to communicate with a server that accepts in the header a value "PHP serialized" and then base64encoded and I'm unable to modify the server side code... Commented Jun 28, 2018 at 13:51

1 Answer 1

0

You can serialize the object to json and then convert it. I guess it will be much easier than convert object to php serialize.

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

Comments

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.