Xcode 8已經(jīng)發(fā)布2天了父腕,并且beta版本已經(jīng)可以下載泽台,迫不及待的看了官方文檔,終于還是被我看到了一個(gè)讓人興奮的特性:Xcode Source Editor Extensions
介紹
New Xcode extensions for the source editor let you customize your coding experience. Use extensions to navigate within your editor’s text, and select, modify, and transform your code. Bind your favorite extensions to a keyboard shortcut to make common reformatting tasks a snap. Xcode includes a new template so you can easily create editor extensions and distribute them on the Mac App Store, or sign your extensions with your Developer ID to share them online. And because extensions run in a separate process, Xcode stays safe and stable.
備注:為了不丟失翻譯過(guò)程中的準(zhǔn)確度谜洽,上述文字不翻譯悍抑,挺容易懂得
準(zhǔn)備
- 下載Xcode 8
- 安裝additional system components(Xcode 會(huì)自動(dòng)處理,不用手動(dòng))
- 安裝完畢后, 打開(kāi)terminal开呐,然后執(zhí)行命令sudo /usr/libexec/xpccachectl
- 重啟電腦烟勋,這個(gè)只需要一次(意味著以后開(kāi)發(fā)的Extension不需要了)
起航
一、創(chuàng)建一個(gè)Project筐付,并且選中OS X - Application - Cocoa Application
二卵惦、第1步結(jié)束之后會(huì)創(chuàng)建一個(gè)Mac OS程序,我們完全不用管它瓦戚。選中Targets沮尿,點(diǎn)擊底部的“+”,然后繼續(xù)選擇OS X - Application Extensions - Xcode Source Editor Extentsion
三较解、接下來(lái)我們會(huì)得到2個(gè)文件畜疾,其中SourceEditoCommand.swift就是我們需要處理的。
四印衔、 在SourceEditorCommand.swift愉快的編寫(xiě)你的代碼吧啡捶!
五、編寫(xiě)好你的代碼之后奸焙,運(yùn)行extension scheme瞎暑,當(dāng)彈出"Choose an app to run"菜單時(shí),選擇Xcode 8与帆,打開(kāi)任意源文件了赌,你的插件就會(huì)在"Editor"菜單出現(xiàn)。
實(shí)戰(zhàn)
大坑
- Xcode 8這個(gè)新特性出來(lái)后玄糟,以前所有能在Xcode 7及以下版本運(yùn)行的插件都用不了了勿她,非常悲劇的消息!
- Xcode Source Editor Extension必須依賴(lài)某一個(gè)Mac OS程序存在茶凳,不像以前的插件一樣獨(dú)立存在嫂拴!
- 在Editor菜單下有時(shí)看不到我們開(kāi)發(fā)好擴(kuò)展菜單播揪!