Skip to content

Instantly share code, notes, and snippets.

View Nirma's full-sized avatar

Nicholas Maccharoli Nirma

  • Screaming Cactus LLC
  • Tokyo
  • 12:21 (UTC +09:00)
View GitHub Profile

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.

@HamptonMakes
HamptonMakes / RBResizer.swift
Created June 27, 2014 14:45
Swift Image Resizer
//
// RBResizer.swift
// Locker
//
// Created by Hampton Catlin on 6/20/14.
// Copyright (c) 2014 rarebit. All rights reserved.
//
import UIKit
@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 / 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 / enumerateEnum.swift
Last active May 12, 2017 01:56
Helper functions for enumerating values of a Swift enum
// 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.
@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
}