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 <CommonCrypto/CommonCrypto.h> |
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 md5: String? { | |
let length = Int(CC_MD5_DIGEST_LENGTH) | |
guard let data = self.data(using: String.Encoding.utf8) else { return nil } | |
let hash = data.withUnsafeBytes { (bytes: UnsafeRawBufferPointer) -> [UInt8] in | |
var hash: [UInt8] = [UInt8](repeating: 0, count: Int(CC_MD5_DIGEST_LENGTH)) |
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 localized: String { | |
NSLocalizedString(self, comment: "") | |
} | |
} |
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 { | |
subscript (i: Int) -> Character { | |
return self[index(startIndex, offsetBy: i)] | |
} | |
subscript (bounds: CountableRange<Int>) -> Substring { | |
let start = index(startIndex, offsetBy: bounds.lowerBound) | |
let end = index(startIndex, offsetBy: bounds.upperBound) |
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 containsOnlyDigits: Bool { | |
let notDigits = NSCharacterSet.decimalDigits.inverted | |
return rangeOfCharacter(from: notDigits, options: String.CompareOptions.literal, range: nil) == nil | |
} | |
var containsOnlyLetters: Bool { | |
let notLetters = NSCharacterSet.letters.inverted |
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 isValidEmail: Bool { | |
let emailRegEx = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,64}" | |
let emailTest = NSPredicate(format: "SELF MATCHES %@", emailRegEx) | |
return emailTest.evaluate(with: 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 Int { | |
init?(key: String) { | |
guard UserDefaults.standard.value(forKey: key) != nil else { return nil } | |
self.init(UserDefaults.standard.integer(forKey: key)) | |
} | |
func store(key: String) { | |
UserDefaults.standard.set(self, forKey: key) |
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 Data { | |
init?(json: Any) { | |
guard let data = try? JSONSerialization.data(withJSONObject: json, options: .fragmentsAllowed) else { return nil } | |
self.init(data) | |
} | |
func jsonToDictionary() -> [String: Any]? { | |
(try? JSONSerialization.jsonObject(with: self, options: .allowFragments)) as? [String: Any] |
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 { | |
func toInt() -> Int { | |
Int(self)! | |
} | |
func toIntOrNull() -> Int? { | |
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
#if os(iOS) || os(tvOS) | |
import UIKit | |
typealias UniColor = UIColor | |
#else | |
import Cocoa | |
typealias UniColor = NSColor | |
#endif | |
private extension Int { | |
func duplicate4bits() -> Int { |
OlderNewer