Skip to content

Instantly share code, notes, and snippets.

Avatar
🤠
Sailing the seas of Swift

Nicholas Maccharoli Nirma

🤠
Sailing the seas of Swift
View GitHub Profile
View Using Swift Package Manager with iOS.md

Using Swift Package Manager with iOS

Step 1:

File > New > Project...

Step 2:

Create a Package.swift file in your root project directory, add dependencies, then run swift package fetch on the command line in the same directory. We’re not going to run swift build because it will just complain.

@adeekshith
adeekshith / .git-commit-template.txt
Last active Oct 9, 2021 — forked from Linell/.git-commit-template.txt
This commit message template helps you write great commit messages and enforce it across teams.
View .git-commit-template.txt
# <type>: (If applied, this commit will...) <subject> (Max 50 char)
# |<---- Using a Maximum Of 50 Characters ---->|
# Explain why this change is being made
# |<---- Try To Limit Each Line to a Maximum Of 72 Characters ---->|
# Provide links or keys to any relevant tickets, articles or other resources
# Example: Github issue #23
@Nirma
Nirma / map_filter_reduce.swift
Last active Jan 24, 2016
Map and filter implemented with a version of reduce taking a list as input.
View map_filter_reduce.swift
func reduce<T,R>(list: [T], block: (([R],T) -> R?)) -> [R]? {
var acc = [R]()
for x in list {
if let val = block(acc, x) {
acc += [val]
}
}
return acc
}
@ericdke
ericdke / splitBy.swift
Last active Nov 23, 2020
Swift: split array by chunks of given size
View splitBy.swift
let arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
extension Array {
func splitBy(subSize: Int) -> [[Element]] {
return 0.stride(to: self.count, by: subSize).map { startIndex in
let endIndex = startIndex.advancedBy(subSize, limit: self.count)
return Array(self[startIndex ..< endIndex])
}
}
}
@kristopherjohnson
kristopherjohnson / NSURLSession_Example.swift
Last active Apr 10, 2019
Swift Playground using NSURLSession
View NSURLSession_Example.swift
import Foundation
import XCPlayground
// Let asynchronous code run
XCPSetExecutionShouldContinueIndefinitely()
if let url = NSURL(string: "http://www.google.com/") {
let session = NSURLSession.sharedSession()
@aorcsik
aorcsik / string-truncate.swift
Last active Sep 27, 2017
A little truncate function extension for the default String type
View string-truncate.swift
extension String {
/// Truncates the string to length number of characters and
/// appends optional trailing string if longer
func truncate(length: Int, trailing: String? = nil) -> String {
if countElements(self) > length {
return self.substringToIndex(advance(self.startIndex, length)) + (trailing ?? "")
} else {
return self
}
}
@ericdke
ericdke / base64.swift
Last active Dec 26, 2017
Base64 for Swift
View base64.swift
let plainString = "foo"
// Encoding
guard let plainData = (plainString as NSString).dataUsingEncoding(NSUTF8StringEncoding) else {
fatalError()
}
let base64String = plainData.base64EncodedStringWithOptions(NSDataBase64EncodingOptions(rawValue: 0))
print(base64String) // Zm9v
@hlissner
hlissner / replace.sh
Last active Jul 6, 2021
Bulk search & replace with ag (the_silver_searcher)
View replace.sh
# ag <https://github.com/ggreer/the_silver_searcher>
# usage: ag-replace.sh [search] [replace]
# caveats: will choke if either arguments contain a forward slash
# notes: will back up changed files to *.bak files
ag -0 -l $1 | xargs -0 perl -pi.bak -e "s/$1/$2/g"
# or if you prefer sed's regex syntax:
ag -0 -l $1 | xargs -0 sed -ri.bak -e "s/$1/$2/g"
@kristopherjohnson
kristopherjohnson / enumerateEnum.swift
Last active May 12, 2017
Helper functions for enumerating values of a Swift enum
View enumerateEnum.swift
// Protocol for a type that supports a fromRaw(Int) conversion
// (such as "enum Foo: Int { ... }")
protocol ConvertibleFromRawInt {
class func fromRaw(raw: Int) -> Self?
}
// Note: Tried to use Swift's standard RawRepresentable protocol rather
// than ConvertibleFromRawInt, but couldn't get it to compile.
// Don't know whether it is a Swift bug or something I was doing wrong.
@hcatlin
hcatlin / RBResizer.swift
Created Jun 27, 2014
Swift Image Resizer
View RBResizer.swift
//
// RBResizer.swift
// Locker
//
// Created by Hampton Catlin on 6/20/14.
// Copyright (c) 2014 rarebit. All rights reserved.
//
import UIKit