Skip to content

Instantly share code, notes, and snippets.

@leemorgan
leemorgan / Fibonacci.swift
Last active February 22, 2024 21:37
Fibonacci series in Swift
// Fibonacci series
// F[n] = F[n-1] + F[n-2]
// 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144
// Find the fibonacci number for n interations
func fibonacci(n: Int) {
var num1 = 0
var num2 = 1
@leemorgan
leemorgan / Clamp.swift
Last active November 9, 2022 15:36
clamp() in Swift
///Returns the input value clamped to the lower and upper limits.
func clamp<T: Comparable>(value: T, lower: T, upper: T) -> T {
return min(max(value, lower), upper)
}
//-----------------------------------------------
// Example usage
let proposedIndex = 6
@leemorgan
leemorgan / Xcode Project Line Count
Created October 19, 2014 18:28
Count the lines of code in a Xcode project
# Count the lines of code in a Xcode project
find . "(" -name "*.m" -or -name "*.mm" -or -name "*.cpp" ")" -print0 | xargs -0 wc -l
@leemorgan
leemorgan / arrayExtension.swift
Created August 16, 2015 21:37
Array extension where Element: Int
extension Array where Element: Int {
func foo() {
}
}
var anArray = [Int]()
anArray.foo() // '[Int]' does not have a member named 'foo'
@leemorgan
leemorgan / AutoAssets.md
Last active August 29, 2015 14:25
Xcode build script to generate a Swift file enumerating the project's assets
@leemorgan
leemorgan / autoBuildNumber
Created June 22, 2015 22:56
Auto-populate project build number based on current date and time
# @desc Auto-populate the build number based on the current date and time.
# @usage
# 1. Select: your Target in Xcode
# 2. Select: Build Phases tab
# 3. Select: Add Build Phase -> Add Run Script
# 4. Paste code below in to new "Run Script" section
# 5. Drag the "Run Script" below "Target Dependencies"
now=`date +%y.%m.%d.%H%M`
buildNumber=$(printf "%s" $now)