QuoteKit is a Swift framework for accessing quotes from the Quotable API. It provides a clean, async/await interface for fetching quotes, authors, and tags.
As it uses the async/await feature of Swift 5.5, the platforms supported are iOS 15.0+, macOS 12.0+, watchOS 8.0+, and tvOS 15.0+.
Add QuoteKit to your project using Swift Package Manager:
dependencies: [
.package(url: "https://github.com/rudrankriyam/QuoteKit.git", .upToNextMajor(from: "2.0.0"))
]
QuoteKit provides static methods for fetching data. All methods are async and throw errors on failure.
Returns a single random quote:
let quote = try await QuoteKit.randomQuote()
print(quote.content)
You can filter by tags, length, or author:
// Quotes with specific tags
try await QuoteKit.randomQuote(tags: ["wisdom", "life"])
// Quotes within length range
try await QuoteKit.randomQuote(minLength: 50, maxLength: 150)
// Quotes by specific authors
try await QuoteKit.randomQuote(authors: ["albert-einstein"])
Returns a paginated list of quotes. By default returns 10 quotes per page:
let quotes = try await QuoteKit.quotes()
print("Found \(quotes.count) quotes")
Filter and sort quotes:
// Filter by tags
try await QuoteKit.quotes(tags: ["love", "friendship"])
// Filter by length
try await QuoteKit.quotes(minLength: 50, maxLength: 150)
// Filter by author
try await QuoteKit.quotes(authors: ["mark-twain"])
// Sort by date added
try await QuoteKit.quotes(sortBy: .dateAdded, order: .descending)
// Pagination
try await QuoteKit.quotes(limit: 25, page: 2)
Returns a single quote by its ID:
let quote = try await QuoteKit.quote(id: "abc123")
Returns a paginated list of authors. By default returns 10 authors per page:
let authors = try await QuoteKit.authors()
Filter and sort authors:
// Pagination
try await QuoteKit.authors(limit: 25, page: 2)
// Sort by name
try await QuoteKit.authors(sortBy: .name)
// Sort by quote count
try await QuoteKit.authors(sortBy: .quoteCount, order: .descending)
Returns a single author by ID:
let author = try await QuoteKit.author(id: "abc123")
Returns the image URL for an author's profile:
let imageURL = QuoteKit.authorImage(with: "albert-einstein")
Returns all available tags:
let tags = try await QuoteKit.tags()
Search for quotes containing specific text:
let quotes = try await QuoteKit.searchQuotes(for: "love")
Search for authors by name:
let authors = try await QuoteKit.searchAuthors(for: "einstein")
Represents a single quote:
struct Quote: Decodable, Identifiable {
let id: String
let tags: [String]
let content: String
let author: String
let authorSlug: String
let length: Int
let dateAdded: String
let dateModified: String
}
Represents a single author:
struct Author: Decodable, Identifiable {
let id: String
let link: String
let bio: String
let description: String
let name: String
let quoteCount: Int
let slug: String
let dateAdded: String
let dateModified: String
let quotes: [Quote]?
}
These are collections that support pagination:
typealias Quotes = QuoteItemCollection<Quote>
typealias Authors = QuoteItemCollection<Author>
Represents a quote tag:
struct Tag: Decodable, Identifiable {
let id: String
let name: String
let quoteCount: Int
let dateAdded: String
let dateModified: String
}
A generic paginated collection:
struct QuoteItemCollection<Item: Decodable>: Decodable {
let count: Int
let totalCount: Int
let page: Int
let totalPages: Int
let lastItemIndex: Int?
let results: [Item]
}