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 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)
}
View Sample.swift
class ViewController: NSViewController {
@IBOutlet var threadAValueLabel: NSTextField!
@IBOutlet var threadBValueLabel: NSTextField!
var threadAGoNoGo = 0
var threadBGoNoGo = 0
override func viewDidLoad() {
super.viewDidLoad()
View Share.swift
@IBAction func saveImage(_ sender: Any) {
contextImageView.image = UIImage(contentsOfFile: Bundle.main.path(forResource: "noimage", ofType: "png")!)!
UIGraphicsBeginImageContextWithOptions(contextImageView.bounds.size, false, 0)
imageView1.image?.draw(at: .zero)
imageView2.image?.draw(at: .zero)
let imageToShare = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext()
PHPhotoLibrary.requestAuthorization { _ in }
View ButtonEnabling.swift
class ViewController: UIViewController {
@IBOutlet weak var textOTP1: UITextField!
@IBOutlet weak var textOTP2: UITextField!
@IBOutlet weak var textOTP3: UITextField!
@IBOutlet weak var textOTP4: UITextField!
@IBOutlet weak var textOTP5: UITextField!
@IBOutlet weak var textOTP6: UITextField!
@IBOutlet weak var verifyButton: UIButton!
View Not32BitAligned.swift
class WithUnsafeBytesDemo {
func unalignedExample() {
// deliberately throw in extra byte at the start
let originalData = Data([0xff, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x04])
print("original base address:", originalData.withUnsafeBytes { $0.baseAddress } ?? "Unknown address")
print("original start index: ", originalData.startIndex) // is 0
print("original binary data: ", originalData as NSData)
print("")
// get the data after that extra byte
You can’t perform that action at this time.