Skip to content

Instantly share code, notes, and snippets.

Rob Ryan robertmryan

Block or report user

Report or block robertmryan

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
View Iso8601.swift
// Personally, I would have done this on `DateFormatter` extension, but
// did it on Formatter so it would work with provided code snippet.
extension Formatter {
static var iso8601: DateFormatter = {
let formatter = DateFormatter()
formatter.locale = Locale(identifier: "en_US_POSIX")
formatter.timeZone = TimeZone(secondsFromGMT: 0)
formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSSSS"
return formatter
View RemoveAnnotations.swift
func removeAllAnnotations() {
guard let document = document else { return }
for i in 0..<document.pageCount {
if let page = document.page(at: i) {
while page.annotations.count > 0 {
page.removeAnnotation(page.annotations.last!)
}
}
}
View OSLogExample.swift
// for example, for debugging messages in `ViewController.swift`
#if DEBUG
private let log = OSLog(subsystem: Bundle.main.bundleIdentifier!, category: "ViewController")
#else
private let log = OSLog.disabled
#endif
// and then:
//
View AlamofireRequest.swift
struct Industry: Encodable {
let industryTypeCode: String
}
struct Company: Encodable {
let companyName: String
let cinNumber: String
let isListed: Bool
let industryType: [Industry]
}
View SqliteExample.swift
func retrieveArticles(searchText: String) {
precondition(db != nil, "Database not open")
articles.removeAll()
let query = "SELECT * FROM articles WHERE title = ? "
var statement: OpaquePointer?
guard sqlite3_prepare(db, query, -1, &statement, nil) == SQLITE_OK else {
let errmsg = String(cString: sqlite3_errmsg(db)!)
View ParseJSON.swift
func parse(message: String) -> String {
let json = JSON(parseJSON: message)
return json["Request"]["transactionId"].stringValue
}
let input = """
{"Request": {"content": {"Reset": {}}, "transactionId": "f7c4d630-552b-46d9-a37d-44450537b48d"}}
"""
let transactionID = parse(message: input)
View AccelerateAverageColor.swift
import Accelerate
extension UIImage {
var averageColor: UIColor? {
guard let inputImage = CIImage(image: self) else { return nil }
let extentVector = CIVector(x: inputImage.extent.origin.x, y: inputImage.extent.origin.y, z: inputImage.extent.size.width, w: inputImage.extent.size.height)
guard let filter = CIFilter(name: "CIAreaAverage", parameters: [kCIInputImageKey: inputImage, kCIInputExtentKey: extentVector]) else { return nil }
guard let outputImage = filter.outputImage else { return nil }
View CombineCollect.swift
func getAllData() -> AnyPublisher<[MyData], Error> {
getIDs().flatMap { ids -> AnyPublisher<[MyData], Error> in
let publishers = ids.map { self.getData(with: $0) }
return Publishers.Sequence<[AnyPublisher<MyData, Error>], Error>(sequence: publishers)
.flatMap { $0 }
.collect()
.eraseToAnyPublisher()
}.eraseToAnyPublisher()
}
View AsynchronousExperiment.swift
import os.log
func asynchronousExperiment() {
func asynchronousOperation(index: Int, completion: @escaping () -> ()) {
os_log("STARTING %d", index)
DispatchQueue.main.asyncAfter(deadline: .now() + 5) {
os_log("FINISHED %d", index)
completion()
}
}
View PromisesInParallel.swift
firstly {
when(fulfilled: [foo(), bar()])
}.done { results in
print(results)
}.catch { error in
print(error)
}
You can’t perform that action at this time.