Skip to content

Commit 82f0554

Browse files
authored
Update README.md
1 parent 7d8dc14 commit 82f0554

File tree

1 file changed

+94
-3
lines changed

1 file changed

+94
-3
lines changed

README.md

Lines changed: 94 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
![](https://img.shields.io/badge/platform-iOS-d3d3d3) ![](https://img.shields.io/badge/iOS-14.0%2B-43A6C6) ![](https://img.shields.io/badge/Swift-5-F86F15)
44

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.
66

77
## Table of contents
88
- [Requirements](#requirements)
@@ -43,10 +43,101 @@ https://github.com/MMMagicCoder/SwiftNet.git
4343

4444
Below are examples for each type of task.
4545

46-
Fetching Data
47-
<a id="fetching-data"></a>
46+
### Fetching Data
47+
<a id="fetching"></a>
4848

4949
`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.
5050

51+
- JSON Fetching
5152

53+
You can fetch JSON data by using the following methods, depending on the approach you choose. Make sure your model conforms to FetchableModel:
5254

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

Comments
 (0)