Hello after long time I am writing this blog regarding use of Alamofire with Codable model object for the ease of MVC architecture.
Let’s rewind with some of the post which is used to cover this point:
Let see about use of Alamofire with Codable model object
Step 1 – Create Codable object based on JSON response.
Here I am taking an example of a web service which contains JSON related to the contact.
1
2
3
4
5
6
7
8
9
10
11
12
{
"id":"c200",
"name":"Ravi Tamada",
"email":"ravi@gmail.com",
"address":"xx-xx-xxxx,x - street, x - country",
"gender":"male",
"phone":{
"mobile":"+91 0000000000",
"home":"00 000000",
"office":"00 000000"
}
}
So, we can create two Codable files from this JSON as per hierarchy. These would be like as follows:
1
2
3
4
5
6
7
8
9
10
11
12
13
structContact:Codable{
varid:String
varname:String
varemail:String
varaddress:String
vargender:String
varphone:Phone
}
structPhone:Codable{
varmobile:String
varhome:String
varoffice:String
}
As you can see we have created 2 objects Contact and Phone. Here Phone is in sub hierarchy.
Step 2 – Let’s update AFWrapper
As we have previously revised blogs, there is a blog mentioned about to create a wrapper of the Alamofire and SwiftyJSON. We have to replace the SwiftyJSON code to return normal dictionary objects, and after that we will convert that dictionary to the Codable object.
So here I have updated the AFWrapper class file to do that thing:
Step 3 – Implement and convert the response in object
We will call a web service which contain that kind of JSON response. And after calling the web-service we will decode the object to create the Codable object.
// Dispose of any resources that can be recreated.
}
}
In above sample I have added one text view in the view controller and I am fetching the data from the contacts API. After that using JSONSerialization and JSONDecoder we will get our decoded Contact object with Phone object.
Conclusion
Swift 4 comes with so many updates. We are going to have a great time exploring them all and finding out so much more by using it on further projects. We have learned about Codable and use of it. You too should go check it out and share your experiences with us in the comments section.
Hope you liked the article.
Happy Coding
This article is updated with Swift 4 – Xcode 9 – iOS 11
Why to use a library everytime?
Slider Menu (Drawer) Let’s create our own Slide Menu (Drawer) in Swift 4. 1. Create New Project in Xcode 9 with Swift Language
2. Design the Menu in UIViewController Menu UIViewController
Declaration of Variables and Protocols (Delegate) :
Swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
protocol SlideMenuDelegate {
func slideMenuItemSelectedAtIndex(_ index : Int32)
}
class MenuViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
/**
* Array to display menu options
*/
@IBOutlet var tblMenuOptions : UITableView!
/**
* Transparent button to hide menu
*/
@IBOutlet var btnCloseMenuOverlay : UIButton!
/**
* Array containing menu options
*/
var arrayMenuOptions = [Dictionary<String,String>]()
/**
* Menu button which was tapped to display the menu
*/
var btnMenu : UIButton!
/**
* Delegate of the MenuVC
*/
vardelegate:SlideMenuDelegate?
}
Following method is for updating the Items in the Menu :
3. Now we will create a Base UIViewController to use anywhere in the project which control the delegate of menu.
First we will create this 3 lines Drawer Icon via Code
To open a view controller by identifier :
Set the Restoration Identifier and Storyboard Identifier. If current view is open then we will not open it once again for that we have to check via Restoration Identifier.