When you don’t know the type of feed, use the universal Feed enum type.
The Feed enum type handles RSS, RDF, Atom and JSON feeds and will determine the type of feed before reading, parsing and decoding occurs.
// Read any type of feed
let feed = try await Feed(urlString: "https://surprise.example/feed")
// Use a switch to get the resulting feed model
switch feed {
case let .atom(feed): // An AtomFeed instance
case let .rss(feed): // An RSSFeed instance
case let .rdf(feed): // An RDFFeed instance
case let .json(feed): // A JSONFeed instance
}
Manual Feed Type Detection
The universal Feed enum above automatically determines the feed type, so you generally don’t need to do it manually. However, using a FeedType enum can be helpful when you want to identify the type of feed without
the overhead of parsing and decoding.
Show
let feedType = try FeedType(data: data)
// Detect feed type
switch feedType {
case .rss: // RSS feed detected
case .rdf: // RDF feed detected
case .atom: // Atom feed detected
case .json: // JSON feed detected
}
// Or feed content type
if feedType.isXML {
// XML feed detected
} else if feedType.isJSON {
// JSON feed detected
}
Initializers
All feed types conform to FeedInitializable, sharing multiple common initializers. They provide a flexible way to read feeds from the most common sources.
Note: The URL-fetching initializers require URLSession, which Foundation does not provide on WASI. On WASI, use the local fileURL:, string:, and data: initializers.
Show
From a URL String:
init(urlString: String) async throws
From a URL, handling both local file URLs and remote URLs:
init(url: URL) async throws
From a local file URL:
init(fileURL url: URL) throws
From a remote URL:
init(remoteURL url: URL) async throws
From an XML or JSON String:
init(string: String) throws
From raw Data:
init(data: Data) throws
Feed Generator
XML
To generate an XML string for any given XML feed, create an instance of an RSSFeed or AtomFeed and populate it with the necessary data.
let feed = RSSFeed(
channel: .init(
title: "Breaking News",
link: "http://www.breakingnews.example/",
description: "Get the latest updates as they happen.",
// ...
items: [
.init(
title: "Breaking News: All Hearts are Joyful",
link: "http://breakingnews.example/2025/01/09/joyful-hearts",
description: "A heartwarming story of unity and celebration."
// ...
),
]
)
)
Then call toXMLString(formatted:) to generate the XML string.
try feed.toXMLString(formatted: true)
Output
<?xml version="1.0" encoding="UTF-8"?><rssversion="2.0"><channel><title>Breaking News</title><link>http://www.breakingnews.example/</link><description>Get the latest updates as they happen.</description><item><title>Breaking News: All Hearts are Joyful</title><link>http://breakingnews.example/2025/01/09/joyful-hearts</link><description>A heartwarming story of unity and celebration.</description></item></channel></rss>
JSON
In the same way, you can create an instance of a JSONFeed, populate it, and then call toJSONString(formatted:) to generate a JSON string.
try feed.toJSONString(formatted: true)
Feed Models
The RSS, RDF, Atom, and JSON feed models are highly comprehensive, especially when combined with all the supported namespaces. Below is a small preview of what’s available.
feed.channel?.title
feed.channel?.link
feed.channel?.description
// ...
feed.channel?.dublinCore
feed.channel?.syndication
// In RSS 1.0 the items are siblings of the channel
let item = feed.items?.first
item?.title
item?.link
item?.description
// ...
item?.dublinCore
item?.content
评论