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
class ImageLoader { | |
private let cache = NSCache<NSString, NSData>() | |
class func image(for url: URL, completionHandler: @escaping(_ image: UIImage?) -> ()) { | |
DispatchQueue.global(qos: DispatchQoS.QoSClass.background).async { | |
if let data = self.cache.object(forKey: url.absoluteString as NSString) { | |
DispatchQueue.main.async { completionHandler(UIImage(data: data as Data)) } |
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
func fetchContentLength(for url: URL, completionHandler: @escaping (_ contentLength: UInt64?) -> ()) { | |
var request = URLRequest(url: url) | |
request.httpMethod = "HEAD" | |
let task = URLSession.shared.dataTask(with: request) { (data, response, error) in | |
guard error == nil, | |
let response = response as? HTTPURLResponse, | |
let contentLength = response.allHeaderFields["Content-Length"] as? String else { | |
completionHandler(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
func isRelative(url: URL) -> Bool { | |
guard let regex = try? NSRegularExpression(pattern: "^(?:[a-z]+:)?//", options: .caseInsensitive) else { return false } | |
return regex.firstMatch(in: url.absoluteString, options: [], range: NSRange(location: 0, length: url.absoluteString.count)) == nil | |
} | |
let url = URL(string: "https://s3.amazonaws.com/x265.org/video/Tears_400_x265.mp4")! | |
isRelative(url: url) // false |
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
struct SwitchToMainThread { | |
static func with(_ block: @escaping (() -> ())) { | |
guard !Thread.isMainThread else { block(); return } | |
DispatchQueue.main.async { SwitchToMainThread.with(block) } | |
} | |
} | |
SwitchToMainThread.with { | |
// Do UI stuff | |
} |
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
public struct Units { | |
public let bytes: Int64 | |
public var kilobytes: Double { | |
return Double(bytes) / 1_024 | |
} | |
public var megabytes: Double { | |
return kilobytes / 1_024 |
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 UIViewController { | |
func embed(_ child: UIViewController, animated: Bool = false) { | |
let duration = animated ? 0.3 : 0.0 | |
child.view.alpha = 0 | |
child.willMove(toParent: self) | |
self.addChild(child) | |
self.view.addSubview(child.view) | |
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
@interface myViewController: UIViewController <CLLocationManagerDelegate> | |
CLLocationManager *locationManager; | |
CLLocation *currentLocation; | |
@end |
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
<?xml version="1.0" encoding="UTF-8"?> | |
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> | |
<plist version="1.0"> | |
<dict> | |
<key>items</key> | |
<array> | |
<dict> | |
<key>assets</key> | |
<array> | |
<dict> |
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
/* | |
Distributed under The MIT License: | |
http://opensource.org/licenses/mit-license.php | |
Permission is hereby granted, free of charge, to any person obtaining | |
a copy of this software and associated documentation files (the | |
"Software"), to deal in the Software without restriction, including | |
without limitation the rights to use, copy, modify, merge, publish, | |
distribute, sublicense, and/or sell copies of the Software, and to |
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
/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --allow-file-access-from-files |