0

So i'm going to send data back and forth between 2 applications and i thought since it's A LOT of data in different models i can use a UIPasteboard. However i can't get it to work. Here's the code and issue.

struct TestModel {
    var value1: Int = 0
    var value2: String = "hi"
}

class ViewController: UIViewController {

    @IBOutlet weak var redView: UIView!

    override func viewDidLoad() {
        super.viewDidLoad()
        let model = TestModel(value1: 1, value2: "hello")
        let testModel = Mirror.init(reflecting: model)

        guard let paste = UIPasteboard.init(name: UIPasteboard.Name(rawValue: "test1"), create: true) else {return}

        let items = testModel.children.map({
            return [$0.label ?? "noLabel": $0.value]
        })

        print(items)
        paste.addItems(items)
        print(paste.items[0]["value1"])
    }
}

print(items) gives me the following: [["value1": 1], ["value2": "hello"]] So everything is working so far.

print(paste.items[0]["value1"]) however gives me: <OS_dispatch_data: data[0x2835d3cc0] = { leaf, size = 43, buf = 0x102f18000 }> And i have no clue how to unwrap this, i've tried unwrapping it to Int, String, Data, NSData and everything, but nothing seem to work.. so what is going on?

Vollan
  • 1,887
  • 11
  • 26
  • I think a better way to do this is to serialise your `TestModel` to JSON `Data` using `Codable`, then call `setData` on the paste board. – Sweeper Jun 07 '19 at 07:00
  • @Sweeper I agree, however this code is pretty outdated and dont use Codable yet and it would be annoying to do manually – Vollan Jun 07 '19 at 07:03
  • Why would be annoying? You can literally just write `extension TestModel : Codable {}` and be done with it. – Sweeper Jun 07 '19 at 07:05
  • Because it's obj-c, but i guess i could make something out of it. But still, why can't i just wrap items in to the dictionary – Vollan Jun 07 '19 at 07:10

0 Answers0