Skip to content

Instantly share code, notes, and snippets.

View loganblevins's full-sized avatar

Logan Blevins loganblevins

View GitHub Profile
extension Double {
func safeInt() -> Int? {
guard isFinite else { return nil }
if self >= Double(Int.min) && self < Double(Int.max) {
return Int(self)
} else {
return nil
}
}
}
@loganblevins
loganblevins / XCTestCase+teardown.swift
Created October 16, 2024 17:14
XCTestCase+teardown
extension XCTestCase {
func trackForMemoryLeaks(_ instance: AnyObject, file: StaticString = #filePath, line: UInt = #line) {
addTeardownBlock { [weak instance] in
XCTAssertNil(instance, "Instance should have been deallocated. Potential memory leak.", file: file, line: line)
}
}
}
import Foundation
import Combine
import XCTest
@testable import Stock
extension XCTestCase {
func awaitPublisher<T: Publisher>(
_ publisher: T,

iOS-PairProgramming

Review the REST API playground to review JSON structure: https://swapi.dev

  1. Create new iOS or tvOS project
  2. Create app that fetches data from the following REST API: https://swapi.dev/api/people/1
  3. Display name and height of person ID 1 fetched using above endpoint
  4. Consider architecture, testability, error handling, and empty/placeholder state
@loganblevins
loganblevins / MemoryFootprint.swift
Last active November 19, 2021 15:43
Get current memory footprint, most resembling that of memory profiling tools in Xcode
// https://stackoverflow.com/a/57315975
// https://forums.developer.apple.com/thread/105088#357415
func memoryFootprint() -> mach_vm_size_t? {
// The `TASK_VM_INFO_COUNT` and `TASK_VM_INFO_REV1_COUNT` macros are too
// complex for the Swift C importer, so we have to define them ourselves.
let TASK_VM_INFO_COUNT = mach_msg_type_number_t(MemoryLayout<task_vm_info_data_t>.size / MemoryLayout<integer_t>.size)
let TASK_VM_INFO_REV1_COUNT = mach_msg_type_number_t(MemoryLayout.offset(of: \task_vm_info_data_t.min_address)! / MemoryLayout<integer_t>.size)
var info = task_vm_info_data_t()
var count = TASK_VM_INFO_COUNT
@loganblevins
loganblevins / Apple_mobile_device_types.txt
Created December 18, 2020 19:20 — forked from adamawolf/Apple_mobile_device_types.txt
List of Apple's mobile device codes types a.k.a. machine ids (e.g. `iPhone1,1`, `Watch1,1`, etc.) and their matching product names
i386 : iPhone Simulator
x86_64 : iPhone Simulator
iPhone1,1 : iPhone
iPhone1,2 : iPhone 3G
iPhone2,1 : iPhone 3GS
iPhone3,1 : iPhone 4
iPhone3,2 : iPhone 4 GSM Rev A
iPhone3,3 : iPhone 4 CDMA
iPhone4,1 : iPhone 4S
iPhone5,1 : iPhone 5 (GSM)
@loganblevins
loganblevins / CMSampleBufferRef_to_vImage.m
Created March 31, 2020 20:18 — forked from podkovyrin/CMSampleBufferRef_to_vImage.m
CMSampleBufferRef to vImage and resize
CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
CVPixelBufferLockBaseAddress(imageBuffer,0);
size_t height = CVPixelBufferGetHeight(imageBuffer);
size_t width = CVPixelBufferGetWidth(imageBuffer);
size_t bytesPerRow = CVPixelBufferGetBytesPerRow(imageBuffer);
void *sourceData = CVPixelBufferGetBaseAddress(imageBuffer);
// Set a bunch of variables we need. The "radius" for the blur kernel needs to be positive and odd. The permute map maps the BGRA channels of the buffer to the ARGB that vImage needs.
@loganblevins
loganblevins / UIImageToDataTests.swift
Created February 19, 2020 17:57 — forked from benbahrenburg/UIImageToDataTests.swift
Swift Playground for testing memory associated with converting UIImage to Data
import UIKit
import ImageIO
import MobileCoreServices
import PlaygroundSupport
PlaygroundPage.current.needsIndefiniteExecution = true
func report_memory() -> UInt64 {
var taskInfo = mach_task_basic_info()
var count = mach_msg_type_number_t(MemoryLayout<mach_task_basic_info>.size)/4
@loganblevins
loganblevins / ..build-apple-protobuf-3.6.1.md
Last active August 29, 2018 21:03 — forked from petewarden/..build-protobuf-3.0.0.md
Script used to build Google Protobuf 3.6.1 for use with Xcode 9 / iOS 11. Builds for all supported architectures and produces a universal binary static library.

Google Protobuf 3.6.1 - Mac OS X and iOS Support

The script in this gist will help you buid the Google Protobuf library for use with Mac OS X and iOS. Other methods (such as homebrew or direct compilation) have issues that prevent their use. The libraries built by this script are universal and support all iOS device architectures including the simulator.

This gist was adapted from the original at https://gist.github.com/BennettSmith/7150245, further adapted from https://gist.github.com/petewarden/c8d172709018780eb069 and updated to deal with iPhone Simulator - i386 issues when building on >=Sierra, and download protobuf version 3.6.1.