This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
extension String { | |
var wordCount: Int { | |
let regex = try? NSRegularExpression(pattern: "\\w+") | |
return regex?.numberOfMatches(in: self, range: NSRange(location: 0, length: self.utf16.count)) ?? 0 | |
} | |
func toInt() -> Int? { | |
return Int(self) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
extension DateFormatter { | |
static let instance = DateFormatter() | |
// You can also add multiple date formatters, creating date formater is very heavy task, | |
// It is not advisable to create instance of it every time you want to use it. | |
} | |
extension Calendar { | |
// Shorthands for different calendar | |
static let gregorianCalendar = Calendar(identifier: .gregorian) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
// Its better to create custom font name as constant to avoid spelling mistakes when using this. | |
extension String { | |
static let appCustomFontRegular = "Montserrat" | |
static let appCustomFontBold = "Montserrat-Bold" | |
} | |
extension UIFont { | |
static func appRegularFont(size: CGFloat) -> UIFont { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
extension Array { | |
var isNotEmpty: Bool { | |
return !isEmpty | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
extension UIAlertController { | |
static func showSimpleAlert(title: String, message: String, context: UIViewController) { | |
let alert = UIAlertController(title: title, message: message, preferredStyle: .alert) | |
alert.addAction(UIAlertAction(title: "Ok", style: .default, handler: nil)) | |
context.present(alert, animated: true, completion: nil) | |
} | |
static func showSimpleAlert(title: String, message: String, okButtonTitle: String, okButtonAction: (@escaping () -> ()), context: UIViewController) { | |
let alert = UIAlertController(title: title, message: message, preferredStyle: .alert) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
extension UIImage { | |
func resize(height: CGFloat, width: CGFloat) -> UIImage { | |
UIGraphicsBeginImageContext(CGSize(width: width, height: height)) | |
draw(in: CGRect(x: 0, y: 0, width: width, height: height)) | |
let resizedImage = UIGraphicsGetImageFromCurrentImageContext() | |
UIGraphicsEndImageContext() | |
return resizedImage! | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
extension Bundle { | |
var appName: String? { | |
return Bundle.main.infoDictionary?["CFBundleName"] as? String | |
} | |
var appVersion: String? { | |
return Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String | |
} | |
var bundleVersion: String? { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
extension UIApplication { | |
func openSetting() { | |
if let _url = URL(string: UIApplication.openSettingsURLString), canOpenURL(_url) { | |
open(_url, options: [:], completionHandler: nil) | |
} | |
} | |
func clearNotificationBadge() { | |
applicationIconBadgeNumber = 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
extension NSNotification.Name { | |
static var userLoggedInSuccessfully: NSNotification.Name { NSNotification.Name(rawValue: "userLoggedInSuccessfully") } | |
static var userLoggedOutSuccessfully: NSNotification.Name { NSNotification.Name(rawValue: "userLoggedOutSuccessfully") } | |
static var appEnteredInForeground: NSNotification.Name { NSNotification.Name(rawValue: "appEnteredInForeground") } | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
extension UIView { | |
func addConstraintToLayoutSubviewInCenter(_ subview: UIView, size: CGSize? = nil) { | |
subview.translatesAutoresizingMaskIntoConstraints = false | |
subview.centerXAnchor.constraint(equalTo: centerXAnchor, constant: 0).isActive = true | |
subview.centerYAnchor.constraint(equalTo: centerYAnchor, constant: 0).isActive = true | |
if let _size = size { | |
subview.widthAnchor.constraint(equalToConstant: _size.width).isActive = true | |
subview.heightAnchor.constraint(equalToConstant: _size.height).isActive = true |