Skip to content

Instantly share code, notes, and snippets.

View zwaldowski's full-sized avatar

Zachary Waldowski zwaldowski

View GitHub Profile
/// Compare and swap references.
///
/// This function compares the reference `old` to the reference in the
/// location referenced by `to`. If the references match, this method
/// stores the reference `new` to that location atomically.
///
/// Similar to a pointer compare-and-swap, but safe for owned (retaining) pointers:
/// - ObjC: `MyObject *__strong *`
/// - Swift: `UnsafeMutablePointer<MyObject>`
/// If the swap is made, the new value is retained by its owning pointer.
// Derived from http://blog.krzyzanowskim.com/2015/10/24/chunksequence-have-cake-and-eat-it/
struct ChunkSequence<Element>: SequenceType {
typealias Collection = AnyForwardCollection<Element>
let collection: Collection
let chunkSize: Collection.Index.Distance
private init<C: CollectionType where C.Index: ForwardIndexType, C.Generator.Element == Element>(_ base: C, chunkSize: C.Index.Distance) {
self.collection = AnyForwardCollection(base)
self.chunkSize = chunkSize.toIntMax()
~ xcrun -n -v --show-sdk-platform-path
xcrun: note: looking up SDK with '/Applications/Xcode.app/Contents/Developer/usr/bin/xcodebuild -sdk macosx -version Path'
xcrun: note: PATH = '/usr/local/opt/rbenv/shims:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin'
xcrun: note: SDKROOT = 'macosx'
xcrun: note: TOOLCHAINS = ''
xcrun: note: DEVELOPER_DIR = '/Applications/Xcode.app/Contents/Developer'
xcrun: note: XCODE_DEVELOPER_USR_PATH = ''
xcrun: note: xcrun_db = '/var/folders/vg/h8b5rtg91y36r8d_44kfp6j00000gn/T/xcrun_db'
2015-09-25 11:06:46.977 xcodebuild[59708:8756864] [MT] PluginLoading: Required plug-in compatibility UUID 0420B86A-AA43-4792-9ED0-6FE0F2B16A13 for plug-in at path '~/Library/Application Support/Developer/Shared/Xcode/Plug-ins/Multiplex.xcplugin' not present in DVTPlugInCompatibilityUUIDs
2015-09-25 11:06:46.979 xcodebuild[59708:8756864] [MT] PluginLoading: Required plug-in compatibility UUID 0420B86A-AA43-4792-9ED0-6FE0F2B16A13 for plug-in at path '~/Library/Application Support/Developer/Shared/Xcode/
~ xcrun -n -v --show-sdk-platform-path
xcrun: note: looking up SDK with '/Applications/Xcode-6.app/Contents/Developer/usr/bin/xcodebuild -sdk macosx -version Path'
2015-09-25 11:00:15.279 xcodebuild[59635:8742785] [MT] PluginLoading: Required plug-in compatibility UUID 7FDF5C7A-131F-4ABB-9EDC-8C5F8F0B8A90 for plug-in at path '~/Library/Application Support/Developer/Shared/Xcode/Plug-ins/Multiplex.xcplugin' not present in DVTPlugInCompatibilityUUIDs
xcrun: note: lookup resolved to: '/Applications/Xcode-6.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.10.sdk'
xcrun: note: looking up SDK with '/Applications/Xcode-6.app/Contents/Developer/usr/bin/xcodebuild -sdk /Applications/Xcode-6.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.10.sdk -version PlatformPath'
2015-09-25 11:00:15.708 xcodebuild[59636:8742811] [MT] PluginLoading: Required plug-in compatibility UUID 7FDF5C7A-131F-4ABB-9EDC-8C5F8F0B8A90 for plug-in at path '~/Library/Application Support/Developer/Sh
// http://opensource.apple.com/source/CF/CF-1153.18/CFInternal.h
// Returns a generic dispatch queue for when you want to just throw some work
// into the concurrent pile to execute, and don't care about specifics except
// to match the QOS of the main thread.
CF_INLINE dispatch_queue_t __CFDispatchQueueGetGenericMatchingMain(void) {
return dispatch_get_global_queue(qos_class_main(), DISPATCH_QUEUE_OVERCOMMIT);
}
// Returns a generic dispatch queue for when you want to just throw some work
@zwaldowski
zwaldowski / MyFramework-Common.xcconfig
Created September 20, 2015 18:01
Using "private" C module from Swift
SWIFT_INCLUDE_PATHS = $(SRCROOT)
protocol MyProtocol {}
protocol MyProtocolView: MyProtocol {
var view: UIView { get }
}
extension MyProtocolView where Self: UIView {
enum AddingError: ErrorType {
case TooBig
}
let x = try? (0..<10000).reduce(0) {
if $1 > 50 {
throw AddingError.TooBig
}
return $0 + $1
}
private extension JSON {
init<Collection: CollectionType where Collection.Generator.Element == JSON>(_ collection: Collection) {
self = .Array(Swift.Array(collection))
}
init<Dictionary: SequenceType where Dictionary.Generator.Element == (Swift.String, JSON)>(_ pairs: Dictionary) {
var dictionary = Swift.Dictionary<Swift.String, JSON>(minimumCapacity: pairs.underestimateCount())
for (key, value) in pairs {
dictionary[key] = value
private func itemForQuery<T: AnyObject>(keychainQuery: KeychainAttributes, all: Bool = false, includeReferences: Bool = false) -> Result<T?, ErrorType> {
let isData = T.self is NSData.Type
precondition(!isData || !includeReferences, "Cannot get references when fetching data")
let limitedQuery = keychainQuery + [
kSecMatchLimit: kSecMatchLimitAll,
kSecReturnAttributes: true,
kSecReturnPersistentRef: includeReferences