Skip to content

Instantly share code, notes, and snippets.

View DisappearPing's full-sized avatar

Disappear Ping DisappearPing

  • Taoyuan,Taiwan R.O.C
View GitHub Profile
@DisappearPing
DisappearPing / 關於遞迴
Last active December 24, 2015 08:32
非常重要
遞迴是程式設計師的最愛也最恨
愛的是簡潔
恨的是出包往往在無形之間(Stack Overflow)
以及效率非常緩慢
所有的遞迴寫法一定可以用迴圈取代
但換來的就是一連串冗長的程式碼了
L. Peter Deutsch 程式設計師說過一段經典名句
let sortedTradingRecords = tradingRecords.sort { $0.time.compareAsNSDate < $1.time.compareAsNSDate }
@DisappearPing
DisappearPing / GetYTIdFromURL.swift
Last active March 10, 2016 06:51
String extension for getting Youtube id from URL
import Foundation
/*
this extension cover these case
www.youtube.com/v/id
www.youtube.com?v=vid
http://www.youtube.com/watch?v=id&feature=youtu.be
http://www.youtube.com/watch?v=id
youtube.be/id
@DisappearPing
DisappearPing / StringAndDateExchangeExtension.Swift
Last active March 16, 2016 04:09
string -> define date -> define string -> string
import Foundation
// string -> define date -> define string -> string
extension NSDate {
var formattedString: String{
let formatter = NSDateFormatter()
// ex: 12.04 -> NSDate
formatter.dateFormat = "MM.dd"
formatter.timeZone = NSTimeZone(name: "Asia/Taipei")
@DisappearPing
DisappearPing / AttributesString.Swift
Last active March 29, 2016 08:10
AttributesString
//One attributed string method
let theInfoTitleString = "曼麗卡說明 Manli Card\n\n"
let theInfoTitleAttribute = [NSFontAttributeName: UIFont.systemFontOfSize(22), NSUnderlineStyleAttributeName: 1]
let theInfoDetailString = "是大魯閣草衙道專屬會員卡,更是全台首創結合購物集點、小額付款的一卡通會員卡,讓您一卡在手、暢遊草衙道!"
// let theAllInfoString = theInfoStringTitle + "\n" + theInfoStringDetail
let theInfoTitleAttributedString = NSMutableAttributedString(string: theInfoTitleString, attributes: theInfoTitleAttribute)
let theInfoDetailAttributedString = NSAttributedString(string: theInfoDetailString)
//產出用zip為key找 縣市/鄉鎮市區
println(zipJson?.dictionaryValue.keys.array)
println(zipJson?.dictionaryValue.values.array)
for var i = 0; i < zipJson?.dictionaryValue.keys.array.count ; i++ {
let first = zipJson?.dictionaryValue.keys.array[i]
let second = zipJson?.dictionaryValue.values.array[i]
let third = zipJson?.dictionaryValue.values.array[i].dictionaryValue
@DisappearPing
DisappearPing / tw_zipcode.json
Created March 30, 2016 06:46
台灣郵遞區號
{
"基隆市": {"仁愛區": "200", "信義區": "201", "中正區": "202", "中山區": "203", "安樂區": "204", "暖暖區": "205", "七堵區": "206"},
"台北市": {"中正區": "100", "大同區": "103", "中山區": "104", "松山區": "105", "大安區": "106", "萬華區": "108", "信義區": "110", "士林區": "111", "北投區": "112", "內湖區": "114", "南港區": "115", "文山區": "116"},
"新北市": {
"萬里區": "207", "金山區": "208", "板橋區": "220", "汐止區": "221", "深坑區": "222", "石碇區": "223",
"瑞芳區": "224", "平溪區": "226", "雙溪區": "227", "貢寮區": "228", "新店區": "231", "坪林區": "232",
"烏來區": "233", "永和區": "234", "中和區": "235", "土城區": "236", "三峽區": "237", "樹林區": "238",
"鶯歌區": "239", "三重區": "241", "新莊區": "242", "泰山區": "243", "林口區": "244", "蘆洲區": "247",
"五股區": "248", "八里區": "249", "淡水區": "251", "三芝區": "252", "石門區": "253"
},
@DisappearPing
DisappearPing / zipKVReverse.json
Created March 30, 2016 06:47
用台灣郵遞區號當Key查鄉鎮市區
{
"929" : [
"琉球鄉",
"屏東縣"
],
"508" : [
"和美鎮",
"彰化縣"
],
"884" : [
@DisappearPing
DisappearPing / customNavigationBarTitleViewFunction.swift
Created July 1, 2016 08:51
simple function for customNavigationTitleView with imageview
func setupCustomTitleViewAndCleanTitle(toTheNavigationItem theNavigationItem: UINavigationItem){
let titleView = UIView(frame: CGRect(x: 0, y: 0, width: 128, height: 44))
//Customizing Navigation Bar
// 3
let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 128, height: 44))
imageView.contentMode = .ScaleAspectFit
// 4
let image = UIImage(named: "nav_logo")
imageView.image = image
// 5
@DisappearPing
DisappearPing / FindCurrentViewcontroller.swift
Created September 2, 2016 09:51
Find CurrentViewcontroller (or TopViewController) in swift
extension AppDelegate {
func findCurrentViewController() -> UIViewController{
let rootVC = UIApplication.sharedApplication().keyWindow?.rootViewController
return findCurrentViewController(byTempTopVC: rootVC!)
}
func findCurrentViewController(byTempTopVC vc: UIViewController) -> UIViewController {
let presentedVC = vc.presentedViewController