Upgrade to Pro — share decks privately, control downloads, hide ads and more …

ca.swift.pdf

Kyohei Ito
December 01, 2016
2k

 ca.swift.pdf

Kyohei Ito

December 01, 2016
Tweet

Transcript

  1. 2. .protoϑΝΠϧΛ࡞੒ .protoϑΝΠϧྫ syntax = "proto3"; message BookInfo { int64

    id = 1; string title = 2; string author = 3; } message MyLibrary { int64 id = 1; string name = 2; repeated BookInfo books = 3; map<string,string> keys = 4; }
  2. ͦͷ··ΦϒδΣΫτͱͯ͠ར༻Մೳ • protobuf message MyLibrary { int64 id = 1;

    string name = 2; repeated BookInfo books = 3; map<string,string> keys = 4; } • Swift public struct MyLibrary { public var id: Int64 = 0 public var name: String = "" public var books: [BookInfo] = [] public var keys: Dictionary<String,String> = [:] }
  3. ϦΫΤετͷAcceptϔομͰ൑அ let library = getLibrary() let accept = request.headers["Accept"] if

    accept == "application/protobuf" { response.headers["Content-Type"] = "application/protobuf" response.send(data: try library.serializeProtobuf()) } else { response.headers["Content-Type"] = "application/json; charset=UTF-8" response.send(try library.serializeJSON()) }
  4. ϨεϙϯεϔομΛݟͯjsonͱprotobufͷ੾Γସ͑ let contentType = response.allHeaderFields["Content-Type"] as? String if let type

    = contentType, type == "application/protobuf" { return try MyLibrary(protobuf: data) } else { let json = String(bytes: data, encoding: .utf8) return try MyLibrary(json: json!) }
  5. protobufΛฦ͢ϞοΫ let json = String(bytes: data, encoding: .utf8) let library

    = try MyLibrary(json: json!) return try library.serializeProtobuf()
  6. .protoͷυΩϡϝϯτԽ https://github.com/estan/protoc-gen-doc $ brew install qt5 $ brew link --force

    qt5 $ git clone https://github.com/estan/protoc-gen-doc.git $ cd protoc-gen-doc $ PROTOBUF_PREFIX=/usr/local/Cellar/protobuf/3.1.0 qmake $ make && make install ※PROTOBUF_PREFIX͸ɺ࢖༻͢Δprotobufʹ߹Θͤͯͩ͘͞ ͍ɻ
  7. .swiftΛframeworkԽ͢Δ Pod::Spec.new do |s| s.name = "Protobuf" s.summary = "Protobuf"

    s.homepage = "https://github.com/KyoheiG3/ProtobufExample" s.version = "0.0.1" s.author = { "Kyohei Ito" => "[email protected]" } s.ios.deployment_target = '8.0' s.source = { :path => '.' } s.source_files = "**/*.swift" s.requires_arc = true s.dependency 'SwiftProtobuf' end