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
//Simple Type - Person | |
struct Person: Codable { | |
let name: String | |
let age: Int | |
func getString() -> String { | |
return "Name: \(name), Age: \(age)" | |
} | |
} |
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
static boolean isBetween(String start, String end) { | |
def sdf = new SimpleDateFormat("HH:mm") | |
def startCalendar = Calendar.getInstance() | |
startCalendar.setTime(sdf.parse(start)) | |
def endCalendar = Calendar.getInstance() | |
endCalendar.setTime(sdf.parse(end)) | |
def currentCalendar = Calendar.getInstance() |
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
import Foundation | |
/// General doc comment | |
struct SomeClass { | |
private static var count: Int = 0 | |
let aString: String | |
// MARK: - Initializers | |
init(aString: String) { |
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
struct Device { | |
/// Detects if the current architecture is a Simulator | |
static let isSimulator: Bool = { | |
var isSim = false | |
#if arch(i386) || arch(x86_64) | |
isSim = true | |
#endif | |
return isSim | |
}() |
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
extension String { | |
func getDecimalDigits(includeDecimal: Bool = true) -> String { | |
let set = includeDecimal ? "1234567890.": "1234567890" | |
return self.components(separatedBy: CharacterSet(charactersIn: set).inverted).joined() | |
} | |
func lettersOnly() -> String { | |
let characterSet = CharacterSet(charactersIn: "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ").inverted |
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
//Creating an operator | |
//prefix, postfix, infix | |
//https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html | |
//https://developer.apple.com/reference/swift/swift_standard_library_operators | |
postfix operator *** | |
///Cubes value | |
postfix func *** (_ input: Int) -> Int { | |
return input * input * input |
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
//Zip function (Sequence) | |
// https://github.com/apple/swift/blob/master/stdlib/public/core/Zip.swift | |
// https://developer.apple.com/reference/swift/1541125-zip | |
// http://swiftdoc.org/v3.0/func/zip/ | |
let array1 = ["one", "two", "three", "four", "five"] | |
let array2 = 1...5 | |
let zipSequence = zip(array1, array2) | |
type(of: zipSequence) //Zip2Sequence<Array<String>, CountableClosedRange<Int>>.Type |
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
//Custom Map functions | |
//Experimenting with making custom map functions | |
//Original Map implementation: https://github.com/apple/swift/blob/master/stdlib/public/core/Sequence.swift | |
extension Sequence { | |
func mapToDictionary<T>(_ transform: (Iterator.Element) throws -> (String, T)) rethrows -> [String: T] { | |
let initialCapacity = underestimatedCount | |
var result = [String: T]() | |
var iterator = self.makeIterator() | |
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
// Bridge pattern (Structural) | |
// | |
// Decouples an abstraction from its implementation, so that the two can vary independently. | |
// Accomplished by seperating two interacting features and then creating a bridge type to | |
// handle where they overlap | |
protocol Database { | |
func closeConnection() | |
func openConnection() | |
func saveRecord(recordToSave: Record) | |
} |
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
//Factory Pattern (Creational) | |
//Allows logic of which type will be created to be handled by a method | |
protocol SimpleType { | |
var simpleName: String { get } | |
} | |
struct Type1: SimpleType { | |
var simpleName: String = "Type1" | |
} |