Quick Start

For normal RESTful APIs with JSON data, first you need to create a model structure, which represents the expected result:

struct Landmark: Hashable, Codable {
var id: Int
var name: String
var park: String
var state: String
var description: String
}
💡

If you want to process the data manually or have a different type of API, you can create your own fetcher function. Check here for more examples.

Then you can import Gravity and start using it inside any view components:

import SwiftUI
import Gravity
struct LandmarkList: View {
@SWR<URL, Landmark>(url: "https://example.org/api/endpoint") var api
var body: some View {
if let landmarks = api.data {
List(landmarks, id: \.id) { landmark in
LandmarkRow(landmark: landmark)
}
}
}
}

Normally, there're 3 possible states of a request: "loading", "ready", or "error". You can use the value of data and error to determine the current state of the request, and return the corresponding UI.