Giter Club home page Giter Club logo

Comments (4)

valpackett avatar valpackett commented on June 15, 2024 1

Hm, you actually can easily encode the map in-order manually.

Instead of building an outer CBOR enum with dictionaryLiteral, just directly return the bytes for the map.

Encoding a map is simple, you just encode the count, OR the first byte with 0b101_00000, and encode the key-value pairs.

Something like

    public func encode() -> [UInt8] {
        var res: [UInt8] = Int(3).encode() // well, your actual count
        res[0] = res[0] | 0b101_00000
        res.append(contentsOf: 0.encode())
        res.append(contentsOf: 1001.encode())
        res.append(contentsOf: 1.encode())
        res.append(contentsOf: self.some_int32.encode())
        res.append(contentsOf: 2.encode())
        res.append(contentsOf: self.hasSome_int8() ? CBOR(integerLiteral: self.some_int8!) : CBOR(nilLiteral: ())) // CBOR enum itself is encodable
        return res
    }

from swiftcbor.

valpackett avatar valpackett commented on June 15, 2024

would it be possible to get the keys (integerLiteral) ordered in ascending order?

Not currently, our map representation is a Swift map, which doesn't preserve order :(

case map([CBOR : CBOR])

It would make sense to use a list of key-value pairs instead, but that's a breaking change (not that there's been a 1.0 release haha) and I won't be the one making that change (I don't have a Swift dev environment currently), sorry.

How do I get an Int value from a CBOR.unsignedInt ?

You get a UInt64 (or just UInt before recent changes) by pattern matching.

if case let CBOR.unsignedInt(int_val) = cbor_val {
    do_things_with(int_val);
}

I guess I should put a link to a pattern matching tutorial in the readme, since this is a frequently asked question...

from swiftcbor.

keithle888 avatar keithle888 commented on June 15, 2024

Thanks alot for the guide on the ordering of the map! I've managed to implement it.

I'm still relatively new to Swift, so I'm reading up on Swift pattern matching to get the UInt64 value.

Again, thanks alot for the quick replies :)

from swiftcbor.

keithle888 avatar keithle888 commented on June 15, 2024

Thanks alot for your help! I managed to get pattern matching to work for extracting the values from a CBOR object.

from swiftcbor.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.