-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f1cfce0
commit 5b4bdc5
Showing
4 changed files
with
206 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
Sources/BbcNews/Utils/JSONDecoder+DecodeWithoutThrowing.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// | ||
// JSONDecoder+DecodeWithoutThrowing.swift | ||
// BbcNews | ||
// | ||
// Created by Bilaal Rashid on 09/07/2024. | ||
// | ||
|
||
import Foundation | ||
|
||
extension JSONDecoder { | ||
/// Returns a value of the type you specify, decoded from a JSON object. | ||
/// | ||
/// - Parameters: | ||
/// - type: The type of the value to decode from the supplied JSON object. | ||
/// - data: The JSON object to decode. | ||
/// - Returns: A value of the specified type, if the decoder can parse the data. | ||
func decodeWithoutThrowing<T>(_ type: T.Type, from data: Data) -> Result<T, Error> where T: Decodable { | ||
do { | ||
return .success(try self.decode(type, from: data)) | ||
} catch let error { | ||
return .failure(error) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters