可以在 Swift 应用程序和后端之间交换对象吗?
Possible to exchange objects between Swift app and backend?
据我目前所知,一种方法是使用 JSON。但是,将 swift 对象发送到服务器以确保服务器具有相同的 class 可用似乎更好更容易。这样我就可以继续使用 swift 每一步。
这可能吗?我该怎么做?
当前设置:
- Swift游乐场发送数据。
- Kitura 服务器接收数据。
游乐场代码:
import UIKit
import PlaygroundSupport
PlaygroundPage.current.needsIndefiniteExecution = true
struct TestObject {
let foo = "just a string"
let number = 125
let array = ["bar", "foo"]
func printSomeInfo() {
print(foo + "\(number+25)")
}
}
func send() {
let request = NSMutableURLRequest(url: URL(string: "http://192.168.178.80:8090/test")!)
request.httpMethod = "POST"
let testObject = TestObject()
let bodyData = "\(testObject)"
request.httpBody = bodyData.data(using: String.Encoding.utf8)
let task = URLSession.shared.dataTask(with: request as URLRequest,
completionHandler: {
(data, response, error) -> Void in
})
task.resume()
}
send()
Kitura main.swift 代码:
import Kitura
import Foundation
let router = Router()
struct TestObject {
let foo = "just a string"
let number = 125
let array = ["bar", "foo"]
func printSomeInfo() {
print(foo + "\(number+25)")
}
}
router.post("/test") {request, response, next in
response.headers["Content-Type"] = "text/plain; charset=utf-8"
if let post = try request.readString() {
// would like to cast to TestObject but that doesn't work
// let postObject = post as TestObject
print(post)
}
}
Kitura.addHTTPServer(onPort: 8090, with: router)
Kitura.run()
对于您的情况,JSON 对象映射有一个很好的库。
https://github.com/Hearst-DD/ObjectMapper
但是,您需要在 类 和结构中添加一些额外的代码来实现它们被映射的能力
您需要通过网络以某种方式序列化数据。最常见的方法之一是使用 JSON。 This recent swift blog 解释了如何做到这一点。如果您需要为很多不同的对象执行此操作,您可以将 JSON serialization/deserialization 抽象为一个公共基础。
据我目前所知,一种方法是使用 JSON。但是,将 swift 对象发送到服务器以确保服务器具有相同的 class 可用似乎更好更容易。这样我就可以继续使用 swift 每一步。
这可能吗?我该怎么做?
当前设置:
- Swift游乐场发送数据。
- Kitura 服务器接收数据。
游乐场代码:
import UIKit
import PlaygroundSupport
PlaygroundPage.current.needsIndefiniteExecution = true
struct TestObject {
let foo = "just a string"
let number = 125
let array = ["bar", "foo"]
func printSomeInfo() {
print(foo + "\(number+25)")
}
}
func send() {
let request = NSMutableURLRequest(url: URL(string: "http://192.168.178.80:8090/test")!)
request.httpMethod = "POST"
let testObject = TestObject()
let bodyData = "\(testObject)"
request.httpBody = bodyData.data(using: String.Encoding.utf8)
let task = URLSession.shared.dataTask(with: request as URLRequest,
completionHandler: {
(data, response, error) -> Void in
})
task.resume()
}
send()
Kitura main.swift 代码:
import Kitura
import Foundation
let router = Router()
struct TestObject {
let foo = "just a string"
let number = 125
let array = ["bar", "foo"]
func printSomeInfo() {
print(foo + "\(number+25)")
}
}
router.post("/test") {request, response, next in
response.headers["Content-Type"] = "text/plain; charset=utf-8"
if let post = try request.readString() {
// would like to cast to TestObject but that doesn't work
// let postObject = post as TestObject
print(post)
}
}
Kitura.addHTTPServer(onPort: 8090, with: router)
Kitura.run()
对于您的情况,JSON 对象映射有一个很好的库。 https://github.com/Hearst-DD/ObjectMapper 但是,您需要在 类 和结构中添加一些额外的代码来实现它们被映射的能力
您需要通过网络以某种方式序列化数据。最常见的方法之一是使用 JSON。 This recent swift blog 解释了如何做到这一点。如果您需要为很多不同的对象执行此操作,您可以将 JSON serialization/deserialization 抽象为一个公共基础。