Skip to content

Instantly share code, notes, and snippets.

@steventroughtonsmith
steventroughtonsmith / Foundation.py
Last active November 15, 2023 23:48
UIKit+UIFoundation & Foundation for Pythonista - autoconverted from SDK tbd (includes non-public SPI)
# coding: utf-8
from objc_util import *
NSAKDeserializer = ObjCClass('NSAKDeserializer')
NSAKDeserializerStream = ObjCClass('NSAKDeserializerStream')
NSAKSerializer = ObjCClass('NSAKSerializer')
NSAKSerializerStream = ObjCClass('NSAKSerializerStream')
NSAbstractLayoutGuide = ObjCClass('NSAbstractLayoutGuide')
NSAddressCheckingResult = ObjCClass('NSAddressCheckingResult')
NSAffineTransform = ObjCClass('NSAffineTransform')
@nicklockwood
nicklockwood / gist:21495c2015fd2dda56cf
Last active August 13, 2020 13:57
Thoughts on Swift 2 Errors

Thoughts on Swift 2 Errors

When Swift was first announced, I was gratified to see that one of the (few) philosophies that it shared with Objective-C was that exceptions should not be used for control flow, only for highlighting fatal programming errors at development time.

So it came as a surprise to me when Swift 2 brought (What appeared to be) traditional exception handling to the language.

Similarly surprised were the functional Swift programmers, who had put their faith in the Haskell-style approach to error handling, where every function returns an enum (or monad, if you like) containing either a valid result or an error. This seemed like a natural fit for Swift, so why did Apple instead opt for a solution originally designed for clumsy imperative languages?

I'm going to cover three things in this post:

// Previous async code based on Result. A "Search" wraps an async operation and offers features like cancel().
// An earlier experiment includes self.pages = result ?? [], but that's cheating.
// If you didn't care about error handling, what's the point of Result? Added displayError() to test reality.
// Of course maybe "self.pages = result ?? { displayError(err); return [] }()" would work,
// but that gets a little obtuse and scales poorly.
func search(text: String, completionHandler: (Result<[Page]>) -> Void) -> Search { ... }
self.currentSearch = self.searcher.search(searchString, completionHandler: { result in
switch result {
@radex
radex / wwdc2015.md
Last active August 29, 2015 14:22
WWDC (iOS 9, OS X 10.11, Swift 2.0, Xcode 7) hopes&wishes

iOS 9:

  1. Stability, stability, stability. Things should just work. Especially networking/cloud related stuff: AirDrop, Handoff, syncing, etc…
  2. Notification center that's actually useful. Instead of grouping them by apps, and failing to intelligently show the notifications I'm interested in, show the chronological stream/feed of all notifications. Don't remove anything from that feed, only gray out notifications you've tapped on/seen. So, just like Facebook notifications. Much better model.
  3. Presence awareness for more intelligent notifications. Don't give me the cacophony of notifications of all 4 devices. Devices within each other's BTLE range should have awareness of each other and know that if I'm actively using my Mac, I want notifications to only show up there.
  4. Better app folders. The 3x3 grid is bullshit design, and even worse on the iPad. Make the folder zoom in and expand into a scrolling list of apps, not a paged interface. Kinda like on the Mac
  5. Allow people to choose default a
@andymatuschak
andymatuschak / MultiDirectionAdjudicatingScrollView.swift
Created January 26, 2015 19:31
Source for the Khan Academy app's unusual scrolling interactions
//
// MultiDirectionAdjudicatingScrollView.swift
// Khan Academy
//
// Created by Andy Matuschak on 12/16/14.
// Copyright (c) 2014 Khan Academy. All rights reserved.
//
import UIKit
import UIKit.UIGestureRecognizerSubclass
@indragiek
indragiek / DynamicTypeLabel.swift
Last active August 25, 2017 15:08
UILabel subclass that automatically adjusts the font when the global dynamic type setting changes.
//
// DynamicTypeLabel.swift
//
// Created by Indragie on 10/16/14.
// Copyright (c) 2014 Indragie Karunaratne. All rights reserved.
//
import UIKit
class DynamicTypeLabel : UILabel {
@steipete
steipete / DevelopmentEnviromentDetector.m
Last active October 30, 2019 03:49
Detect if you're currently running a development version or an App Store/Ad Hoc version.
static BOOL PSPDFIsDevelopmentBuild(void) {
#if TARGET_IPHONE_SIMULATOR
return YES;
#else
static BOOL isDevelopment = NO;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
// There is no provisioning profile in AppStore Apps.
NSData *data = [NSData dataWithContentsOfFile:[NSBundle.mainBundle pathForResource:@"embedded" ofType:@"mobileprovision"]];
if (data) {
@brentsimmons
brentsimmons / gist:5810992
Last active January 3, 2021 02:22
Detect a tap on a URL inside a UITextView. Note: the rs_links method isn't included -- you'll need something that takes text and returns an array of detected links. This gist just demonstrates walking through the UITextView characters.
@implementation UITextView (RSExtras)
static BOOL stringCharacterIsAllowedAsPartOfLink(NSString *s) {
/*[s length] is assumed to be 0 or 1. s may be nil.
Totally not a strict check.*/
if (s == nil || [s length] < 1)
return NO;
@boredzo
boredzo / wwdc2013index-redacted.txt
Last active December 18, 2015 12:59
Script to rename WWDC videos and slides PDFs to include the session title in the name. Also included: A list of the session numbers and (redacted where necessary) titles, in TSV format, for use with this script.
100 Keynote
101 Platforms State of the Union
102 Apple Design Awards
109 Painting the Future
200 Accessibility in OS X
201 Building User Interfaces for iOS 7
202 Accessibility in iOS
203 What’s New in Cocoa Touch
204 What’s New with Multitasking
205 What’s New in Cocoa
@steipete
steipete / PSPDFUIKitMainThreadGuard.m
Last active March 10, 2024 19:23
This is a guard that tracks down UIKit access on threads other than main. This snippet is taken from the commercial iOS PDF framework http://pspdfkit.com, but relicensed under MIT. Works because a lot of calls internally call setNeedsDisplay or setNeedsLayout. Won't catch everything, but it's very lightweight and usually does the job.You might n…
// Taken from the commercial iOS PDF framework http://pspdfkit.com.
// Copyright (c) 2014 Peter Steinberger, PSPDFKit GmbH. All rights reserved.
// Licensed under MIT (http://opensource.org/licenses/MIT)
//
// You should only use this in debug builds. It doesn't use private API, but I wouldn't ship it.
// PLEASE DUPE rdar://27192338 (https://openradar.appspot.com/27192338) if you would like to see this in UIKit.
#import <objc/runtime.h>
#import <objc/message.h>