|
2 | 2 |
|
3 | 3 |    |
4 | 4 |
|
5 | | -`NetworkKit` is a Swift package designed to simplify networking operations in `SwiftUI`. It offers three flexible approaches for fetching, downloading, and uploading data or files using `Escaping Closures`, `Combine`, and `async-await` techniques, allowing you to choose the method that best suits your project requirements. |
| 5 | +`SwiftNet` is a Swift package designed to simplify networking operations in `SwiftUI`. It offers three flexible approaches for fetching, downloading, and uploading data or files using `Escaping Closures`, `Combine`, and `async-await` techniques, allowing you to choose the method that best suits your project requirements. |
6 | 6 |
|
7 | 7 | ## Table of contents |
8 | 8 | - [Requirements](#requirements) |
@@ -43,10 +43,101 @@ https://github.com/MMMagicCoder/SwiftNet.git |
43 | 43 |
|
44 | 44 | Below are examples for each type of task. |
45 | 45 |
|
46 | | -Fetching Data |
47 | | -<a id="fetching-data"></a> |
| 46 | +### Fetching Data |
| 47 | +<a id="fetching"></a> |
48 | 48 |
|
49 | 49 | `SwiftNet` provides two types of data fetching: `JSON Fetching` and `Data Fetching`. For JSON fetching, your model must conform to the `FetchableModel` protocol, which ensures the proper structure for decoding JSON responses. |
50 | 50 |
|
| 51 | +- JSON Fetching |
51 | 52 |
|
| 53 | +You can fetch JSON data by using the following methods, depending on the approach you choose. Make sure your model conforms to FetchableModel: |
52 | 54 |
|
| 55 | +```swift |
| 56 | +struct DataModel: FetchableModel { |
| 57 | + let userId: Int |
| 58 | + let id: Int |
| 59 | + let title: String |
| 60 | + let body: String |
| 61 | +} |
| 62 | +``` |
| 63 | + |
| 64 | +Using Escaping Closures |
| 65 | + |
| 66 | +```swift |
| 67 | + networkManager.fetchJSON(fromURL: "https://example.com/api/data") { (result: [MyModel]?, response, error) in |
| 68 | + if let result = result { |
| 69 | + // Handle the decoded JSON result |
| 70 | + } else if let error = error { |
| 71 | + // Handle the error |
| 72 | + } |
| 73 | +} |
| 74 | +``` |
| 75 | + |
| 76 | +Using Combine |
| 77 | + |
| 78 | +```swift |
| 79 | + networkManager.fetchJSON(fromURL: url) { (returnedData: [DataModel]?, response, error) in |
| 80 | + if let result = result { |
| 81 | + // Handle the decoded JSON result |
| 82 | + } else if let error = error { |
| 83 | + // Handle the error |
| 84 | + } |
| 85 | +} |
| 86 | +``` |
| 87 | + |
| 88 | +Using async-await |
| 89 | + |
| 90 | +```swift |
| 91 | +Task { |
| 92 | + do { |
| 93 | + let result: [MyModel] = try await networkManager.fetchJSON(fromURL: "https://example.com/api/data") |
| 94 | + // Handle the decoded JSON result |
| 95 | + } catch { |
| 96 | + // Handle the error |
| 97 | + } |
| 98 | +} |
| 99 | +``` |
| 100 | + |
| 101 | +- Data Fetching |
| 102 | + |
| 103 | + Data fetching is used when you need to fetch raw data (e.g., images, binary files) without decoding it into a specific model. |
| 104 | + |
| 105 | + Using Escaping Closures |
| 106 | + |
| 107 | +```swift |
| 108 | + networkManager.fetchData(fromURL: "https://example.com/file") { data, response, error in |
| 109 | + if let data = data { |
| 110 | + // Handle the fetched data |
| 111 | + } else if let error = error { |
| 112 | + // Handle the error |
| 113 | + } |
| 114 | +} |
| 115 | +``` |
| 116 | + |
| 117 | +Using Combine |
| 118 | + |
| 119 | +```swift |
| 120 | + networkManager.fetchData(fromURL: url) { (returnedData, response, error) in |
| 121 | + if let result = result { |
| 122 | + // Handle the returned data |
| 123 | + } else if let error = error { |
| 124 | + // Handle the error |
| 125 | + } |
| 126 | +} |
| 127 | +``` |
| 128 | + |
| 129 | +Using async-await |
| 130 | + |
| 131 | +```swift |
| 132 | +Task { |
| 133 | + do { |
| 134 | + let data = try await networkManager.fetchData(fromURL: "https://example.com/file") |
| 135 | + // Handle the fetched data |
| 136 | + } catch { |
| 137 | + // Handle the error |
| 138 | + } |
| 139 | +} |
| 140 | +``` |
| 141 | + |
| 142 | +### Downloading Files |
| 143 | +<a id="downloading-files"></a> |
0 commit comments