Skip to content

Instantly share code, notes, and snippets.

@gkye
gkye / getRequest.swift
Last active September 6, 2020 16:48
NSURLSession: GET Request with parameters swift 2.0
//Encode params to be web friendly and return a string. Reference ->http://stackoverflow.com/a/27724627/6091482
extension String {
/// Percent escapes values to be added to a URL query as specified in RFC 3986
///
/// This percent-escapes all characters besides the alphanumeric character set and "-", ".", "_", and "~".
///
/// http://www.ietf.org/rfc/rfc3986.txt
///
@gkye
gkye / clear cache memory warning
Created March 24, 2016 17:34
clear cache memory warning. swift 2.2
func applicationDidReceiveMemoryWarning(application: UIApplication) {
NSURLCache.sharedURLCache().removeAllCachedResponses()
}
class VerticalLayout: UIView {
var yOffsets: [CGFloat] = []
init(width: CGFloat) {
super.init(frame: CGRectMake(0, 0, width, 0))
}
required init(coder aDecoder: NSCoder) {
@gkye
gkye / shiftArrayIndex.swift
Created February 22, 2016 03:58
shift array index
mutating func moveItem(fromIndex oldIndex: Index, toIndex newIndex: Index) {
insert(removeAtIndex(oldIndex), atIndex: newIndex)
}
var tags = [1,2,3,4,5,7782,25]
tags.moveItem(fromIndex: 2, toIndex: tags.count-1)
@gkye
gkye / shuffleArray.swift
Last active February 22, 2016 03:59
shuffle array (self)
mutating func shuffle() {
if count < 2 { return }
for i in 0..<(count - 1) {
let j = Int(arc4random_uniform(UInt32(count - i))) + i
swap(&self[i], &self[j])
}
}
var tags = [1,7782,25]
tags.shuffle()