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 dateString = "09 Aug 2016 17:53:51 -0700" | |
let dateFormatter = NSDateFormatter() | |
// Good resource for date time format syntax | |
//http://userguide.icu-project.org/formatparse/datetime | |
dateFormatter.dateFormat = "dd MMM yyyy HH:mm:ss ZZZ" | |
let date = dateFormatter.dateFromString(dateString) //NSDate = "Aug 09, 2016, 5:53 PM" |
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
//XCode Playground Friendly | |
/** | |
Singleton Pattern - Ensures that only one instance of a object exists in the application context | |
usefull for things that will have a global context across the app. | |
Ex: An internet connection, Credentials, etc. | |
* Can only be used for reference types. | |
*/ |
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
//Playground friendly | |
import Foundation | |
/* | |
The Builder Pattern helps in the creation of complex objects | |
*Implemented with Protocols | |
*/ | |
/* Before Builder Pattern is implemented */ |
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
//Anagram - Two words that contain the same characters | |
let testWord = "abcde" | |
let testWord2 = "edcba" | |
func isAnagram(word word: String, isAnagramOf word2: String) -> Bool { | |
guard | |
let word1Dictionary = countChars(word), | |
let word2Dictionary = countChars(word2) else { | |
return false |
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 word = "abcdefg" | |
//Using swift lib | |
String(word.characters.reverse()) //"gfedcba" | |
//Using char array and insert | |
func reverseString(wordToReverse: String) -> String { | |
guard wordToReverse.characters.count > 1 else { | |
return wordToReverse | |
} |
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" | |
} |
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
//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
//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
//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 |
OlderNewer