Welcome to the documentation for Jayme, a powerful library for handling JSON APIs in Swift.
Installation
To install Jayme, simply add the following line to your Podfile:
pod 'Jayme'
Then run pod install
to fetch the latest version of the library.
Getting Started
Jayme provides an easy-to-use interface for interacting with JSON APIs. To get started, follow these steps:
- Create an instance of
JaymeClient
by supplying the base URL of your API. - Define your data model by conforming to the
Decodable
protocol and implementing the necessary properties and coding keys. - Create a subclass of
JaymeResource
for each endpoint in your API, providing the necessary URL path components and data model type. - Start making requests using
get()
,post()
,put()
,patch()
, ordelete()
methods of yourJaymeResource
subclasses.
Examples
Example 1: Fetching Data
The following code demonstrates how to fetch data using Jayme:
let client = JaymeClient(baseURL: "https://api.example.com")
struct User: Decodable {
let id: Int
let name: String
}
class UsersResource: JaymeResource
init() {
super.init(pathComponents: ["users"])
}
}
let users = UsersResource()
users.get().then { fetchedUsers in
print(fetchedUsers)
}.catch { error in
print("Error: \(error)")
}
Example 2: Creating Data
The following code demonstrates how to create data using Jayme:
let client = JaymeClient(baseURL: "https://api.example.com")
struct User: Codable {
let name: String
let email: String
}
class UsersResource: JaymeResource
init() {
super.init(pathComponents: ["users"])
}
}
let users = UsersResource()
let newUser = User(name: "John Doe", email: "johndoe@example.com")
users.post(newUser).then { createdUser in
print("New user created: \(createdUser)")
}.catch { error in
print("Error: \(error)")
}
Conclusion
Congratulations! You now have a basic understanding of how to use Jayme to interact with JSON APIs in Swift. For more detailed information and advanced features, please refer to the Jayme documentation.
Have fun exploring the power and simplicity of Jayme!