Created
July 10, 2014 09:57
-
-
Save asus4/a400ed14310c3cf9d05f to your computer and use it in GitHub Desktop.
My Swift Snipets ref: http://qiita.com/asus4/items/15f843c39929aedd3013
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let date = NSDate() | |
let dateFormatter = NSDateFormatter() | |
dateFormatter.setDateFormat("yyyy-MM-dd-HH-mm-ss") | |
let dateStr = dateFormatter.stringFromDate(date) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
typedef void (^JsonDelegate)(NSDictionary json); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class TheDelegteSample { | |
// 定義 | |
typealias JsonDelegate = (json:NSDictionary) -> () | |
// 変数 | |
var jsonDelegate:JsonDelegate? | |
// 叩く | |
func foo() { | |
if jsonDelegate { | |
jsonDelegate(json:theJson) | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// デスクトップから取得。構造体になってるので注意 | |
let directory = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DesktopDirectory, NSSearchPathDomainMask.UserDomainMask, true) | |
let directoryStr = directory[0] as NSString | |
let path = NSString(format: "%@/%@.txt", directoryStr, "thefile") | |
let url = NSURL(fileURLWithPath:path) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// dispatch_get_main_queueは廃止 | |
// 5秒後に | |
let offset = 5.0 | |
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64(offset * Double(NSEC_PER_SEC))),dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0),{ | |
// TODO dispatch | |
}) | |
// Background Task | |
let queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); | |
dispatch_async(queue, { | |
// Background | |
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Cocoa | |
class SoundManager { | |
struct Cache { | |
static let sound = Dictionary<String, NSSound>() | |
} | |
init() { | |
} | |
class func getSound(name:String) -> NSSound { | |
var sound = Cache.sound[name] | |
if sound == nil { | |
let soundpath = NSBundle.mainBundle().pathForResource(name, ofType:"aif") | |
sound = NSSound(contentsOfFile: soundpath, byReference: false) | |
} | |
return sound! | |
} | |
class func play(name:String) { | |
let sound = SoundManager.getSound(name) | |
sound.play() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment