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 { | |
// MARK: 计算字符串的 MD5 哈希值 | |
var md5: String? { | |
let length = Int(CC_MD5_DIGEST_LENGTH) | |
guard let data = self.data(using: String.Encoding.utf8) else { return nil } | |
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 { | |
init?(json: Any) { | |
guard let data = Data(json: json) else { return nil } | |
self.init(decoding: data, as: UTF8.self) | |
} | |
func jsonToDictionary() -> [String: Any]? { | |
self.data(using: .utf8)?.jsonToDictionary() |
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
#if os(iOS) || os(tvOS) | |
import UIKit | |
typealias UniColor = UIColor | |
#else | |
import Cocoa | |
typealias UniColor = NSColor | |
#endif | |
private extension Int { | |
func duplicate4bits() -> Int { |
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 URL { | |
func withQueries(_ queries: [String: String]) -> URL? { | |
var components = URLComponents(url: self, resolvingAgainstBaseURL: true) | |
components?.queryItems = queries.compactMap { URLQueryItem(name: $0.0, value: $0.1) } | |
return components?.url | |
} | |
} |
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
extension UIImage { | |
class func imageWithColor(color: UIColor) -> UIImage { | |
let rect: CGRect = CGRect(x: 0, y: 0, width: 1, height: 1) | |
UIGraphicsBeginImageContextWithOptions(CGSize(width: 1, height: 1), false, 0) | |
color.setFill() | |
UIRectFill(rect) | |
let image: UIImage? = UIGraphicsGetImageFromCurrentImageContext() | |
UIGraphicsEndImageContext() | |
return image! |
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
extension UIColor { | |
func image(_ size: CGSize = CGSize(width: 1, height: 1)) -> UIImage { | |
return UIGraphicsImageRenderer(size: size).image { (rendererContext) in | |
self.setFill() | |
rendererContext.fill(CGRect(origin: .zero, size: size)) | |
} | |
} | |
} |