3 Replies
      Latest reply on Feb 14, 2017 4:03 AM by eskimo
      Akdov Level 1 Level 1 (0 points)

        Hello everyone!


        I have a problem or I dont understand how i can send my "Section" to an other device.

        Here is my "Section" witch I would like to send.

        import UIKit
        class Section: NSObject, NSCoding {
            var heading : String
            var items : [String]
            var foto :  [UII
            var beschreibung: [String]
            var genre: [String]
            var listeZutat: [[String]]
            var listeEinheit: [[String]]
            var listeMenge: [[Int]]
            static let DocumentsDirectory = FileManager().urls(for: .documentDirectory, in: .userDomainMask).first!
            static let ArchiveURL = DocumentsDirectory.appendingPathComponent("sections")
            struct PropertyKey {
                static let headingKey = "heading"
                static let itemsKey = "items"
                static let fotoKey = "foto"
                static let beschreibungKey = "beschreibung"
                static let genreKey = "genre"
                static let listeZutatKey = "listeZutat"
                static let listeEinheitKey = "listeEinheit"
                static let listeMengeKey = "listeMenge"
                init?(heading: String, items : [String], foto : [UIImage], beschreibung: [String], genre: [String], listeZutat: [[String]], listeEinheit: [[String]], listeMenge: [[Int]]) {
                    self.heading = heading
                    self.items = items
                    self.foto = foto
                    self.beschreibung = beschreibung
                    self.genre = genre
                    self.listeMenge = listeMenge
                    self.listeZutat = listeZutat
                    self.listeEinheit = listeEinheit
        func encode(with aCoder: NSCoder) {
            aCoder.encode(heading, forKey: PropertyKey.headingKey)
            aCoder.encode(items, forKey: PropertyKey.itemsKey)
            aCoder.encode(genre, forKey: PropertyKey.genreKey)
            aCoder.encode(listeZutat, forKey: PropertyKey.listeZutatKey)
            aCoder.encode(beschreibung, forKey: PropertyKey.beschreibungKey)
            aCoder.encode(listeMenge, forKey: PropertyKey.listeMengeKey)
            aCoder.encode(listeEinheit, forKey: PropertyKey.listeEinheitKey)
            aCoder.encode(foto, forKey: PropertyKey.fotoKey)
            required convenience init?(coder aDecoder: NSCoder) {
                let heading = aDecoder.decodeObject(forKey: PropertyKey.headingKey) as! String
                let items = aDecoder.decodeObject(forKey: PropertyKey.itemsKey) as! [String]
                let foto = aDecoder.decodeObject(forKey: PropertyKey.fotoKey) as? [UIImage]
                let beschreibung = aDecoder.decodeObject(forKey: PropertyKey.beschreibungKey) as! [String]
                let genre = aDecoder.decodeObject(forKey: PropertyKey.genreKey) as! [String]
                let listeZutat = aDecoder.decodeObject(forKey: PropertyKey.listeZutatKey) as! [[String]]
                let listeMenge = aDecoder.decodeObject(forKey: PropertyKey.listeMengeKey) as! [[Int]]
                let listeEinheit = aDecoder.decodeObject(forKey: PropertyKey.listeEinheitKey) as! [[String]]
                self.init(heading: heading, items: items, foto: foto!, beschreibung: beschreibung, genre: genre, listeZutat: listeZutat, listeEinheit: listeEinheit, listeMenge: listeMenge)


        I can save and load this "Section" on my iPhone but when I use the "multipeer connectivity" its crash because my Section is not a type data. I dont know how i can convert this...



             - Mabye it is better to use an other way to send my "Section2ß

             - Is it possible that I can send this with an email??


        Thanks you for your help and sorry about my English!

        • Re: Send data to other devices or cloud
          eskimo Apple Staff Apple Staff (13,395 points)

          There two parts to this:

          • How to serialise and deserialise your objects so that they can be transported over the network

          • How to transport that data over the network

          It seems like you’re looking for help with the first part.  If you have questions about the second, please feel free to ask them over in CoreO S > Networking.

          In terms of serialising and deserialising, there’s no fully automatic way to do this.  You do, however, have a bunch of options.  It looks like you’re trying to do this using NSCoding, which is a reasonable approach.  Was that NSCoding code written for serialising to disk?  Or is it part of a network solution that you’re trying to complete?

          Share and Enjoy

          Quinn “The Eskimo!”
          Apple Developer Relations, Developer Technical Support, Core OS/Hardware
          let myEmail = "eskimo" + "1" + "@apple.com"

            • Re: Send data to other devices or cloud
              Akdov Level 1 Level 1 (0 points)

              Yes, I am looking at first  for the first part. I know that there isn´t an automatic way to do that. But I can only send one String or one Image.

              But how I send an Array or send more Strings?

                • Re: Send data to other devices or cloud
                  eskimo Apple Staff Apple Staff (13,395 points)

                  Yes, I am looking at first for the first part.

                  OK, on the serialisation and deserialisation front…

                  You can use NSCoding from Swift but I personally wouldn’t use it in this case.  This isn’t a Swift thing, but an NSCoding thing.  Actually, there are two issues:

                  • If you use NSCoding then you’re tying your on-the-wire format to a mechanism that’s only available on Apple platforms.  If you ever end up needing to support some other platform, you’ll need to change your on-the-wire protocol, which is generally not fun.

                  • NSCoding is not appropriate for data that crosses security boundaries, and that includes networking.  At a minimum you’d need to use NSSecureCoding.

                  In situations like this I typically reach for JSON.  I write a small amount of code that converts my model objects into JSON-compatible objects, and then serialise that using JSONSerialization.  And I do the opposite on the reverse side.

                  You can do this in an ad hoc fashion, or implement some sort of systematic approach based on a Swift protocol (similar to how NSCoding works).  Some quick ’net searches will turn up various existing infrastructure for this.

                  JSON isn’t the only option here; if you look around you’ll find folks have used all sorts of different approaches, including:

                  • Property lists (XML and binary)

                  • XML

                  • ASN.1

                  • Protocol Buffers (protobufs)

                  Again, as each of these is quite popular, you’re likely to find existing infrastructure to help you out.

                  Share and Enjoy

                  Quinn “The Eskimo!”
                  Apple Developer Relations, Developer Technical Support, Core OS/Hardware
                  let myEmail = "eskimo" + "1" + "@apple.com"