{"id":778,"date":"2016-11-04T08:01:35","date_gmt":"2016-11-04T08:01:35","guid":{"rendered":"https:\/\/admin.qltech.com.au\/?post_type=knowledgehub&#038;p=778"},"modified":"2019-11-06T12:11:48","modified_gmt":"2019-11-06T12:11:48","slug":"swift-best-practices-and-tips-by-toptal-developers","status":"publish","type":"post","link":"https:\/\/steamlinedesign.com\/qltech\/new\/2016\/11\/04\/swift-best-practices-and-tips-by-toptal-developers\/","title":{"rendered":"Swift Best Practices and Tips by Toptal Developers"},"content":{"rendered":"\n<p>Swift as a programming language was brought to the limelight in the year 2014. Swift has transformed the complex practice of programming&nbsp;<strong><a href=\"https:\/\/www.qltech.com.au\/category\/develop\/mobile-apps-development\/\">mobile app development<\/a>&nbsp;<\/strong>into an easy to write, learn and understand proposing the up to date features. Designed by Apple INC, Swift is specifically developed to facilitate iOS programming for products running on iOS platform. It is loaded with LLVM compiler that features C, C++, Objective-C and Swift code to run within one program. Swift is very friendly to program developers as it supports backgrounds, a unique which feature which allows the iOS developer to edit the script codes and generates result immediately without running the app again. Google has also announced her intention for using Swift as a first-class language for its Android app development. Here we present you some of the practices and tips followed by Toptal developers to make iOS app development easy.<\/p>\n\n\n\n<p><strong><em>A Dose of Insight: \u201c<a href=\"https:\/\/www.toptal.com\/\" target=\"_blank\" rel=\"noreferrer noopener\">Toptal<\/a>&nbsp;is a community of designers and designers who are dedicated to exchanging services to companies, to each other as well as to the community as a whole. The members of Toptal are considered as the top 3 percent amongst all the designers and developers in the world.\u201d<\/em><\/strong><\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"546\" height=\"400\" src=\"https:\/\/admin.qltech.com.au\/wp-content\/uploads\/2019\/09\/common-binding-code.jpg\" alt=\"common-binding-code\" class=\"wp-image-779\" srcset=\"https:\/\/steamlinedesign.com\/qltech\/new\/wp-content\/uploads\/2019\/09\/common-binding-code.jpg 546w, https:\/\/steamlinedesign.com\/qltech\/new\/wp-content\/uploads\/2019\/09\/common-binding-code-300x220.jpg 300w\" sizes=\"auto, (max-width: 546px) 100vw, 546px\" \/><\/figure><\/div>\n\n\n\n<p>Before we delve into the world of Swift development, one needs to understand the defining elements as well as the differences between objects, interfaces, and model classes.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>QL Tech Trivia 101:<\/strong><\/h2>\n\n\n\n<p><strong>Object:<\/strong>&nbsp;In the world of software development, an object is an entity within the coded structure of an application. It is a group or arrangement of data and functions that help in defining specific aspects of the functioning of the application.<\/p>\n\n\n\n<p><strong>Class:<\/strong>&nbsp;The methods along with the variables that are a part an object make up the class of the object. They can be divided on the basis of the methods and variables that are considered.<\/p>\n\n\n\n<p><strong>Interface:<\/strong>&nbsp;This is the part of the application in which only the declarations from the object are visible and utilized. This means that an interface essentially displays or makes use of the output born from objects and the arrangement of classes in an application.<\/p>\n\n\n\n<p>In the above image, more than one object has same common binding to accelerate the working efficiency. The reuse of the same code is a sensible idea, as introduced by Swift. For example using a verbatim code language Protocol Extensions to Bind Model classes with interfaces can be understood as under:<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Let\u2019s suppose we have a \u2018User\u2019 class:<\/h3>\n\n\n\n<p>class User {<\/p>\n\n\n\n<p>var name = \u201c\u201d<\/p>\n\n\n\n<p>var email = \u201c\u201d<\/p>\n\n\n\n<p>var bio = \u201c\u201d<\/p>\n\n\n\n<p>var image: UIImage? = nil<\/p>\n\n\n\n<p>init(name: String, email: String, bio: String) {<\/p>\n\n\n\n<p>self.name = name<\/p>\n\n\n\n<p>self.email = email<\/p>\n\n\n\n<p>self.bio = bio<\/p>\n\n\n\n<p>}<\/p>\n\n\n\n<p>}<\/p>\n\n\n\n<p>Gradually a protocol will implement all the interfaces together that would be bind with the \u2018User\u2019 instances calling it \u2018UserBindable\u2019.<\/p>\n\n\n\n<p>protocol UserBindable: AnyObject {<br>var user: User? { get set }<\/p>\n\n\n\n<p>var nameLabel: UILabel! { get }<br>var emailLabel: UILabel! { get }<br>var bioLabel: UILabel! { get }<br>var imageView: UIImageView! { get }<br>}<\/p>\n\n\n\n<p>Here \u2018User\u2019 as an individual variable is a user (operator) to bind and all other \u2018UIView\u2019 the subclasses use to bind the user. The image below will clarify the above code understanding:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"860\" height=\"316\" src=\"https:\/\/admin.qltech.com.au\/wp-content\/uploads\/2019\/09\/user-subclasses-protocol-extension-1.jpg\" alt=\"user-subclasses-protocol-extension-1\" class=\"wp-image-780\" srcset=\"https:\/\/steamlinedesign.com\/qltech\/new\/wp-content\/uploads\/2019\/09\/user-subclasses-protocol-extension-1.jpg 860w, https:\/\/steamlinedesign.com\/qltech\/new\/wp-content\/uploads\/2019\/09\/user-subclasses-protocol-extension-1-300x110.jpg 300w, https:\/\/steamlinedesign.com\/qltech\/new\/wp-content\/uploads\/2019\/09\/user-subclasses-protocol-extension-1-768x282.jpg 768w\" sizes=\"auto, (max-width: 860px) 100vw, 860px\" \/><\/figure>\n\n\n\n<p>Now, let\u2019s we create a protocol extension:<br>extension UserBindable {<\/p>\n\n\n\n<p>\/\/ Make the views optionals<\/p>\n\n\n\n<p>var nameLabel: UILabel! {<br>return nil<br>}<\/p>\n\n\n\n<p>var emailLabel: UILabel! {<br>return nil<br>}<\/p>\n\n\n\n<p>var bioLabel: UILabel! {<br>return nil<br>}<\/p>\n\n\n\n<p>var imageView: UIImageView! {<br>return nil<br>}<\/p>\n\n\n\n<p>\/\/ Bind<\/p>\n\n\n\n<p>func bind(user: User) {<br>self.user = user<br>bind()<br>}<\/p>\n\n\n\n<p>func bind() {<\/p>\n\n\n\n<p>guard let user = self.user else {<br>return<br>}<\/p>\n\n\n\n<p>if let nameLabel = self.nameLabel {<br>nameLabel.text = user.name<br>}<\/p>\n\n\n\n<p>if let bioLabel = self.bioLabel {<br>bioLabel.text = user.bio<br>}<\/p>\n\n\n\n<p>if let emailLabel = self.emailLabel {<br>emailLabel.text = user.email<br>}<\/p>\n\n\n\n<p>if let imageView = self.imageView {<br>imageView.image = user.image<br>}<br>}<br>}<\/p>\n\n\n\n<p>Here the extension is divided into two parts:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><a href=\"http:\/\/www.123rf.com\/\" target=\"_blank\" rel=\"noreferrer noopener\"><img loading=\"lazy\" decoding=\"async\" width=\"614\" height=\"178\" src=\"https:\/\/admin.qltech.com.au\/wp-content\/uploads\/2019\/09\/utb-1-2.png\" alt=\"UIt table for cell value\" class=\"wp-image-781\" srcset=\"https:\/\/steamlinedesign.com\/qltech\/new\/wp-content\/uploads\/2019\/09\/utb-1-2.png 614w, https:\/\/steamlinedesign.com\/qltech\/new\/wp-content\/uploads\/2019\/09\/utb-1-2-300x87.png 300w\" sizes=\"auto, (max-width: 614px) 100vw, 614px\" \/><\/a><\/figure><\/div>\n\n\n\n<ol class=\"wp-block-list\"><li>Default Value created that allows some exclusion and some inclusion.<\/li><li>All the values of user properties and fix to view.<\/li><\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">Presenting a user list, create a \u2018UITableViewCell\u2019.<\/h3>\n\n\n\n<p>class UserTableViewCell: UITableViewCell, UserBindable {<\/p>\n\n\n\n<p>var user: User?<\/p>\n\n\n\n<p>\/\/ we can set the labels in interface builder or with by code.<br>@IBOutlet weak var nameLabel: UILabel!<br>@IBOutlet weak var emailLabel: UILabel!<br>}<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"615\" height=\"185\" src=\"https:\/\/admin.qltech.com.au\/wp-content\/uploads\/2019\/09\/cell-bodu-content-image.jpg\" alt=\"cell-bodu-content-image\" class=\"wp-image-782\" srcset=\"https:\/\/steamlinedesign.com\/qltech\/new\/wp-content\/uploads\/2019\/09\/cell-bodu-content-image.jpg 615w, https:\/\/steamlinedesign.com\/qltech\/new\/wp-content\/uploads\/2019\/09\/cell-bodu-content-image-300x90.jpg 300w\" sizes=\"auto, (max-width: 615px) 100vw, 615px\" \/><\/figure><\/div>\n\n\n\n<p>The above image indicates that the cell is binding the user through the use of the UserBindable protocol. This binds its interface to the user object.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">In the UITableViewDataSource protocol, we can see the following object classes:<\/h3>\n\n\n\n<p>func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -&gt; UITableViewCell {<br>let cell = tableView.dequeueReusableCellWithIdentifier(\u201cCell\u201d) as! UserTableViewCell<br>let user = \/\/ find the user from your array or whatever<br>cell.bind(user)<br>return cell<br>}<\/p>\n\n\n\n<p>If we want to show the detail of this user after touching it, we can have a view controller like this:<\/p>\n\n\n\n<p>class UserDetailViewController: UIViewController, UserBindable {<\/p>\n\n\n\n<p>var user: User?<\/p>\n\n\n\n<p>@IBOutlet weak var nameLabel: UILabel!<br>@IBOutlet weak var emailLabel: UILabel!<br>@IBOutlet weak var bioLabel: UILabel!<br>@IBOutlet weak var imageView: UIImageView!<\/p>\n\n\n\n<p>override func viewDidLoad() {<br>super.viewDidLoad()<br>\/\/ here we suppose that we have set the user value before the viewDidLoad<br>bind()<br>}<br>}<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"655\" height=\"590\" src=\"https:\/\/admin.qltech.com.au\/wp-content\/uploads\/2019\/09\/code-display-by-qltech-3.jpg\" alt=\"code display by qltech\" class=\"wp-image-783\" srcset=\"https:\/\/steamlinedesign.com\/qltech\/new\/wp-content\/uploads\/2019\/09\/code-display-by-qltech-3.jpg 655w, https:\/\/steamlinedesign.com\/qltech\/new\/wp-content\/uploads\/2019\/09\/code-display-by-qltech-3-300x270.jpg 300w\" sizes=\"auto, (max-width: 655px) 100vw, 655px\" \/><\/figure>\n\n\n\n<p>Thus, the above example codes shown the efficient use and output of reused binding code in android mobile app development.<\/p>\n\n\n\n<p><strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Use `NSDateFormatter` efficiently<\/strong><\/p>\n\n\n\n<p>Creating NSDateFormatter is a tedious job for any android programmer. But one thing that is to be notified here is that all the formatters to be used in the app are static constant. They can be reused and no need to create them again and again. The image below is an example of showing static constant:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"358\" height=\"240\" src=\"https:\/\/admin.qltech.com.au\/wp-content\/uploads\/2019\/09\/NSDateFormatter-1.png\" alt=\"NSDateFormatter\" class=\"wp-image-784\" srcset=\"https:\/\/steamlinedesign.com\/qltech\/new\/wp-content\/uploads\/2019\/09\/NSDateFormatter-1.png 358w, https:\/\/steamlinedesign.com\/qltech\/new\/wp-content\/uploads\/2019\/09\/NSDateFormatter-1-300x201.png 300w\" sizes=\"auto, (max-width: 358px) 100vw, 358px\" \/><\/figure><\/div>\n\n\n\n<p>Here in the above image the day changes but the formatters remain constant. Like the name of days, 12-hour format, the name of months and numbers from 0-9.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">The example of the code is as follows:<\/h3>\n\n\n\n<p>extension NSDateFormatter {<\/p>\n\n\n\n<p>@nonobjc static let shortDateAndTime: NSDateFormatter = {<br>let formatter = NSDateFormatter()<br>formatter.dateStyle = .ShortStyle<br>formatter.timeStyle = .ShortStyle<br>return formatter<br>}()<\/p>\n\n\n\n<p>@nonobjc static let dayMonthAndYear: NSDateFormatter = {<br>let formatter = NSDateFormatter()<br>formatter.dateFormat = \u201cMM\/dd\/yyyy\u201d<br>return formatter<br>}()<\/p>\n\n\n\n<p>@nonobjc static let monthAndYear: NSDateFormatter = {<br>let formatter = NSDateFormatter()<br>formatter.setLocalizedDateFormatFromTemplate(\u201cMMMyyyy\u201d)<br>return formatter<br>}()<br>}<\/p>\n\n\n\n<p>Now in the above code, we can make out @nonobjc attribute. This means that the value is supposed to be added in the cell. A complaint of \u2018<em>A declaration cannot be both \u2018final\u2019 and \u2018dynamic\u2019.<\/em>\u2018 is seen. If the formatter has applied for \u201cDD\/MM\/YYYY\u201d format, then the date should be \u201c01\/01\/2000\u201d. No object can be added (\u201c001\u201d) nor can be deducted (\u201c1\u201d). Similarly, notice that adding the @objc will make your extension incompatible with Objective-C.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Next, let\u2019s we create an NSDate extension to make a simple API convert to strings and back:<\/h3>\n\n\n\n<p>extension NSDate {<\/p>\n\n\n\n<p>\/\/\/ Prints a string representation for the date with the given formatter<br>func string(with format: NSDateFormatter) -&gt; String {<br>return format.stringFromDate(self)<br>}<\/p>\n\n\n\n<p>\/\/\/ Creates an `NSDate` from the given string and formatter. Nil if the string couldn\u2019t be parsed<br>convenience init?(string: String, formatter: NSDateFormatter) {<br>guard let date = formatter.dateFromString(string) else { return nil }<br>self.init(timeIntervalSince1970: date.timeIntervalSince1970)<br>}<br>}<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Let\u2019s call the NSDate extension methods.<\/h3>\n\n\n\n<p>To create a String from a NSDate:<\/p>\n\n\n\n<p>let date = NSDate()let string = date.string(with: .shortDateAndTime)\/\/ let string = date.string(with: .dayMonthAndYear)\/\/ let string = date.string(with: .monthAndYear)<\/p>\n\n\n\n<p>To create a NSDate from a String<\/p>\n\n\n\n<p>let string = \u201c06\/17\/2016\u201d<br>let date = NSDate(string: string, formatter: .dayMonthAndYear)<\/p>\n\n\n\n<p>By looking at the several techniques and coding customizability that Swift provides, developers can have a field day with the process of iOS application development. This article barely gives a glimpse at the gates of the opportunity that Swift throws open for developers and designers. Would you like to know more, or would you be feeling the urge to share your inputs? Let us know so that we can explore the realm beyond the gates of opportunity together.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Swift as a programming language was brought to the limelight in the year 2014. Swift has transformed the complex practice of programming&nbsp;mobile app development&nbsp;into an easy to write, learn and understand proposing the up to date features. Designed by Apple INC, Swift is specifically developed to facilitate iOS programming for products running on iOS platform. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"lbn_published_stage":false,"lbn_published_production":false,"footnotes":""},"categories":[1935],"tags":[1741,1739,1740],"yst_prominent_words":[597,584,591,586,589,585,596,601,600,180,599,594,588,595,587,593,598,592,602,590],"class_list":["post-778","post","type-post","status-publish","format-standard","hentry","category-mobile-apps","tag-ios-app-development","tag-swift","tag-swift-developemnt"],"yoast":{"focuskw":"swift","title":"Swift Best Practices and Tips by Toptal Developers- QL Tech","metadesc":"read best ways in which Toptal Developers can use Swift best Practices for iOS app development, Android development & iOS programming.","linkdex":"69","metakeywords":"","meta-robots-noindex":"","meta-robots-nofollow":"","meta-robots-adv":"","canonical":"","redirect":"","opengraph-title":"","opengraph-description":"","opengraph-image":"","twitter-title":"","twitter-description":"","twitter-image":""},"acf":{"img":"https:\/\/steamlinedesign.com\/qltech\/new\/wp-content\/uploads\/2019\/09\/common-binding-code-1.jpg","long_descrpition":"","written_by":"","feature_image":"https:\/\/steamlinedesign.com\/qltech\/new\/wp-content\/uploads\/2016\/11\/swift-best-practices-by-topal-developers-featured-1-1.jpg","posted_by":"QL Tech","is_featured":"0:Non Featured","knowledge_hub_featured_image":false,"short_description":"","icon":false,"service_slider":false},"_links":{"self":[{"href":"https:\/\/steamlinedesign.com\/qltech\/new\/wp-json\/wp\/v2\/posts\/778","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/steamlinedesign.com\/qltech\/new\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/steamlinedesign.com\/qltech\/new\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/steamlinedesign.com\/qltech\/new\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/steamlinedesign.com\/qltech\/new\/wp-json\/wp\/v2\/comments?post=778"}],"version-history":[{"count":4,"href":"https:\/\/steamlinedesign.com\/qltech\/new\/wp-json\/wp\/v2\/posts\/778\/revisions"}],"predecessor-version":[{"id":1913,"href":"https:\/\/steamlinedesign.com\/qltech\/new\/wp-json\/wp\/v2\/posts\/778\/revisions\/1913"}],"wp:attachment":[{"href":"https:\/\/steamlinedesign.com\/qltech\/new\/wp-json\/wp\/v2\/media?parent=778"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/steamlinedesign.com\/qltech\/new\/wp-json\/wp\/v2\/categories?post=778"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/steamlinedesign.com\/qltech\/new\/wp-json\/wp\/v2\/tags?post=778"},{"taxonomy":"yst_prominent_words","embeddable":true,"href":"https:\/\/steamlinedesign.com\/qltech\/new\/wp-json\/wp\/v2\/yst_prominent_words?post=778"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}