Skip to content

Instantly share code, notes, and snippets.

@cennydavidsson
cennydavidsson / .swift
Last active June 11, 2019 14:36
Sampel unit test for a view controller.
class CalculatorViewControllerTests: XCTestCase {
func testEquationLabel_OnLoad_TextShouldBeEmpty() {
let sut = makeLoadedViewController()
XCTAssertEqual("" ,sut.EquationLabel.text)
}
func test_CalculateButton_TouchUpInside_ShouldCalculateEquationText() {
let sut = makeLoadedViewController()
sut.EquationLabel.text = "3+5"
struct Superhero: SuperheroType {
let name: String
let secretIdentiy: String
let canFly: Bool
let powerLevel: Int
}
protocol SuperheroType {
struct Superhero {
let name: String
let secretIdentiy: String
let canFly: Bool
let powerLevel: Int
func flyToLocation(location: Location) {/* implementation */}
func add(x: Int?, y: Int?) -> Int? {
guard let x = x where x.isEven else { return .None }
guard let y = y else { return .None }
return x + y
}
func add(x: Int?, y: Int?) -> Int? {
guard let x = x, y = y where x.isEven else { return .None }
return x + y
}
extension IntegerType {
var isEven: Bool {
return self % 2 == 0
}
}
func add(x: Int?, y: Int?) -> Int? {
if let x = x, y = y where x.isEven {
return x + y
}
return .None
}
//
// AttatchButton.swift
// Solution
//
// Created by Cenny Davidsson on 2014-10-26.
// Copyright (c) 2014 Cenny. All rights reserved.
//
import UIKit
@cennydavidsson
cennydavidsson / validInput-ObjC.m
Last active August 29, 2015 14:05
An old snippet from the code of my game Solution.
+ (NSCharacterSet *)validCharacters {
return [NSCharacterSet characterSetWithCharactersInString:@"1234567890+-*()"];
}
+ (BOOL)isStringValid:(NSString *)string {
if (!string) return NO;
NSCharacterSet *inputSet = [NSCharacterSet characterSetWithCharactersInString:string];
if (string.length > 1) {
if ([[NSCharacterSet decimalDigitCharacterSet] isSupersetOfSet:inputSet]) return YES;
}else {
@cennydavidsson
cennydavidsson / validInput-bad.swift
Last active August 29, 2015 14:05
First attempt to rewrite some of the Solution codebase to swift.
func validInput(input:String) -> Bool {
var last = ""
if let x = equation.last { last = x }
switch last {
case "+", "-", "*", "(", "":
switch input {
case let x where input.toInt() != nil:
return true