posted in Others 

写着写着代码,终于忍受不了在Xcode、Emulator、Chrome、Dash等应用中切来切去的麻烦。Dock栏可不是那么好点的。于是花了一段时间查找Mac上有哪些App可以实现快捷键切换应用程序。
最后,终于挑定了一款应用的时候,突然想起来 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){
        self.id = id
    }
    
    required init? (coder aDecoder: NSCoder){
        self.id = aDecoder.decodeIntegerForKey(Book.KeyId)
        self.name = aDecoder.decodeObjectForKey(Book.KeyName) as? String
        self.author = 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)
book.name = "The first book"
book.author = "IMLC.ME"

let book2 = Book(id:2)
book.name = "The second book"
book.author = "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 \(restoredBook.name!) 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