Skip to content

Instantly share code, notes, and snippets.

View mohsinbmwm3's full-sized avatar
🏠
Working from home

Mohsin Khan mohsinbmwm3

🏠
Working from home
View GitHub Profile
@mohsinbmwm3
mohsinbmwm3 / StringExtension.swift
Last active January 18, 2021 14:03
Useful String extensions in swift
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)
}
@mohsinbmwm3
mohsinbmwm3 / DateExtensions.swift
Last active January 18, 2021 14:28
Some useful date extension ideas I have in my mind. Please take a look.
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)
@mohsinbmwm3
mohsinbmwm3 / FontExtension.swift
Created January 18, 2021 14:35
Some custom font swift extension ideas.
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 {
@mohsinbmwm3
mohsinbmwm3 / ArrayExtension.swift
Created January 18, 2021 14:45
Array swift extensions.
import Foundation
extension Array {
var isNotEmpty: Bool {
return !isEmpty
}
}
@mohsinbmwm3
mohsinbmwm3 / UIAlertControllerExtensions.swift
Created January 18, 2021 14:46
Some useful UIAlertController extension in swift
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)
@mohsinbmwm3
mohsinbmwm3 / UIImageExtensions.swift
Created January 18, 2021 14:51
Some UIImage extensions
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!
}
@mohsinbmwm3
mohsinbmwm3 / BundleExtension.swift
Created January 18, 2021 14:55
Bundle extensions in swift
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? {
@mohsinbmwm3
mohsinbmwm3 / UIApplicationExtension.swift
Created January 18, 2021 15:03
UIApplication extensions
import UIKit
extension UIApplication {
func openSetting() {
if let _url = URL(string: UIApplication.openSettingsURLString), canOpenURL(_url) {
open(_url, options: [:], completionHandler: nil)
}
}
func clearNotificationBadge() {
applicationIconBadgeNumber = 0
@mohsinbmwm3
mohsinbmwm3 / NSNotificationExtensions.swift
Created January 18, 2021 15:06
NSNotification extension idea.
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") }
}
@mohsinbmwm3
mohsinbmwm3 / UIViewExtension.swift
Created January 18, 2021 15:17
Some very useful UIView extension ideas.
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