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 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
@robertmryan
robertmryan / .swiftlint.yml
Created Apr 28, 2019
Personal .swiftlint.yml
View .swiftlint.yml
disabled_rules:
- trailing_whitespace
- force_cast
- discarded_notification_center_observer
- type_body_length
- weak_computed_property
opt_in_rules:
- empty_string
@robertmryan
robertmryan / Database.swift
Last active May 6, 2019
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.
You can’t perform that action at this time.