Skip to content

Instantly share code, notes, and snippets.

Avatar

Dave Wood DaveWoodCom

View GitHub Profile
@DaveWoodCom
DaveWoodCom / TestProjectTests.swift
Created Jul 4, 2019
Xcode Test to test the performance of using a `for` loop with a `where` clause vs an `if` statement vs a `guard` statement.
View TestProjectTests.swift
//
// TestProjectTests.swift
// Tests
//
// Based on a question Nick Lockwood asked on Twitter: https://twitter.com/nicklockwood/status/1146508387498303488 .
// And then followed up by GeekAndDad https://twitter.com/GeekAndDad/status/1146630469842116609 .
// These tests were executed on an iMac 5k, against an iOS project running in the XR Simulator.
// My results are included as comments in each test below. I ran the tests 5 times (which itself runs each test 10 times).
// The average of each 10 runs are listed below, as well as the placing 1st/2nd/3rd for each of the 3 styles.
View Bug.txt
Here's some sample code that works fine in Xcode 10.1, but fails to compile in 10.2 even though it's valid code.
The #if/#else/#endif code seems to be confusing the compiler and thus it is mixing up scope.
Xcode Version 10.2 (10E125) -> Broken
Xcode Version 10.1 (10B61) -> Works correctly
@DaveWoodCom
DaveWoodCom / set_swift_version.post_install.ruby
Created Oct 4, 2017
`post_install` hook to set the Swift version of pods (add to the end of your project's podfile)
View set_swift_version.post_install.ruby
post_install do |installer|
print "Setting the default SWIFT_VERSION to 4.0\n"
installer.pods_project.build_configurations.each do |config|
config.build_settings['SWIFT_VERSION'] = '4.0'
end
installer.pods_project.targets.each do |target|
if ['SomeTarget-iOS', 'SomeTarget-watchOS'].include? "#{target}"
print "Setting #{target}'s SWIFT_VERSION to 3.0\n"
target.build_configurations.each do |config|
@DaveWoodCom
DaveWoodCom / String+Email.swift
Last active Feb 28, 2021
Swift method to check for a valid email address (uses Apples data detector instead of a regex)
View String+Email.swift
extension String {
func isValidEmail() -> Bool {
guard !self.lowercaseString.hasPrefix("mailto:") else { return false }
guard let emailDetector = try? NSDataDetector(types: NSTextCheckingType.Link.rawValue) else { return false }
let matches = emailDetector.matchesInString(self, options: NSMatchingOptions.Anchored, range: NSRange(location: 0, length: self.characters.count))
guard matches.count == 1 else { return false }
return matches[0].URL?.scheme == "mailto"
}
}
@DaveWoodCom
DaveWoodCom / fixXcode6OnElCapitan.sh
Last active Sep 15, 2021
Script to fix Xcode 6.x so that it will run on El Capitan
View fixXcode6OnElCapitan.sh
#!/bin/bash
## Copyright (C) 2015 Cerebral Gardens http://www.cerebralgardens.com/
##
## Permission is hereby granted, free of charge, to any person obtaining a copy of this
## software and associated documentation files (the "Software"), to deal in the Software
## without restriction, including without limitation the rights to use, copy, modify,
## merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
## permit persons to whom the Software is furnished to do so, subject to the following
## conditions:
@DaveWoodCom
DaveWoodCom / XCGTextViewLogDestination
Created Apr 16, 2015
An XCGLogDestination that adds the logs to a UITextView
View XCGTextViewLogDestination
extension XCGLogger {
public class XCGTextViewLogDestination: XCGLogDestinationProtocol, DebugPrintable {
public var owner: XCGLogger
public var identifier: String
public var outputLogLevel: XCGLogger.LogLevel = .Debug
public var showThreadName: Bool = false
public var showFileName: Bool = true
public var showLineNumber: Bool = true
View keybase.md

Keybase proof

I hereby claim:

  • I am davewoodcom on github.
  • I am davewood (https://keybase.io/davewood) on keybase.
  • I have a public key ASDVQq_MleuhYXva8i4Zs2Tt4yIJbtJx15W15Wd_Mx5Yfgo

To claim this, I am signing this object:

@DaveWoodCom
DaveWoodCom / gist:ce3a3a6467a7fc7a71a3
Created Aug 5, 2014
if optional != nil is fugly!
View gist:ce3a3a6467a7fc7a71a3
public extension Optional {
public var exists: Bool {
return (self != nil)
}
public var notExists: Bool {
return (self == nil)
}
}
@DaveWoodCom
DaveWoodCom / UIViewController+Alerts.h
Created Dec 31, 2012
UIViewController+Alerts Category for presenting a view controller with a transparency above another view controller
View UIViewController+Alerts.h
//
// UIViewController+Alerts.h
//
// Created by Dave Wood on 2012-01-17.
//
// Simplified BSD License
// Copyright (c) 2012, Cerebral Gardens
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
@DaveWoodCom
DaveWoodCom / clearCookies.m
Created Sep 19, 2012
Objective-C - clearCookies functions
View clearCookies.m
// By Dave Wood - Released into the Public Domain
+ (void)clearCookies
{
NSHTTPCookieStorage *httpCookieStorage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
for (NSHTTPCookie *httpCookie in [httpCookieStorage cookies])
{
[httpCookieStorage deleteCookie:httpCookie];
}
[[NSUserDefaults standardUserDefaults] synchronize];
}