Skip to content

Instantly share code, notes, and snippets.

View KrisYu's full-sized avatar

Xue Yu KrisYu

View GitHub Profile
@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)
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 / 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")
@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 / 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 / kindnotes.py
Created June 22, 2017 18:44
use on Mac,split My Clippings.txt by booktitle ( 4 year old script
#filename: kindlesplit.py
f=open('My Clippings.txt').read()
notes = f.split('==========\r\n')
lines = []
booktitle = []
lines = notes[:]
for i in range(len(lines)):
lines[i]=lines[i].split('\r\n')
booktitle.append(lines[i][0])
import Cocoa
import PlaygroundSupport
PlaygroundPage.current.needsIndefiniteExecution = true
if let url = URL(string: "https://api.github.com/users/octocat") {
URLSession.shared.dataTask(with: url){ data, response, error in
import Cocoa
import PlaygroundSupport
PlaygroundPage.current.needsIndefiniteExecution = true
struct iTunesRequestManager {
static func getSearchResult(_ query: String, results: Int, langString: String, completionHandler: @escaping ([[String: AnyObject]], NSError?) -> Void){
var urlComponents = URLComponents.init(string: "https://itunes.apple.com/search")
@KrisYu
KrisYu / URLSession_Download_File.swift
Created July 9, 2017 05:03
URLSession download files
func downloadFile(url: URL) {
let downloadRequest = URLRequest(url: url)
URLSession.shared.downloadTask(with: downloadRequest) { location, response, error in
// To do check resoponse before saving
guard let tempLocation = location, error == nil else { return }
let documentDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).last
do {
let fullURL = try documentDirectory?.appendingPathComponent((response?.suggestedFilename!)!)
try FileManager.default.moveItem(at: tempLocation, to: fullURL!)
print("saved at \(String(describing: fullURL)) ")