Skip to content

Instantly share code, notes, and snippets.

View Nirma's full-sized avatar

Nicholas Maccharoli Nirma

  • Screaming Cactus LLC
  • Tokyo
  • 10:01 (UTC +09:00)
View GitHub Profile
@ericdke
ericdke / base64.swift
Last active December 26, 2017 22:52
Base64 for 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
@aorcsik
aorcsik / string-truncate.swift
Last active September 27, 2017 16:22
A little truncate function extension for the default String type
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
}
}
@kristopherjohnson
kristopherjohnson / NSURLSession_Example.swift
Last active April 10, 2019 00:24
Swift Playground using NSURLSession
import Foundation
import XCPlayground
// Let asynchronous code run
XCPSetExecutionShouldContinueIndefinitely()
if let url = NSURL(string: "http://www.google.com/") {
let session = NSURLSession.sharedSession()
@ericdke
ericdke / splitBy.swift
Last active July 10, 2023 09:55
Swift: split array by chunks of given size
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])
}
}
}
@Nirma
Nirma / map_filter_reduce.swift
Last active January 24, 2016 14:16
Map and filter implemented with a version of reduce taking a list as input.
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
}
@adeekshith
adeekshith / .git-commit-template.txt
Last active February 21, 2024 12:06 — forked from Linell/.git-commit-template.txt
This commit message template helps you write great commit messages and enforce it across teams.
# <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

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.