swiftfoundationnsvalueobjective-c-swift-bridgeunsafemutablepointer

How to Represent Dictionary as NSValue in Swift 3?


I have the following Dictionary:

let example: [String: (Int, Int)] = ["test": (0, 1)]

I need to store this as an NSData variable, and to do that it must first be converted to an NSValue, which I try to do as follows:

let nsval = NSValue().getValue(example as! UnsafeMutableRawPointer)

Only to be met with the error:

Cannot convert value of type 'Void' (aka '()') to specified type 'NSValue'

I've seen SO answers that suggest using:

let test = UnsafeMutablePointer.load(example)

But that also yields the error:

Type 'UnsafeMutablePointer<_>' has no member 'load'

So then, how is one to convert a dictionary to an NSValue in Swift 3?


Solution

  • In theory, if you really want to turn a swift Tuple into a data object, you could do this.

    var x:(Int, Int) = (4, 2)
    
    var bytes:Data = withUnsafeBytes(of: &x){ g in
        var d = Data()
        for x in g {
            d.append(x)
        }
    
        return d
    }
    

    heres the reverse:

    var y:(Int, Int) = (0,0)
    bytes.withUnsafeBytes { (x:UnsafePointer<(Int, Int)>) in
        y = x.pointee
    }
    
    print(y) // prints (4, 2)
    

    Beware: this is the path to the dark side.