Skip to content

CoderHsiao/SwiftPBDemo

Repository files navigation

Swift项目使用protobuf

官方文档 : https://github.com/apple/swift-protobuf

1、配置编译器

  • 新建一个文件夹用于存放swift-protobuf
  • 下载官方的swift-protobuf : git clone https://github.com/apple/swift-protobuf.git
  • 进入下载后的swift-protobuf文件夹 : cd swift-protobuf
  • 查看版本:git tag -l 最新版1.8.0
  • 选择最新版本执行 : git checkout tags/1.8.0
  • 创建protoc-gen-swift文件 : swift build -c release
  • 路径/Users/imac/Desktop/ED/iOS/ProtocolBuffer/swift-protobuf/.build/x86_64-apple-macosx/release/protoc-gen-swift
  • 配置protoc-gen-swift
    • 把 protoc-gen-swift 文件直接拷贝到/usr/local/bin目录下

2、编译proto文件

  • 在项目中创建Person.proto文件

    syntax = "proto3";
    
    message PersonInfo {
       int64 id = 1;
       string name = 2;
       string address = 3;
    }
  • 使用protoc编译Person.proto文件

    • 指令:protoc --swift_out=. Person.proto
    • 生成 :Person.pb.swift文件
    • Person.protoPerson.proto添加到项目中

3、Podfile

  • Podfile中需要使用Apple官方库pod 'SwiftProtobuf', '~> 1.8.0'

    # Uncomment this line to define a global platform for your project
    platform :ios, '9.0'
    # Comment this line if you're not using Swift and don't want to use dynamic frameworks
    use_frameworks!
    
    target 'SwiftPBDemo' do
    
    pod 'SwiftProtobuf', '~> 1.8.0'
    
    
    end

4、使用

	func testPB()  {
        var person = PersonInfo()
        person.id = 001
        person.name = "艾德一站通"
        person.address = "彩讯科创中心16层"
        print("person = \(person)")
        /**
         person = SwiftPBDemo.PersonInfo:
         id: 1
         name: "艾德一站通"
         address: "彩讯科创中心16层"
         */
        
        //1、转data
        guard let person2Data :Data = try? person.serializedData() else {
            print("error");
            return
        }
        print("person2Data : \(person2Data)")
        /**
         person2Data : 44 bytes
         */
        
        
        //2、data回转pb
        guard let data2Person = try? PersonInfo(serializedData: person2Data) else {
             print("error")
             return
        }
        print("personFromData : \(data2Person)")
        /**
        personFromData : SwiftPBDemo.PersonInfo:
        id: 1
        name: "艾德一站通"
        address: "彩讯科创中心16层"
        */
        
        
        
        //3、jsonString初始化PB
        let jsonString = "{\"id\":888,\"name\":\"jsonString\",\"address\":\"json address\"}"
        guard let jsonString2Person = try? PersonInfo(jsonString: jsonString) else {
             print("error")
            return
        }
        print("jsonString2Person : \(jsonString2Person)")
        /**
         jsonString2Person : SwiftPBDemo.PersonInfo:
         id: 888
         name: "jsonString"
         address: "json address"
         */
        
        //4、pb转jsonString
        guard let person2JsonString = try? jsonString2Person.jsonString() else{
            print("error")
            return
        }
        print("person2JsonString = \(person2JsonString)")
        /**
         person2JsonString = {"id":"888","name":"jsonPerson","address":"json address"}
         */
        
        
        //5、pb转jsonUTF8
        guard let person2jsonUTF8 = try? person.jsonUTF8Data() else {
            print("Error")
            return
        }
        print("person2jsonUTF8 = \(person2jsonUTF8)")
        /**
         person2jsonUTF8 = 71 bytes
         */
        
        //6、jsonUTF8Data转pb
        guard let jsonUTF8ToPerson = try? PersonInfo(jsonUTF8Data: person2jsonUTF8) else{
            print("error")
            return
        }
        print("jsonUTF8ToPerson = \(jsonUTF8ToPerson)")
        /**
         jsonUTF8ToPerson = SwiftPBDemo.PersonInfo:
         id: 1
         name: "艾德一站通"
         address: "彩讯科创中心16层"
         */
    }

About

A simple demo of protocol buffer use in Swift

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published