Skip to content

Instantly share code, notes, and snippets.

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

A. Hinton ahcode0919

🏠
Working from home
  • Worldwide
View GitHub Profile
@ahcode0919
ahcode0919 / reverse-a-string-algorithm.swift
Last active October 29, 2021 12:59
A few ways to reverse a string in Swift
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
}
@ahcode0919
ahcode0919 / anagram-algorithm.swift
Last active April 24, 2022 17:49
Example of an anagram algorithm in Swift
//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
@ahcode0919
ahcode0919 / builder-pattern.swift
Last active April 17, 2017 22:59
Example of the builder pattern in Swift with protocols
//Playground friendly
import Foundation
/*
The Builder Pattern helps in the creation of complex objects
*Implemented with Protocols
*/
/* Before Builder Pattern is implemented */
@ahcode0919
ahcode0919 / singleton-pattern.swift
Last active March 3, 2021 06:44
Example of the singleton pattern in Swift
//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.
*/
@ahcode0919
ahcode0919 / date-time.swift
Last active April 17, 2017 23:00
Example of handling dates in Swift
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"