Skip to content

Instantly share code, notes, and snippets.

View kostiakoval's full-sized avatar

Kostiantyn Koval kostiakoval

View GitHub Profile
func testFunc() {
for i in 0...100 {
let y = i * 10
}
}
testFunc()
struct DataEncodable<T> {
static func encode(var value: T) -> NSData {
return withUnsafePointer(&value) { NSData(bytes: $0, length: sizeof(T)) }
}
static func decode(data: NSData) -> T {
let pointer = UnsafeMutablePointer<T>.alloc(sizeof(T))
data.getBytes(pointer, length: sizeof(T))
@kostiakoval
kostiakoval / NSCoder+Sugar.swift
Last active August 29, 2015 14:23
Encode any type to NSData
func encode(var value: T) -> NSData {
return withUnsafePointer(&value) { NSData(bytes: $0, length: sizeof(T)) }
}
func decode(data: NSData) -> T {
let pointer = UnsafeMutablePointer<T>.alloc(sizeof(T))
data.getBytes(pointer, length: sizeof(T))
return pointer.move()
}
#!/bin/sh
#ID='A16FF353-8441-459E-A50C-B071F53F51B7' # Xcode 6.2
#ID='992275C1-432A-4CF7-B659-D84ED6D42D3F' # Xcode 6.3
ID='9F75337B-21B4-4ADC-B558-F9CADF7073A7' # Xcode 6.3 GM
PLIST_BUDDY=/usr/libexec/PlistBuddy
function add_compatibility() {
"$PLIST_BUDDY" -c "Add DVTPlugInCompatibilityUUIDs:10 string $2" \
@kostiakoval
kostiakoval / loop-sped-swift.swift
Last active December 12, 2015 11:11
loop-speed
let startTime = CACurrentMediaTime()
var sum = 0
for var i = 0; i < 100_000_000; i++ {
//sum += Int(arc4random_uniform(UINT32_MAX))
sum += i
}
let totalTime = CACurrentMediaTime() - startTime
print("time: - \(totalTime) sec")
print("total: \(sum)")
@kostiakoval
kostiakoval / loop-speed.c
Last active August 29, 2015 14:18
blog-speed-c
double startTime = clock();
unsigned int sum = 0;
for (int i = 0; i < 100000000; i++) {
// sum += arc4random_uniform(UINT32_MAX);
sum += i;
}
double endTime = clock();
double totalTime = (endTime — startTime) / CLOCKS_PER_SEC;
@kostiakoval
kostiakoval / Tuple_otherRules.swift
Last active August 29, 2015 14:16
Tuples as function arguments, part 3
//Default value
func sum4(x: Int, y: Int, maybe: Int = 1) -> Int {
return x + y + maybe
}
let params4 = (1, 5, 1)
let params4_1 = (1, 5, maybe: 1)
sum4(1, 5, maybe: 1)
sum4(1, 5)
@kostiakoval
kostiakoval / Tuple_Rule-2.swift
Created March 8, 2015 18:13
Tuple as Function argument Rule-2
func sum(x: Int, y: Double) {} // no names (,)
let params = (1, 1.0) //no names
sum(params) // works fine
func other(# x: Int, # y: Double) // name (x, y)
other(params) //Fails
let paramsOther = (x: 1, y: 1.0) //name (x, y)
other(paramsOther) //Works!
@kostiakoval
kostiakoval / Tuple_Rule-1.swift
Last active August 29, 2015 14:16
Tuple as function arguments
func sum(x: Int, y: Double) {} // type (Int, Double)
let params = (1, 1.0) //type (Int, Double)
let paramsWrongOrder = (1.0, 1) //type (Double, Int)
let oneParam = (1) //type (Int)
sum(params) // works fine
sum(oneParam) //Fails
sum(paramsWrongOrder) //Fails
func other(x: Int, y: String) {} // type (Int, String)
@kostiakoval
kostiakoval / TupleAndFunction.swift
Last active August 29, 2015 14:16
Use Tuple as function arguments
// Playground - noun: a place where people can play
// Rename file to TupleAndFunction.playground
import Cocoa
func sum(x: Int, y: Int) -> Int {
return x + y
}
func sum1(# x: Int, y: Int) -> Int {
return x + y