Skip to content

Instantly share code, notes, and snippets.

View hvsw's full-sized avatar
🏠
Working from home

Henrique Valcanaia hvsw

🏠
Working from home
View GitHub Profile
@hvsw
hvsw / iOS7-notifications.h
Created January 3, 2016 06:09 — forked from hpique/iOS7-notifications.h
List of all public notifications available in iOS 7.0 (or at least those whose constants are properly named).
// Developer/Library/Frameworks/SenTestingKit.framework/Headers/SenTestCaseRun.h
SENTEST_EXPORT NSString * const SenTestCaseDidStartNotification;
SENTEST_EXPORT NSString * const SenTestCaseDidStopNotification;
SENTEST_EXPORT NSString * const SenTestCaseDidFailNotification;
// Developer/Library/Frameworks/SenTestingKit.framework/Headers/SenTestDistributedNotifier.h
SENTEST_EXPORT NSString * const SenTestNotificationIdentifierKey;
// Developer/Library/Frameworks/SenTestingKit.framework/Headers/SenTestSuiteRun.h
SENTEST_EXPORT NSString * const SenTestSuiteDidStartNotification;
public protocol ErrorType {}
extension ErrorType {}
protocol ErrorType {
var _domain: Swift.String { get }
var _code: Swift.Int { get }
}
extension ErrorType {
public var _domain: String {
return String(reflecting: self.dynamicType)
}
}
@hvsw
hvsw / MyError.swift
Last active September 17, 2016 17:15
enum MyError: ErrorType {
case AnError,
case AnotherError(message: String)
}
enum DivisionErrorEnum: ErrorType {
case DividendIsZero(message: String)
case DivisorIsZero(message: String)
}
func divide(x: Float, _ y: Float) throws -> Float {
guard x != 0 else {
throw DivisionErrorEnum.DividendIsZero(message: "You’re trying to divide 0")
}
guard y != 0 else {
throw DivisionErrorEnum.DivisorIsZero(message: "You’re trying to divide \(x) by 0")
}
return x/y
func calculate() {
do {
let x:Float = 2.0
let y:Float = 2.0
let result = try divide(x, y)
resultLabel.text = String(result)
} catch DivisionErrorEnum.DividendIsZero(let msg) {
print(msg)
} catch DivisionErrorEnum.DivisorIsZero(let msg) {
print(msg)
enum DivisionErrorEnum: ErrorType {
case DividendIsZero(code: Int)
case DivisorIsZero(code: Int)
}
func calculate() {
do {
let x = 2
let y = 2
let result = try divide(x, y)
resultLabel.text = String(result)
} catch DivisionErrorEnum.DividendIsZero(let code) {
print("Your message when the DIVIDEND is zero. \nDetails: Execution failed with code \(code).")
} catch DivisionErrorEnum.DivisorIsZero(let code) {
print("Your custom message when the DIVISOR is zero. \nDetails: Execution failed with code \(code).")
// Using optional try
let x = try? divide(0, 2) // (Yes you can use an if let/guard/whatever to unwrap it safely)
// Using do-catch
let y: Float?
do {
y = try divide(0, 2)
} catch { // exhaustive just for testing
y = nil
}