{"id":18416703,"url":"https://github.com/infyomlabs/swiftextension","last_synced_at":"2025-06-10T21:37:15.408Z","repository":{"id":56947436,"uuid":"55407828","full_name":"InfyOmLabs/SwiftExtension","owner":"InfyOmLabs","description":null,"archived":false,"fork":false,"pushed_at":"2020-11-11T12:19:58.000Z","size":71,"stargazers_count":25,"open_issues_count":0,"forks_count":12,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-06-01T05:01:07.311Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/InfyOmLabs.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-04-04T12:02:05.000Z","updated_at":"2023-07-13T08:45:05.000Z","dependencies_parsed_at":"2022-08-21T08:20:45.292Z","dependency_job_id":null,"html_url":"https://github.com/InfyOmLabs/SwiftExtension","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/InfyOmLabs%2FSwiftExtension","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/InfyOmLabs%2FSwiftExtension/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/InfyOmLabs%2FSwiftExtension/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/InfyOmLabs%2FSwiftExtension/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/InfyOmLabs","download_url":"https://codeload.github.com/InfyOmLabs/SwiftExtension/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/InfyOmLabs%2FSwiftExtension/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":259155321,"owners_count":22813787,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-06T04:06:57.189Z","updated_at":"2025-06-10T21:37:15.378Z","avatar_url":"https://github.com/InfyOmLabs.png","language":"Swift","funding_links":[],"categories":[],"sub_categories":[],"readme":"# SwiftExtension\n\n[![CI Status](http://img.shields.io/travis/dhvl-golakiya/SwiftExtension.svg?style=flat)](https://travis-ci.org/dhvl-golakiya/SwiftExtension)\n[![Version](https://img.shields.io/cocoapods/v/SwiftExtension.svg?style=flat)](http://cocoapods.org/pods/SwiftExtension)\n[![License](https://img.shields.io/cocoapods/l/SwiftExtension.svg?style=flat)](http://cocoapods.org/pods/SwiftExtension)\n[![Platform](https://img.shields.io/cocoapods/p/SwiftExtension.svg?style=flat)](http://cocoapods.org/pods/SwiftExtension)\n\nSimple Swift extensions for classes which are mostly used in iOS apps.\n\n## Requirements\nRequired Swift 4.0 or later\n\n## Installation\n\n### Install Manually\nDownload and drop Classes folder to your project\n\n### Install via CocoaPods\nSwiftExtension is available through [CocoaPods](http://cocoapods.org). To install\nit, simply add the following line to your Podfile:\n\n```ruby\npod \"SwiftExtension\"\n```\nImport `SwiftExtension` to your class\n\n``` swift\nimport UIKit\nimport SwiftExtension\n```\n\n##Array extension\n\nMake Comma separated String from array\n``` swift\nvar myArray = [\"New York\", \"London\", \"Paris\"]\nmyArray.toCommaString // New York,London,Paris\n```\n\nRemove Specific object from Array\n``` swift\nvar myArray = [\"New York\", \"London\", \"Paris\"]\nmyArray.removeObject(\"London\") // [\"New York\", \"Paris\"]\n```\n\nChack Array contain specific object\n``` swift\nvar myArray = [\"New York\", \"London\", \"Paris\"]\nmyArray.containsObject(\"London\") // true\nmyArray.containsObject(\"Seattle\") // false\n```\n\nGet Index of specific object\n``` swift\nvar myArray = [\"New York\", \"London\", \"Paris\"]\nmyArray.indexOfObject(\"Paris\") // 2\n```\n\nGets the object at the specified index, if it exists.\n``` swift\nvar myArray = [\"New York\", \"London\", \"Paris\"]\nmyArray.get(1) // London\n```\n\n## Int Extension\n``` swift\nlet number = 10\nprint(number.isOdd) // false\nprint(number.isEven) // true\nprint(number.isNegative) // false\nprint(number.isPositive) // true\nprint(number.toUInt) // 10\nprint(number.toFloat)// 10.0\nprint(number.toInt64) // 10\nprint(number.toDouble) // 10.0\nprint(number.toString) // 10\nprint(number.toCGFloat) // 10.0\nprint(number.digit()) // [1, 0]\nprint(number.digitCount) // 2\nprint(number.factorial()) // 3628800\n```\n\n## NSData Extension\n\n``` swift\nlet data = NSData()\ndata.toDictionary // Convert NSData to NSDictionary\ndata.toJsonString // Convert NSData to NSString\n```\n\n## NSMutableData Extension\n``` swift\nlet data = NSMutableData()\ndata.appendString(\"String\") // Convert String to NSdata then append to NSMutableData\n```\n\n## NSDate Extension\n``` swift\nlet date = Date()\nlet afterDate = Date(timeIntervalSinceNow: 253333)\nprint(date.weekDay) // 2 : Get Week day from date\nprint(date.weekOfMonth)// : Get Week index of month from date\nprint(date.weekDayName) // Monday : Get Week day name from date\nprint(date.monthName) // April  : Get Month name from date\nprint(date.month) // 4  : Get Month index from date\nprint(date.day) // 4  : Get Day index from date\nprint(date.year) // 2016  : Get Year index from date\nprint(date.getHourAndMinute()) // (19, 43)  : Get Hour and Minute from date\nprint(date.weekDaysInMonth()) // 4  : Get Total count of weeks in month from date\nprint(date.weeksInMonth()) // 4 : Get Total count of week days in month from date\nprint(date.daysInMonth()) // 30 : Get Total count of days in month from date\nprint(date.getTime()) // 07:43 PM : Get Time in AM / PM format\nprint(date.getTimeInShortFormat()) // 04 Apr  : Get Time short (i.e 12 Mar) format\nprint(date.getTimeInFullFormat()) // 04 Apr, 2016 :Get Time short (i.e 12 Mar, 2016) format\nprint(date.formateBirthDate()) // 2016-04-04  : Get Time standard (i.e 2016-03-12) format\nprint(date.afterDate(afterDate)) // true  : Check date is after date\nprint(date.beforDate(afterDate)) // false : Check date is before date\nprint(date.equalDate(afterDate)) // false : Check date is equal date\nprint(date.daysInBetweenDate(afterDate)) // 2 : Get days difference between dates\nprint(date.hoursInBetweenDate(afterDate)) // 70 : Get hours difference between dates\nprint(date.minutesInBetweenDate(afterDate)) // 4222 : Get minutes difference between dates\nprint(date.secondsInBetweenDate(afterDate)) // 253333 : Get seconds difference between dates\nprint(afterDate.getDifferenceBetweenDates()) // 2 days : 22h : 22m : 22s : Get time difference between date from today\n```\n## NSDictionary Extension\n``` swift\nlet dictionary = NSDictionary(objects: [\"value1\", \"value2\"], forKeys: [\"key\", \"secondKey\"])\nprint(dictionary.has(\"key\"))  //  true\nprint(dictionary.has(\"firstKey\")) //  false\ndictionary.toNSData // Convert NSDictionary to NSDate\n ```\n \n## String Extension\n\nGeneral extensions\n``` swift\nlet testString = \"Simple Swift extensions ,for classes which are mostly used in iOS apps\"\nprint(testString.trim())  // SimpleSwiftextensions,forclasseswhicharemostlyusediniOSapps\nprint(testString.length)  //  70\nprint(testString.makeArray) //  [\"Simple Swift extensions \",\"for classes which are mostly used in iOS apps\" ]\nprint(testString.makeArrayByWhiteSpace) //  [ Simple, Swift, extensions,\",for\", classes, which, are, mostly, used, in, iOS, apps ]\nprint(testString[5])  //  e\nprint(testString[5 ..\u003c 15]) // e Swift ex\nprint(testString.stringSizeWith(UIFont.systemFontOfSize(16), width: 200, lineSpecing: 5)) //  (0.0, 0.0, 183.0390625, 91.375)\nprint(testString.stringWidth(UIFont.systemFontOfSize(16)))  //  510.4140625\nprint(testString.removeCharsFromEnd(15))  //  Simple Swift extensions ,for classes which are mostly u\nprint(testString.contains(\"extensions\"))  //  true\nprint(testString.contains(\"Extensions\"))  //  false\nprint(testString.containsIgnoreCase(\"extensions\"))  //  true  \n\nlet testStringNew = \"Simple Swift extensions for classes \\nwhich are mostly used in iOS apps\"\nprint(testStringNew.trimForNewLineCharacterSet()) //  Simple Swift extensions for classes  which are mostly used in iOS apps\n```\n\nGet Youtube video Id from Youtube url\n``` swift\nlet youtubeUrl = \"https://www.youtube.com/watch?v=0wIiDnjz4X4\"\nprint(youtubeUrl.getYoutubeID())  //  0wIiDnjz4X4\n```\n\nGet Phone number string from Phone numner\n``` swift\nlet phoneNumberStriing = \"+91 22222-22222\"\nprint(phoneNumberStriing.trimPhoneNumberString()) //  912222222222\n```\n\nString to Date\n``` swift\nlet dateString = \"1995-12-15\"\nprint(dateString.getDate()) //  1995-12-14 18:30:00 +0000\n\nlet dateAndTimeString = \"2016-04-05 12:12:10\"\nprint(dateAndTimeString.getDateAndTime()) //  2016-04-05 06:42:10 +0000\n```\nString to NSDictionary and NSDate\n``` swift\nlet jsonString\nprint(jsonString.toDictionary)  //  Convert String to NSDicationry\nprint(jsonString.toNSData())    //  Convert String to NSdate\n```\nCheck string contain Regex (Regular expresion)\n``` swift\nlet imageUrl = http://www.testing.com/uploads/images/__w-200-400-600-800-1000__/9fbaa5bc4e032fb528f3f41997f660e7.jpg\nprint(imageUrl.matchesForRegexInText(\"__w-((?:-?\\\\d+)+)__\")) // Get only image size string array [\"__w-200-400-600-800-1000__\"]\n```\nPath Component and Extensions\n``` swift\nlet pathString = \"file.png\" //  \nprint(pathString.lastPathComponent) //  file.png\nprint(pathString.pathExtension) //  png\nprint(pathString.stringByDeletingPathExtension) //  file\nprint(pathString.stringByDeletingLastPathComponent) //  \"\"\nprint(pathString.pathComponents)  //  [\"file.png\"]\nprint(pathString.stringByAppendingPathComponent(\"jpeg\"))  //  file.png/jpeg\nprint(pathString.stringByAppendingPathExtension(\"files\")) //  file.png.files\nprint(pathString.insertSubString(2, \"new testing\")) //  finew testingle.png\n```\n\nString to number\n``` swift\nlet numberString = \"44\"\nprint(numberString.toDouble())  //  44.0\nprint(numberString.toFloat()) //  44.0\nprint(numberString.toUInt())  //  44\nprint(numberString.toBool())  //  true\n```\n\nEmail validation\n``` swift\nlet emailString = \"dhvl.golakiya@gmail.com\"\nlet emailStringNew = \"dhvl@golakiya@gmail.com\"\nprint(emailString.isEmail)  //  true\nprint(emailStringNew.isEmail) //  false\n```\n\nURL validation\n``` swift\nlet urlString = \"www.google.com\"\nlet urlStriongNew = \"www.\"\nprint(urlString.isValideUrl)  //  true\nprint(urlStriongNew.isValideUrl)  //  false\n```\n\n## UIButton Extension\n``` swift\nlet button = UIButton()\nbutton.applyCornerRadius(true)  //  Make button round\nbutton.setBackgroundColor(UIColor.greenColor(), forState: .Selected)  //  Set background color green for selected state\nbutton.textForAllState(\"Done\")  //  Set title label text \"Done\" for all state\nbutton.textForNormal(\"Done\")  //  Set title label text \"Done\" for normal state\nbutton.textForSelected(\"Done\")  //  Set title label text \"Done\" for selected state\nbutton.textForHighlighted(\"Done\")  //  Set title label text \"Done\" for highlighted state\nbutton.imageForAllState(UIImage(named: \"done\")!)  //  Set \"done\" image for all state\nbutton.imageForNormal(UIImage(named: \"done\")!)  //  Set \"done\" image for normal state\nbutton.imageForSelected(UIImage(named: \"done\")!)  //  Set \"done\" image for selected state\nbutton.imageForHighlighted(UIImage(named: \"done\")!) //  Set \"done\" image for highlighted state\nbutton.colorOfTitleLabelForAllState(UIColor.grayColor()) //  Set title label color grey for all state\nbutton.colorOfTitleLabelForNormal(UIColor.grayColor()) //  Set title label color grey for normal state\nbutton.colorOfTitleLabelForSelected(UIColor.grayColor()) // Set title label color grey for selected state\nbutton.colorOfTitleLabelForHighlighted(UIColor.grayColor()) //  Set title label color grey for highlighted state\nbutton.setImageBehindTextWithCenterAlignment(15, buttonWidth: 100, space: 2)  //  Set image behind text in Center alignment\nbutton.setImageOnRightAndTitleOnLeft(15, buttonWidth: 100)  //  Set image right side and text on left side\nbutton.setImageBehindTextWithLeftAlignment(15, buttonWidth: 100)  //  Set image behind text in left alignment\n```\n\n## UIColor Extension\n``` swift\nUIColor(hex: 222222, alpha: 1.0)  // Return UIColor from hext\nUIColor(hexString: \"222222\")  //  Return UIcolor from hex string\nUIColor(hexString: \"222222\", alpha: 1.0)  //  Return UIColor from hext string with alpha value\n```\n\n## UIFont Extension\n``` swift\nUIFont(fontname: \"Custom Font\", fontSize: 16) //  Return Custom font with font size\n```\n\n## UIImage Extension\n``` swift\nlet image = UIImage(named : \"test\")\nimage.colorizeImage(UIColor.blackColor) // Return black color image from this image\nimage.tintedImage() //  Return tinted image from this image\nimage.croppedImage()  //  Return cropped image \nimage.rotateImageToNighntyDegree() // Return 90 degree rotated image\n```\n\n## UILabel Extension\n``` swift\nlet label = UILabel()\nlabel.getEstimatedSize(320, height: 100)  //  Return estimated size of label\nlabel.getEstimatedHeight()  //  Return estimated height of label\nlabel.getEstimatedWidth() //  Return estimated width of label\nlabel.fitHeight() //  Fit label height\nlabel.fitWidth()  //  Fit label width\nlabel.fitSize() //  Fit label size\nlabel.getLinesArrayOfString() // Get line text in string array\nlabel.getLinesArrayOfString(2)  //  Get nth line text\n```\n\n## UIViewController\nUsed for tracking screen for app analysis using `event_viewDidLoad` method which screen shown\n``` swift\nself.className() // Viewcontroller class name\n```\n\n## UIView Extension\n``` swift\nlet view = UIView()\nprint(view.endX)  //  Get view.frame.size.width + view.frame.origin.x\nprint(view.endY)  //  Get view.frame.size.height + view.frame.origin.y\nprint(view.startX) //  Get view.frame.origin.x\nprint(view.startY)  //  Get view.frame.origin.y\nprint(view.getWidth)  // Get view's width\nprint(view.getHeight) //Get view's height\nview.setStartX(10)  //  Set view origin.x to 10\nview.setStartY(100) //  Set view origin.y to 100\nview.setWidth(150)  //  Set view width 150\nview.setHeight(200) //  Set view height 200\nview.setCenter(75, y: 75) //  Set vew center to (75,75)\nprint(view.getCenter()) //  Get view center\nview.setCenterX(75) //  Set view center.x to 75\nprint(view.getCenterX())  //  Get view center.x\nview.setCenterY(75) //  Set view center.y to 75\nprint(view.getCenterY())  //  Get view center.y\nprint(view.parentViewController) // Get view's parent view controller\nprint(view.applyPlainShadow()) // Apply plain shadow\nprint(view.applyBorder()) //  Apply border\nview.applyCornerRadius(10, mask: true)  //  Apply corner radius with mask endble / disable\nview.addTopBorderWithColor(UIColor.redColor(), width: 2)  //  Add top border\nview.addRightBorderWithColor(UIColor.redColor(), width: 2)  //  Add right border\nview.addBottomBorderWithColor(UIColor.redColor(), width: 2) //  Add bottom border\nview.addLeftBorderWithColor(UIColor.redColor(), width: 2) //  Add left border\n```\n\n## Author\ndhvl-golakiya, dhvl.golakiya@gmail.com\n\n## License\n\nSwiftExtension is available under the MIT license. See the LICENSE file for more info.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Finfyomlabs%2Fswiftextension","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Finfyomlabs%2Fswiftextension","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Finfyomlabs%2Fswiftextension/lists"}