Skip to content

Instantly share code, notes, and snippets.

@robertmryan
robertmryan / Database.swift
Created Apr 14, 2019
Very minimalist SQLite wrapper for Swift
View Database.swift
//
// Database.swift
//
// Created by Robert Ryan on 3/8/19.
// Copyright © 2019 Robert Ryan. All rights reserved.
//
import Foundation
import SQLite3
View Factorial.c
// consider
void process(long long * array, int arraySizeInBytes) {
long long value = 1;
for (long long i = 0; i < arraySizeInBytes / sizeof(long long); i++) {
value *= i + 1;
NSLog(@"%lld", array[i]);
array[i] = value;
}
View TimerDemo.m
@interface ViewController ()
@property (nonatomic, weak) NSTimer *minutePassed;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.minutePassed = [NSTimer scheduledTimerWithTimeInterval:60 target:self selector:@selector(handleTimer:) userInfo:nil repeats:NO];
View OpenMPExperiment.c
void matrixProduct3(int threads) {
int i, j, k, aik;
omp_set_num_threads(threads);
kdebug_signpost_start(3, threads, 0, 0, 3);
double start = omp_get_wtime();
memset(c, n * n, sizeof(int));
View UploadVideo.swift
import UIKit
import MobileCoreServices
class ViewController: UIViewController {
let networkManager = NetworkManager()
@IBAction func didTapButton(_ sender: Any) {
let controller = UIImagePickerController()
controller.mediaTypes = [kUTTypeMovie as String]
controller.sourceType = .photoLibrary
View CurvedIndentedView.swift
@IBDesignable
class CurveIndentedView: UIView {
@IBInspectable
var radius: CGFloat = 50 { didSet { updatePath() } }
private var shapeLayer: CAShapeLayer = {
let shapeLayer = CAShapeLayer()
shapeLayer.fillColor = UIColor.red.cgColor
return shapeLayer
View Knight.swift
// if Knight is just initializing the properties of its super class, you can omit
// the init method entirely, and it will inherit it from its superclass
class Knight: Unit {
// override init(pHealthPoints: Int, pDamage: Int, pMovement: Int) {
// self.healthPoints = pHealthPoints
// self.damage = pDamage
// self.movement = pMovement
// }
View Download.swift
// method with closure that throws
func download(completion: @escaping (URL?) throws -> Void) {
let url = ...
URLSession.shared.downloadTask(with: url) { location, _, _ in
do {
try completion(location)
} catch let fileSavingError {
// Perhaps you're going to do some complicated error handling process that logs the problem or whatever.
// In this case, I'll just print the error that the `completion` closure threw.
View Fetch.m
- (void)verifyCode:(NSString*)code {
NSString *identifier = self.potentialPerson[@"ID"];
NSString *urlString = [[inputModel api] stringByAppendingFormat:@"action=validate&code=%@&inputID=%@",
code, identifier];
[self validateInputFrom:urlString completionHandler:^(NSDictionary *dictionary) {
self.codeResponse = dictionary;
NSLog(@"message %@", dictionary);
// use `dictionary` here, but ...
View FileDownloader.swift
//
// FileDownloader.swift
//
// Created by Robert Ryan on 1/30/19.
//
import Foundation
import os.log
private let log = OSLog(subsystem: Bundle.main.bundleIdentifier!, category: "FileDownloader")
You can’t perform that action at this time.