throws
in Swift 2 is just another way to return from a function/method. Because of its propagation semantics, it's not really "exceptions", which is probably a good change.- But unlike an
Either
orResult
return type,throws
in Swift 2 cannot communicate type information. - This sucks; and we will realize it sucks when we keep having to refer to documentation every time we call a function that
throws
to see what sort of errors we may have to handle. This is part of the problem that types solve; they are documentation, with compiler-enforced guarantees. - Arguments based on the clunkiness of pattern-matching on a
Result
type vs. the new syntax are not valid. Had the Swift team chosen to introduce a blessed stdlibResult
type, they could have introduced elegant syntax to handle it without requiring boilerplate; remember, everything related tothrows
is new syntax. - Yes, this would make versioning harder. Changing the types of errors you return changes the contract of an API, if you care abou
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let nilString: String? = nil | |
let aString: String? = "a non-nil string" | |
print (nilString ?? "the string is nil") | |
print (aString ?? "the string is nil") | |
// if you don't want to use `??` … | |
// (inspired by Scala's Option's `getOrElse`) | |
extension Optional { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
tell application id "OOut" | |
try | |
set doc_name to name of front document | |
on error | |
activate | |
display alert "No Document Open" | |
return | |
end try | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var m=document.getElementsByTagName('meta'); | |
for(var i=0; i<m.length; i++) { | |
if (m[i].name == 'viewport') { | |
if (m[i].content.indexOf('user-scalable=0') != -1 || m[i].content.indexOf('user-scalable=no') != -1) { | |
m[i].content = 'width=device-width, initial-scale=1.0, minimum-scale=0.5, maximum-scale=4.0, user-scalable=1'; | |
alert('Found & fixed.'); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// CDZObjectWatcher.h | |
// | |
// Created by Chris Dzombak. | |
// Copyright (c) 2015 Chris Dzombak. All rights reserved. | |
// | |
@import Foundation; | |
@import CoreData; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// in -loadView or similar… | |
self.completeButtonBottomConstraint = [NSLayoutConstraint constraintWithItem:self.completeButton attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:v attribute:NSLayoutAttributeBottom multiplier:1.0 constant:-1.0*IDPCompleteButtonBottomSpacing]; | |
// in -viewWillAppear:… | |
[NSNotificationCenter.defaultCenter addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil]; | |
[NSNotificationCenter.defaultCenter addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil]; | |
- (void)keyboardWillShow:(NSNotification *)note { | |
[self updateBottomConstraintFromNotification:note]; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
property droppedContext : "Dropped" | |
tell application "OmniFocus" | |
tell front document | |
try | |
set theDroppedContextID to id of item 1 of (complete droppedContext as context) | |
set theDroppedContext to first flattened context whose id is theDroppedContextID | |
on error | |
display alert "No context found whose name contains “" & droppedContext & "”" | |
return |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@protocol NSArray | |
+ (instancetype)array; | |
+ (instancetype)arrayWithObject:(id)anObject; | |
+ (instancetype)arrayWithObjects:(const id [])objects count:(NSUInteger)cnt; | |
+ (instancetype)arrayWithObjects:(id)firstObj, ... NS_REQUIRES_NIL_TERMINATION; | |
+ (instancetype)arrayWithArray:(NSArray *)array; | |
- (instancetype)initWithObjects:(id)firstObj, ... NS_REQUIRES_NIL_TERMINATION; | |
- (instancetype)initWithArray:(NSArray *)array; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
NYTCompletionBlock completion = ^(id result) {}; | |
id completionMock = [OCMockObject partialMockForObject:completion]; | |
[[[completionMock expect] copy] andReturn:completion]; | |
id sut = [[NYTXXX alloc] initWithCompletion:completionMock]; | |
#pragma unused (sut) | |
[completionMock verify]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// CDZWeak.h | |
// https://gist.github.com/cdzombak/844e887ed4bdb933a905 | |
// | |
// Created by Chris Dzombak on 3/22/15. | |
// Copyright (c) 2015 Chris Dzombak. All rights reserved. | |
// | |
#ifndef CDZWeak_h | |
#define CDZWeak_h |