Skip to content

Instantly share code, notes, and snippets.

public extension Int {
public var seconds: DispatchTimeInterval {
return DispatchTimeInterval.seconds(self)
}
public var second: DispatchTimeInterval {
return seconds
}
@Thomvis
Thomvis / NSURLSession+FutureProof.swift
Created November 26, 2015 20:16
An extension of NSURLSession showcasing how its functionality could be wrapped in Futures
extension NSURLSession {
func fetch(url: String) -> Async<Result<(NSData, NSURLResponse), Error>> {
return Async { completion in
let task = self.dataTaskWithURL(NSURL(string: url)!, completionHandler: { (data, response, error) -> Void in
if let data = data, response = response {
completion(Result(value: (data, response)))
} else {
completion(Result(error: .LegacyError(error)))
}
})
config.connectionProxyDictionary = [
kCFNetworkProxiesHTTPProxy: "localhost",
kCFNetworkProxiesHTTPPort: 8888
];
public enum NoError {}
extension NoError: Equatable { }
public func ==(lhs: NoError, rhs: NoError) -> Bool {
return true
}
@Thomvis
Thomvis / selfu.swift
Last active August 29, 2015 14:27
This does not compile because Self in Self<U> is undeclared
class Box<T> {
let value: T
func map<U>(transform: T -> U) -> Self<U> {
return Self<U>(value: transform(value))
}
}
typedef NS_ENUM(NSInteger, HSArrayDiffActionType) {
HSArrayDiffActionTypeAppend,
HSArrayDiffActionTypeReplace,
HSArrayDiffActionTypeDelete
};
@interface HSArrayDiffAction : NSObject
@property (nonatomic) HSArrayDiffActionType type;
@property (nonatomic) id sourceObject;
var promise: Promise<Int, NSError>!
var testSubject: SomeObject!
beforeEach {
promise = Promise<Int, NSError>()
testSubject = SomeObject(future: promise.future)
}
describe("when the request succeeds") {
beforeEach {
@interface HSIntrinsicView : UIView
@property (nonatomic) CGSize intrinsicContentSize;
@end
@Thomvis
Thomvis / gist:6b91a8aa0bf233e125f0
Created June 29, 2015 09:28
http://fuckingblocksyntax.com should be updated to help with nullability in block definitions
- (void)methodWithBlock:(nonnull id __nullable (^)(NSString * __nonnull title))block;