Export class instances to a string (for sharing, backup) and importing them I have a class (Patient) with many instances.. I need a way to share the instances with other users, so I thought of converting the instance into a string and sharing it, then reconverting it into an instance

From instance to string

func ToString(pt: Patient) -&gt; String {
    let myarray: [String] = [
        pt.id,
        pt.name,
        String(pt.age),
        pt.address,
        pt.tele,
        pt.notes,
        dateToString(pt.date),
        pt.history,
        pt.diagnosis,
        (pt.investigations.map {$0.name + "{|}" + $0.value}).joinWithSeparator("{|X|}"),
        (pt.treatments.map {$0.name + "{|}" + $0.dose}).joinWithSeparator("{|X|}"),
        (pt.images.map {$0.imagePath + "{|}" + $0.caption}).joinWithSeparator("{|X|}")
    ]

    return myarray.joinWithSeparator("{|NEWLINE|}")
}


From string to instance

func FromString(mystring: String) {
    let pt = Patient()

    let ptarray = mystring.componentsSeparatedByString("{|NEWLINE|}")
    let ixarray = ptarray[9].componentsSeparatedByString("{|X|}")
    let rxarray = ptarray[10].componentsSeparatedByString("{|X|}")
    let photoarray = ptarray[11].componentsSeparatedByString("{|X|}")
    let ixcarray = ixarray.map { Investigation(name: $0.componentsSeparatedByString("{|}")[0], value: $0.componentsSeparatedByString("{|}")[1]) }
    let rxcarray = rxarray.map { Treatment(name: $0.componentsSeparatedByString("{|}")[0], dose: $0.componentsSeparatedByString("{|}")[1]) }
    let photocarray = photoarray.map { Pic(imagePath: $0.componentsSeparatedByString("{|}")[0], caption: $0.componentsSeparatedByString("{|}")[1]) }

    pt.id = ptarray[0]
    pt.name = ptarray[1]
    pt.age = Int(ptarray[2]) ?? -1
    pt.address = ptarray[3]
    pt.tele = ptarray[4]
    pt.notes = ptarray[5]
    pt.date = stringToDate(ptarray[6])
    pt.history = ptarray[7]
    pt.diagnosis = ptarray[8]

    pt.savePatient()

    _ = ixcarray.map { x in pt.addInvestigation(x) }
    _ = rxcarray.map { x in pt.addTreatment(x) }
    _ = photocarray.map { x in pt.addImage(x) }
}


Notes:


investigation, treatment, and images are array of classes with two properties
I used "|" as a separator because I disabled it in my app so the user can't interfere with my functions
I use Realm in my app for data persistance
My specific concern is performance issues because thousands of instances may be shared at once
I don't see a question here, but I think I understand your concerns...

Regarding performance - I don't think you need to worry. Note that JSON and XML exist, in part, as formats for transmitting objects between apps in a platform independent way and there isn't a whole lot of worries about performance with either of them.

That said, I suggest you use one of these standard data interchange formats rather than trying to invent your own. Both JSON and XML already have very efficient and OS provided functions for doing the conversion...

extension Patient {
    init(dictionary dict: [String: AnyObject]) {
        id = dict["id"] as! String
        name = dict["name"] as! String
        age = dict["age"] as! Int
        // etc...
    }

    var dictionary: [String: AnyObject] {
        var dict: [String: AnyObject] = [:]
        dict["id"] = id
        dict["name"] = name
        dict["age"] = age
        // etc...
        return dict
    }
}


Once you have the above, you can easily convert a Patient to and from a Dictionary object and once you have that, you can use Apple's methods to convert the dictionary to/from either JSON or XML depending on what you like best.

You might also want to consider using SwiftyJSON instead of [String: AnyObject] dictionaries.