Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem with NSData as Value when Decoding #4

Open
mbalex99 opened this issue Apr 1, 2019 · 1 comment
Open

Problem with NSData as Value when Decoding #4

mbalex99 opened this issue Apr 1, 2019 · 1 comment

Comments

@mbalex99
Copy link

mbalex99 commented Apr 1, 2019

I have this sample use case where I have a JSON-like NSDictionary but I want to put some random NSData to prove that I can add something like a small UIImage/Audio file into CBOR.

func test_2_encodeMap_2() {

        // begin generating some random NSData
        let length = 2048
        let bytes = [UInt32](repeating: 0, count: length).map { _ in arc4random() }
        let data = Data(bytes: bytes, count: length)
        // end generating some random NSData
        
        let json: NSDictionary = [
            "_id": "123abc",
            "color": "Red",
            "age": 56,
            "mileage": 56.444,
            "tags": [
                "a", "b", "d", 4
            ],
            "data": data
        ]
        
        
        var encoded = CBOR.encode(json as NSDictionary)
        var decoded = CBOR.decode(encoded!) as! NSDictionary
}

It encodes fine but crashes saying "Thread 1: Fatal error: Array index is out of range" at Decoder.swift on Line 77

What am I doing wrong?

@mbalex99 mbalex99 changed the title Problem with NSData Problem with NSData as Value when Decoding Apr 1, 2019
@phisakel
Copy link

You can wrap data in an NSByteString

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants