Extended Codable Protocol
First, make the structure compliant with the protocol Serializable
.
struct Article: Serializable {
let id: String
let title: String
let tag: [String]
let content: String
}
Serialize (object to JSON string):
let article = Article(id: "001", title: "Hello!", tag: ["Introduction"], content: "# Hi there! I'm Enchan.")
let serialized = article.serialize()!
print(serialized)
Deserialize (JSON string to object):
let deserialized = "{"id":"001","title":"Hello!","content":"# Hi there! I'm Enchan.","tag":["Introduction"]}"
let article = Article.deserialize(object: deserialized)!
print(article)
This repository is published under MIT License.
In details, see LICENSE.
link |
Stars: 0 |
Last commit: 2 years ago |
Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics