{"id":25435,"date":"2015-08-11T22:12:31","date_gmt":"2015-08-11T16:42:31","guid":{"rendered":"https:\/\/2thenew.xyz\/blog\/?p=25435"},"modified":"2015-08-26T12:48:23","modified_gmt":"2015-08-26T07:18:23","slug":"common-utility-extensions-in-swift-utils-swift","status":"publish","type":"post","link":"https:\/\/2thenew.xyz\/blog\/common-utility-extensions-in-swift-utils-swift\/","title":{"rendered":"Common Utility Extensions in Swift (Utils.swift)"},"content":{"rendered":"<h1>What is Extension:<\/h1>\n<p>Extensions are basically similar to an Objective-C category. Extensions are sort of methods adding in a Classe or Structure in a different flavour.<\/p>\n<h1>Syntax of Extension:<\/h1>\n<p>The syntax to create an extension is very similar to creating a type. You can write the following code in your Utils.swift class to add a <b>UIColor<\/b>\u00a0extension.<\/p>\n<div>\n<table>\n<tbody>\n<tr>\n<td>\n<pre>extension <span style=\"color: orange;\">UIColor<\/span>\r\n{\r\n    \/\/Fill in the blank\r\n}<\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<h1>Common Extensions List:<\/h1>\n<h3>NSData\u00a0Extensions:<\/h3>\n<div>\n<table>\n<tbody>\n<tr>\n<td>\n<pre>\/\/MARK: NSData Extensions\r\nextension <span style=\"color: orange;\">NSData<\/span> {\r\n    \r\n    \/\/ Convert Data to HexString mostly used in Encrypton Algos\r\n    public var hexString: String {\r\n        return self.toHexString()\r\n    }\r\n    \r\n    func toHexString() -&gt; String {\r\n        let count = self.length \/ sizeof(UInt8)\r\n        var bytesArray = [UInt8](count: count, repeatedValue: 0)\r\n        self.getBytes(&amp;bytesArray, length:count * sizeof(UInt8))\r\n        \r\n        var s:String = \"\";\r\n        for byte in bytesArray {\r\n            s = s + String(format:\"%02X\", byte)\r\n        }\r\n        return s;\r\n    }   \r\n\r\n\t\/\/ Return Swift Array from NSData\r\n    \r\n    \r\n    public func arrayOfBytes() -&gt; [UInt8] {\r\n        let count = self.length \/ sizeof(UInt8)\r\n        var bytesArray = [UInt8](count: count, repeatedValue: 0)\r\n        self.getBytes(&amp;bytesArray, length:count * sizeof(UInt8))\r\n        return bytesArray\r\n    }\r\n    \r\n    \/\/ Return array from NSData\r\n    class public func withBytes(bytes: [UInt8]) -&gt; NSData {\r\n        return NSData(bytes: bytes, length: bytes.count)\r\n    }\r\n}<\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p><b>Note:<\/b> NSData has an initializer that looks takes a bytes pointer: init(bytes: UnsafeMutablePointer , length: Int).<\/p>\n<p>You can read more about unsafe pointer parameters in Apple&#8217;s Interacting with C APIs<\/p>\n<h3>NSFileManager\u00a0Extensions:<\/h3>\n<div>\n<table>\n<tbody>\n<tr>\n<td>\n<pre>\/\/MARK: NSFileManager Extensions\r\n\r\nextension <span style=\"color: orange;\">NSFileManager<\/span> {\r\n    class func documentsDirectory() -&gt; String {\r\n        var paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true) as! [String]\r\n        return paths[0]\r\n    }\r\n    \r\n    class func cachesDirectory() -&gt; String {\r\n        var paths = NSSearchPathForDirectoriesInDomains(.CachesDirectory, .UserDomainMask, true) as! [String]\r\n        return paths[0]\r\n    }\r\n}\r\n<\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<div>\n<h3>Array Extensions:<\/h3>\n<table>\n<tbody>\n<tr>\n<td>\n<pre>\/\/MARK: Array Extensions\r\nextension <span style=\"color: orange;\">Array<\/span> {\r\n   \r\n    \/\/Array First Object\r\n    \r\n    func firstObject() -&gt; T! {\r\n        var firstItem: T!\r\n        if !self.isEmpty {\r\n            firstItem = self[0]\r\n        }\r\n        return firstItem\r\n    }\r\n    \r\n    \/\/Array Last Object\r\n    \r\n    func lastObject() -&gt; T! {\r\n        var lastItem: T!\r\n        if !self.isEmpty {\r\n            lastItem = self[self.count-1]\r\n        }\r\n        \r\n        return lastItem\r\n    }\r\n    \r\n    \r\n    \r\n    \/\/ Remove Object from Array similar to [ArrayObjec removeObject:(id)] in Objective-C\r\n\r\n    mutating func removeObject(object: U) -&gt; Bool {\r\n        for (idx, objectToCompare) in enumerate(self) {\r\n            if let to = objectToCompare as? U {\r\n                if object == to {\r\n                    self.removeAtIndex(idx)\r\n                    return true\r\n                }\r\n            }\r\n        }\r\n        return false\r\n    }\r\n    \r\n    \/\/ Check Array contains Object similar to [ArrayObjec containsObject:(id)] in Objective-C\r\n    \r\n    func contains(obj: T) -&gt; Bool {\r\n            return self.filter({$0 as? T == obj}).count &gt; 0\r\n        }\r\n}\r\n\r\n<\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<h3>NSObject Extensions:<\/h3>\n<div>\n<table>\n<tbody>\n<tr>\n<td>\n<pre>\/\/ Perform selector after delay in swift\r\n\r\nextension <span style=\"color: orange;\">NSObject<\/span> {\r\n    \r\n    func callSelectorAsync(selector: Selector, object: AnyObject?, delay: NSTimeInterval) -&gt; NSTimer {\r\n        \r\n        var timer = NSTimer.scheduledTimerWithTimeInterval(delay, target: self, selector: selector, userInfo: object, repeats: false)\r\n        return timer\r\n    }\r\n}\r\n<\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<h3>Localization String Extensions:<\/h3>\n<div>\n<table>\n<tbody>\n<tr>\n<td>\n<pre>\/\/ localized string extension\r\n\r\nextension  <span style=\"color: orange;\">String<\/span> {\r\n    var localized: String {\r\n        return NSLocalizedString(self, tableName:nil, bundle: NSBundle.mainBundle(), value: self, comment: \"\")\r\n    }\r\n}\r\n\r\n\r\n\/\/ localizing String with comment\r\n\r\nextension <span style=\"color: orange;\">String<\/span>{\r\n    \r\n    func localizedStringWithComment(comment:String)-&gt; String{\r\n        return NSLocalizedString(self, tableName: nil, bundle: NSBundle.mainBundle(), value: self, comment: comment)\r\n    }\r\n    \r\n}\r\n\r\nextension <span style=\"color: orange;\">String<\/span>{\r\n    \r\n \/\/ MARK: Remove White Spaces\r\n\r\n    var condenseWhitespace:String{\r\n        return  self.stringByReplacingOccurrencesOfString(\" \", withString: \"\")\r\n    }\r\n}<\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<h3>Integer Extensions:<\/h3>\n<div>\n<table>\n<tbody>\n<tr>\n<td>\n<pre>\/\/ MARK: Integer Extensions\r\n\r\n\/\/Add space before Integer\r\nextension <span style=\"color: orange;\">Int<\/span> {\r\n    func format(f: String) -&gt; String {\r\n        return NSString(format: \"%\\(f)d\", self) as String\r\n    }\r\n}\r\n\r\n\/\/MARK: Double Extensions\r\n\r\n\/\/Add space before Double\r\nextension <span style=\"color: orange;\">Double<\/span> {\r\n    func format(f: String) -&gt; String {\r\n        return NSString(format: \"%\\(f)f\", self) as String\r\n    }\r\n}\r\n<\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h1>Source:<\/h1>\n<h3 class=\"r\"><a href=\"https:\/\/developer.apple.com\/library\/ios\/documentation\/Swift\/Conceptual\/Swift_Programming_Language\/Extensions.html\">The Swift Programming Language: Extensions &#8211; Apple<\/a><\/h3>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>What is Extension: Extensions are basically similar to an Objective-C category. Extensions are sort of methods adding in a Classe or Structure in a different flavour. Syntax of Extension: The syntax to create an extension is very similar to creating a type. You can write the following code in your Utils.swift class to add a [&hellip;]<\/p>\n","protected":false},"author":169,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":15,"footnotes":""},"categories":[1400,1],"tags":[4848],"class_list":["post-25435","post","type-post","status-publish","format-standard","hentry","category-ios","category-technology","tag-ios"],"aioseo_notices":[],"aioseo_head":"\n\t\t<!-- All in One SEO 5.0.0.1 - aioseo.com -->\n\t<meta name=\"description\" content=\"What is Extension: Extensions are basically similar to an Objective-C category. Extensions are sort of methods adding in a Classe or Structure in a different flavour. Syntax of Extension: The syntax to create an extension is very similar to creating a type. You can write the following code in your Utils.swift class to add a\" \/>\n\t<meta name=\"robots\" content=\"max-image-preview:large\" \/>\n\t<meta name=\"author\" content=\"Abhayam Rastogi\"\/>\n\t<link rel=\"canonical\" href=\"https:\/\/2thenew.xyz\/blog\/common-utility-extensions-in-swift-utils-swift\/\" \/>\n\t<meta name=\"generator\" content=\"All in One SEO (AIOSEO) 5.0.0.1\" \/>\n\t\t<meta property=\"og:locale\" content=\"en_US\" \/>\n\t\t<meta property=\"og:site_name\" content=\"TO THE NEW BLOG\" \/>\n\t\t<meta property=\"og:type\" content=\"blog\" \/>\n\t\t<meta property=\"og:title\" content=\"Common Utility Extensions in Swift (Utils.swift) | TO THE NEW Blog\" \/>\n\t\t<meta property=\"og:description\" content=\"What is Extension: Extensions are basically similar to an Objective-C category. Extensions are sort of methods adding in a Classe or Structure in a different flavour. Syntax of Extension: The syntax to create an extension is very similar to creating a type. You can write the following code in your Utils.swift class to add a\" \/>\n\t\t<meta property=\"og:url\" content=\"https:\/\/2thenew.xyz\/blog\/common-utility-extensions-in-swift-utils-swift\/\" \/>\n\t\t<meta property=\"og:image\" content=\"https:\/\/2thenew.xyz\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png\" \/>\n\t\t<meta property=\"og:image:secure_url\" content=\"https:\/\/2thenew.xyz\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png\" \/>\n\t\t<meta name=\"twitter:card\" content=\"summary\" \/>\n\t\t<meta name=\"twitter:site\" content=\"@tothenew\" \/>\n\t\t<meta name=\"twitter:title\" content=\"Common Utility Extensions in Swift (Utils.swift) | TO THE NEW Blog\" \/>\n\t\t<meta name=\"twitter:description\" content=\"What is Extension: Extensions are basically similar to an Objective-C category. Extensions are sort of methods adding in a Classe or Structure in a different flavour. Syntax of Extension: The syntax to create an extension is very similar to creating a type. You can write the following code in your Utils.swift class to add a\" \/>\n\t\t<meta name=\"twitter:image\" content=\"https:\/\/2thenew.xyz\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png\" \/>\n\t\t<script type=\"application\/ld+json\" class=\"aioseo-schema\">\n\t\t\t{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/common-utility-extensions-in-swift-utils-swift\\\/#article\",\"name\":\"Common Utility Extensions in Swift (Utils.swift) | TO THE NEW Blog\",\"headline\":\"Common Utility Extensions in Swift (Utils.swift)\",\"author\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/author\\\/abhayam-rastogi\\\/#author\"},\"publisher\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/#organization\"},\"datePublished\":\"2015-08-11T22:12:31+05:30\",\"dateModified\":\"2015-08-26T12:48:23+05:30\",\"inLanguage\":\"en-US\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/common-utility-extensions-in-swift-utils-swift\\\/#webpage\"},\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/common-utility-extensions-in-swift-utils-swift\\\/#webpage\"},\"articleSection\":\"iOS, Technology, iOS\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/common-utility-extensions-in-swift-utils-swift\\\/#breadcrumblist\",\"itemListElement\":[{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog#listItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.tothenew.com\\\/blog\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/category\\\/technology\\\/#listItem\",\"name\":\"Technology\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/category\\\/technology\\\/#listItem\",\"position\":2,\"name\":\"Technology\",\"item\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/category\\\/technology\\\/\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/common-utility-extensions-in-swift-utils-swift\\\/#listItem\",\"name\":\"Common Utility Extensions in Swift (Utils.swift)\"},\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog#listItem\",\"name\":\"Home\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/common-utility-extensions-in-swift-utils-swift\\\/#listItem\",\"position\":3,\"name\":\"Common Utility Extensions in Swift (Utils.swift)\",\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/category\\\/technology\\\/#listItem\",\"name\":\"Technology\"}}]},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/#organization\",\"name\":\"TO THE NEW Blog\",\"url\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/author\\\/abhayam-rastogi\\\/#author\",\"url\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/author\\\/abhayam-rastogi\\\/\",\"name\":\"Abhayam Rastogi\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/common-utility-extensions-in-swift-utils-swift\\\/#authorImage\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/a32e8bc2dc0fb2ba517ed7d802045c65d7977124ef9e2ee1a93c2508994a21d9?s=96&d=mm&r=g\",\"width\":96,\"height\":96,\"caption\":\"Abhayam Rastogi\"}},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/common-utility-extensions-in-swift-utils-swift\\\/#webpage\",\"url\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/common-utility-extensions-in-swift-utils-swift\\\/\",\"name\":\"Common Utility Extensions in Swift (Utils.swift) | TO THE NEW Blog\",\"description\":\"What is Extension: Extensions are basically similar to an Objective-C category. Extensions are sort of methods adding in a Classe or Structure in a different flavour. Syntax of Extension: The syntax to create an extension is very similar to creating a type. You can write the following code in your Utils.swift class to add a\",\"inLanguage\":\"en-US\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/#website\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/common-utility-extensions-in-swift-utils-swift\\\/#breadcrumblist\"},\"author\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/author\\\/abhayam-rastogi\\\/#author\"},\"creator\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/author\\\/abhayam-rastogi\\\/#author\"},\"datePublished\":\"2015-08-11T22:12:31+05:30\",\"dateModified\":\"2015-08-26T12:48:23+05:30\"},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/\",\"name\":\"TO THE NEW Blog\",\"inLanguage\":\"en-US\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/#organization\"}}]}\n\t\t<\/script>\n\t\t<!-- All in One SEO -->\n\n","aioseo_head_json":{"title":"Common Utility Extensions in Swift (Utils.swift) | TO THE NEW Blog","description":"What is Extension: Extensions are basically similar to an Objective-C category. Extensions are sort of methods adding in a Classe or Structure in a different flavour. Syntax of Extension: The syntax to create an extension is very similar to creating a type. You can write the following code in your Utils.swift class to add a","canonical_url":"https:\/\/2thenew.xyz\/blog\/common-utility-extensions-in-swift-utils-swift\/","robots":"max-image-preview:large","keywords":"","webmasterTools":{"miscellaneous":""},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/2thenew.xyz\/blog\/common-utility-extensions-in-swift-utils-swift\/#article","name":"Common Utility Extensions in Swift (Utils.swift) | TO THE NEW Blog","headline":"Common Utility Extensions in Swift (Utils.swift)","author":{"@id":"https:\/\/2thenew.xyz\/blog\/author\/abhayam-rastogi\/#author"},"publisher":{"@id":"https:\/\/2thenew.xyz\/blog\/#organization"},"datePublished":"2015-08-11T22:12:31+05:30","dateModified":"2015-08-26T12:48:23+05:30","inLanguage":"en-US","mainEntityOfPage":{"@id":"https:\/\/2thenew.xyz\/blog\/common-utility-extensions-in-swift-utils-swift\/#webpage"},"isPartOf":{"@id":"https:\/\/2thenew.xyz\/blog\/common-utility-extensions-in-swift-utils-swift\/#webpage"},"articleSection":"iOS, Technology, iOS"},{"@type":"BreadcrumbList","@id":"https:\/\/2thenew.xyz\/blog\/common-utility-extensions-in-swift-utils-swift\/#breadcrumblist","itemListElement":[{"@type":"ListItem","@id":"https:\/\/2thenew.xyz\/blog#listItem","position":1,"name":"Home","item":"https:\/\/2thenew.xyz\/blog","nextItem":{"@type":"ListItem","@id":"https:\/\/2thenew.xyz\/blog\/category\/technology\/#listItem","name":"Technology"}},{"@type":"ListItem","@id":"https:\/\/2thenew.xyz\/blog\/category\/technology\/#listItem","position":2,"name":"Technology","item":"https:\/\/2thenew.xyz\/blog\/category\/technology\/","nextItem":{"@type":"ListItem","@id":"https:\/\/2thenew.xyz\/blog\/common-utility-extensions-in-swift-utils-swift\/#listItem","name":"Common Utility Extensions in Swift (Utils.swift)"},"previousItem":{"@type":"ListItem","@id":"https:\/\/2thenew.xyz\/blog#listItem","name":"Home"}},{"@type":"ListItem","@id":"https:\/\/2thenew.xyz\/blog\/common-utility-extensions-in-swift-utils-swift\/#listItem","position":3,"name":"Common Utility Extensions in Swift (Utils.swift)","previousItem":{"@type":"ListItem","@id":"https:\/\/2thenew.xyz\/blog\/category\/technology\/#listItem","name":"Technology"}}]},{"@type":"Organization","@id":"https:\/\/2thenew.xyz\/blog\/#organization","name":"TO THE NEW Blog","url":"https:\/\/2thenew.xyz\/blog\/"},{"@type":"Person","@id":"https:\/\/2thenew.xyz\/blog\/author\/abhayam-rastogi\/#author","url":"https:\/\/2thenew.xyz\/blog\/author\/abhayam-rastogi\/","name":"Abhayam Rastogi","image":{"@type":"ImageObject","@id":"https:\/\/2thenew.xyz\/blog\/common-utility-extensions-in-swift-utils-swift\/#authorImage","url":"https:\/\/secure.gravatar.com\/avatar\/a32e8bc2dc0fb2ba517ed7d802045c65d7977124ef9e2ee1a93c2508994a21d9?s=96&d=mm&r=g","width":96,"height":96,"caption":"Abhayam Rastogi"}},{"@type":"WebPage","@id":"https:\/\/2thenew.xyz\/blog\/common-utility-extensions-in-swift-utils-swift\/#webpage","url":"https:\/\/2thenew.xyz\/blog\/common-utility-extensions-in-swift-utils-swift\/","name":"Common Utility Extensions in Swift (Utils.swift) | TO THE NEW Blog","description":"What is Extension: Extensions are basically similar to an Objective-C category. Extensions are sort of methods adding in a Classe or Structure in a different flavour. Syntax of Extension: The syntax to create an extension is very similar to creating a type. You can write the following code in your Utils.swift class to add a","inLanguage":"en-US","isPartOf":{"@id":"https:\/\/2thenew.xyz\/blog\/#website"},"breadcrumb":{"@id":"https:\/\/2thenew.xyz\/blog\/common-utility-extensions-in-swift-utils-swift\/#breadcrumblist"},"author":{"@id":"https:\/\/2thenew.xyz\/blog\/author\/abhayam-rastogi\/#author"},"creator":{"@id":"https:\/\/2thenew.xyz\/blog\/author\/abhayam-rastogi\/#author"},"datePublished":"2015-08-11T22:12:31+05:30","dateModified":"2015-08-26T12:48:23+05:30"},{"@type":"WebSite","@id":"https:\/\/2thenew.xyz\/blog\/#website","url":"https:\/\/2thenew.xyz\/blog\/","name":"TO THE NEW Blog","inLanguage":"en-US","publisher":{"@id":"https:\/\/2thenew.xyz\/blog\/#organization"}}]},"og:locale":"en_US","og:site_name":"TO THE NEW BLOG","og:type":"blog","og:title":"Common Utility Extensions in Swift (Utils.swift) | TO THE NEW Blog","og:description":"What is Extension: Extensions are basically similar to an Objective-C category. Extensions are sort of methods adding in a Classe or Structure in a different flavour. Syntax of Extension: The syntax to create an extension is very similar to creating a type. You can write the following code in your Utils.swift class to add a","og:url":"https:\/\/2thenew.xyz\/blog\/common-utility-extensions-in-swift-utils-swift\/","og:image":"https:\/\/2thenew.xyz\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png","og:image:secure_url":"https:\/\/2thenew.xyz\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png","twitter:card":"summary","twitter:site":"@tothenew","twitter:title":"Common Utility Extensions in Swift (Utils.swift) | TO THE NEW Blog","twitter:description":"What is Extension: Extensions are basically similar to an Objective-C category. Extensions are sort of methods adding in a Classe or Structure in a different flavour. Syntax of Extension: The syntax to create an extension is very similar to creating a type. You can write the following code in your Utils.swift class to add a","twitter:image":"https:\/\/2thenew.xyz\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png"},"aioseo_meta_data":{"post_id":"25435","title":null,"description":null,"keywords":null,"keyphrases":null,"primary_term":null,"canonical_url":null,"og_title":"","og_description":"","og_object_type":"blog","og_image_type":"default","og_image_url":null,"og_image_width":null,"og_image_height":null,"og_image_custom_url":null,"og_image_custom_fields":null,"og_video":"","og_custom_url":null,"og_article_section":"","og_article_tags":"","twitter_use_og":false,"twitter_card":"summary","twitter_image_type":"default","twitter_image_url":null,"twitter_image_custom_url":null,"twitter_image_custom_fields":null,"twitter_title":null,"twitter_description":null,"schema":{"blockGraphs":[],"customGraphs":[],"default":{"data":{"Article":[],"Course":[],"Dataset":[],"FAQPage":[],"Movie":[],"Person":[],"Product":[],"ProductReview":[],"Car":[],"Recipe":[],"Service":[],"SoftwareApplication":[],"WebPage":[]},"graphName":"Article","isEnabled":true},"graphs":[]},"schema_type":null,"schema_type_options":null,"pillar_content":false,"robots_default":true,"robots_noindex":false,"robots_noarchive":false,"robots_nosnippet":false,"robots_nofollow":false,"robots_noimageindex":false,"robots_noodp":false,"robots_notranslate":false,"robots_max_snippet":null,"robots_max_videopreview":null,"robots_max_imagepreview":"large","priority":null,"frequency":null,"local_seo":null,"limit_modified_date":false,"created":"2021-04-29 18:37:52","updated":"2024-02-29 08:44:45","focus_keyword":null,"additional_keywords":null,"truseo_locale":null,"ai":null,"breadcrumb_settings":null,"seo_analyzer_scan_date":null},"aioseo_breadcrumb":"<div class=\"aioseo-breadcrumbs\"><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/2thenew.xyz\/blog\" title=\"Home\">Home<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">&raquo;<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/2thenew.xyz\/blog\/category\/technology\/\" title=\"Technology\">Technology<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">&raquo;<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\tCommon Utility Extensions in Swift (Utils.swift)\n\t\t<\/span><\/div>","aioseo_breadcrumb_json":[{"label":"Home","link":"https:\/\/2thenew.xyz\/blog"},{"label":"Technology","link":"https:\/\/2thenew.xyz\/blog\/category\/technology\/"},{"label":"Common Utility Extensions in Swift (Utils.swift)","link":"https:\/\/2thenew.xyz\/blog\/common-utility-extensions-in-swift-utils-swift\/"}],"_links":{"self":[{"href":"https:\/\/2thenew.xyz\/blog\/wp-json\/wp\/v2\/posts\/25435","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/2thenew.xyz\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/2thenew.xyz\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/2thenew.xyz\/blog\/wp-json\/wp\/v2\/users\/169"}],"replies":[{"embeddable":true,"href":"https:\/\/2thenew.xyz\/blog\/wp-json\/wp\/v2\/comments?post=25435"}],"version-history":[{"count":0,"href":"https:\/\/2thenew.xyz\/blog\/wp-json\/wp\/v2\/posts\/25435\/revisions"}],"wp:attachment":[{"href":"https:\/\/2thenew.xyz\/blog\/wp-json\/wp\/v2\/media?parent=25435"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/2thenew.xyz\/blog\/wp-json\/wp\/v2\/categories?post=25435"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/2thenew.xyz\/blog\/wp-json\/wp\/v2\/tags?post=25435"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}