posted in Others 

最后,终于挑定了一款应用的时候,突然想起来 Alfred 来。于是尝试了一下,效果完美。

Alfred 无愧神器之名:)

Step 1: Create Blank Workflow

Add a blank workflow, fill the name, description and other field Alfred required.

Step 2: Add Hotkey Trigger

Click the "+" button on the upper right, select the Trigger -> Hotkey. Now a new window shows, type the Hotkey you want to use.

Step 3: Add Action

Click the "+" button, this time select the Action -> Launch Apps/ Files. Drop the app you want to open from Finder to Alfred.

Step 4: Link the Trigger to the Action

Link the hotkey trigger to the launch apps action. Done!

A workflow to trigger the chrome and xcode

最终效果是,在 Chrome 还没有打开的情况下,将启动 Chrome。如果 Chrome 已经启动,则切换到 Chrome。

posted in iOS 

It's necessary to convert custom object to NSData when using NSUserDefaults or NSKeyedArchiver. Furthermore, a dictionary or an array with custom class.

Here shows steps to do it.

1. Define your class
class Book: NSObject, NSCoding {
    var id:Int
    var name:String?
    var author:String?
    private static let KeyId = "key_id"
    private static let KeyName = "key_name"
    private static let KeyAuthor = "key_author"
    init(id: Int){ = id
    required init? (coder aDecoder: NSCoder){ = aDecoder.decodeIntegerForKey(Book.KeyId) = aDecoder.decodeObjectForKey(Book.KeyName) as? String = aDecoder.decodeObjectForKey(Book.KeyAuthor) as? String
    func encodeWithCoder(aCoder: NSCoder) {
        aCoder.encodeInteger(id, forKey: Book.KeyId)
        aCoder.encodeObject(name, forKey: Book.KeyName)
        aCoder.encodeObject(author, forKey: Book.KeyAuthor)

You must extend your class from NSObject and implement the NSCoding protocal. Thus, you get init(coder:) and encodeWithCoder(aCode:)

2. Save data
let book = Book(id: 1) = "The first book" = "IMLC.ME"

let book2 = Book(id:2) = "The second book" = "IMLC.ME"

let books = [book, book2]

let ud = NSUserDefaults.standardUserDefaults()
ud.setObject(NSKeyedArchiver.archivedDataWithRootObject(book), forKey: "book")
ud.setObject(NSKeyedArchiver.archivedDataWithRootObject(book), forKey: "book-array")

The code above shows how to save book and books (an array) into NSUserDefaults.

3. Restore data
if let bookData = ud.objectForKey("book") as? NSData,
    let bookArrayData = ud.objectForKey("book-array") as? NSData{
    let restoredBook = NSKeyedUnarchiver.unarchiveObjectWithData(bookData) as? Book
    let restoredBookArray = NSKeyedUnarchiver.unarchiveObjectWithData(bookArrayData) as? [Book]
    print("Restored book \(!) and book array [\(restoredBookArray[0].name!),\(restoredBookArray[1].name!)]")

Saving and loading data with swift dictionary is very similar to array, I wouldn't give an another sample here.

TAG: Swift, NSData, Convert, Custom class