Skip to content

Instantly share code, notes, and snippets.

View KrisYu's full-sized avatar

Xue Yu KrisYu

View GitHub Profile
@KrisYu
KrisYu / observer.swift
Created March 13, 2017 20:43 — forked from starhoshi/observer.swift
Swift observer pattern.
import Foundation
protocol Observer {
var id: String { get }
func update(_ string: String)
}
extension Observer {
func update(_ string: String) {
print("\(type(of: self)) に届いた新しい値は \(string) です。")
@KrisYu
KrisYu / observer.swift
Created March 13, 2017 20:43 — forked from starhoshi/observer.swift
Swift observer pattern.
import Foundation
protocol Observer {
var id: String { get }
func update(_ string: String)
}
extension Observer {
func update(_ string: String) {
print("\(type(of: self)) に届いた新しい値は \(string) です。")
@KrisYu
KrisYu / RW.swift
Created February 14, 2017 00:09 — forked from mchirico/RW.swift
RW to file in swift
//
// RW.swift
// Web
//
// Created by Mike Chirico on 10/15/15.
// Copyright © 2015 Mike Chirico. All rights reserved.
//
import UIKit
@KrisYu
KrisYu / postRequest.swift
Last active August 13, 2016 19:15
Simple GET and Parse JSON data with dataTaskWithRequest
let firstTodoEndpoint: String = "http://jsonplaceholder.typicode.com/todos/1"
let firstTodoUrlRequest = NSMutableURLRequest(URL: NSURL(string: firstTodoEndpoint)!) firstTodoUrlRequest.HTTPMethod = "DELETE"
let session = NSURLSession.sharedSession()
let task = session.dataTaskWithRequest(firstTodoUrlRequest) { (data, response, error) in
guard let _ = data else {
print("error calling DELETE on /todos/1")
return
}
print("DELETE ok")
func printData(){
guard let path = NSBundle.mainBundle().pathForResource("contacts", ofType: "txt") else {
print("Error while reading data")
return
}
do {
let content = try String(contentsOfFile: path, encoding: NSUTF8StringEncoding)
print(content)
@KrisYu
KrisYu / gist:6bbd0c184c6a3754d46e
Created March 18, 2016 01:27
iOS reading txt file
func printData(){
guard let path = NSBundle.mainBundle().pathForResource("contacts", ofType: "txt") else {
print("Error while reading data")
return
}
do {
let content = try String(contentsOfFile: path, encoding: NSUTF8StringEncoding)
print(content)