Slide 1

Slide 1 text

Xcode Extensions 2016/6/23 potatotips #30 @y_koh

Slide 2

Slide 2 text

ࣗݾ঺հ • ߴ(͜͏) Ҹ⅑ • @y_koh • ҿ৯ళ޲͚༧໿୆ா
 τϨλ

Slide 3

Slide 3 text

Xcode Extensions

Slide 4

Slide 4 text

New Feature of Xcode 8

Slide 5

Slide 5 text

Active Line Highlight

Slide 6

Slide 6 text

Swift Color Literals

Slide 7

Slide 7 text

Swift Image Literals

Slide 8

Slide 8 text

Documentation Generation

Slide 9

Slide 9 text

Xcode Source Editor Extensions

Slide 10

Slide 10 text

How to Develop

Slide 11

Slide 11 text

1. File - New - Project… 2. OS X - Application - Cocoa Application 3. File - New - Target... 4. OS X - ApplicationExtensions - Xcode Source Editor Extension 5. Activate "APP_NAME" scheme?

Slide 12

Slide 12 text

• SourceEditorExtension.swift • SourceEditorCommand.swift

Slide 13

Slide 13 text

import Foundation import XcodeKit class SourceEditorExtension: NSObject, XCSourceEditorExtension { /* func extensionDidFinishLaunching() { // If your extension needs to do any work at launch, implement this optional method. } */ /* var commandDefinitions: [[XCSourceEditorCommandDefinitionKey: AnyObject]] { // If your extension needs to return a collection of command definitions that differs from those in its Info.plist, implement this optional property getter. return [] } */ }

Slide 14

Slide 14 text

import Foundation import XcodeKit class SourceEditorCommand: NSObject, XCSourceEditorCommand { func perform(with invocation: XCSourceEditorCommandInvocation, completionHandler: (NSError?) -> Void ) -> Void { // Implement your command here, invoking the completion handler when done. Pass it nil on success, and an NSError on failure. completionHandler(nil) } }

Slide 15

Slide 15 text

import Foundation import XcodeKit class SourceEditorCommand: NSObject, XCSourceEditorCommand { func perform(with invocation: XCSourceEditorCommandInvocation, completionHandler: (NSError?) -> Void ) -> Void { // ExtensionΛ࣮ߦͨ࣌͠ʹ։͍͍ͯΔϑΝΠϧ಺༰ // NSMutableArray - // __NSCFString, NSTaggedPointerString, ... for line in invocation.buffer.lines { debugPrint(line) } // ExtensionΛ࣮ߦͨ࣌͠ʹબ୒͍ͯ͠ΔҐஔ৘ใ // NSMutableArray - XCSourceTextRange for selection in invocation.buffer.selections { debugPrint(selection) } completionHandler(nil) } }

Slide 16

Slide 16 text

import Foundation import XcodeKit class SourceEditorCommand: NSObject, XCSourceEditorCommand { func perform(with invocation: XCSourceEditorCommandInvocation, completionHandler: (NSError?) -> Void ) -> Void { let lineCount = invocation.buffer.lines.count for i in 0...(lineCount-1) { let line = invocation.buffer.lines[i] let newLine = "\(String(line).trimmingCharacters(in: CharacterSet.newlines)) // " invocation.buffer.lines.replaceObject(at: i, with: newLine) } completionHandler(nil) } }

Slide 17

Slide 17 text

How to Run

Slide 18

Slide 18 text

1. Target > My Mac - Run 2. Choose an app to run > Xcode Beta 3. Dark Xcode 4. Open Project 5. Menu > Editor > ProjectName > ExtensionName

Slide 19

Slide 19 text

import Foundation import XcodeKit class SourceEditorCommand: NSObject, XCSourceEditorCommand { func perform(with invocation: XCSourceEditorCommandInvocation, completionHandler: (NSError?) -> Void ) -> Void { let lineCount = invocation.buffer.lines.count for i in 0...(lineCount-1) { let line = invocation.buffer.lines[i] let newLine = "\(String(line).trimmingCharacters(in: CharacterSet.newlines)) // " invocation.buffer.lines.replaceObject(at: i, with: newLine) } completionHandler(nil) } }

Slide 20

Slide 20 text

import Foundation // import XcodeKit // // class SourceEditorCommand: NSObject, XCSourceEditorCommand { // // func perform(with invocation: XCSourceEditorCommandInvocation, completionHandler: (NSError?) -> Void ) -> Void { // let lineCount = invocation.buffer.lines.count // for i in 0...(lineCount-1) { // let line = invocation.buffer.lines[i] // let newLine = "\(String(line).trimmingCharacters(in: CharacterSet.newlines)) // " // invocation.buffer.lines.replaceObject(at: i, with: newLine) // } // // completionHandler(nil) // } // // } //

Slide 21

Slide 21 text

Summary • ࠓग़དྷΔ͜ͱ͸։͍͍ͯΔϑΝΠϧΛεΫϦϓτʹᷰΘͤͯԿ ͔Λॲཧ͢ΔɻඞཁͰ͋Ε͹݁ՌΛฦ͚ͩ͢ɻ • ͔͠͠ग़དྷΔ͜ͱ͕গͳ͗͢Δɻɻ • Swift Color LiteralsΈ͍ͨʹύϨοτΛදࣔͨ͠ΓɺΤσΟλͷ දࣔΛม͑Δ͜ͱ͸Ͱ͖ͳ͍ • Xcode editor extensionͱ͍͏ΤσΟλͷ֎ଆͷextension΋͋Δ Β͍͠ɻ͚ΕͲ৘ใݟ౰ͨΒͣɻɻ • ͱ͸͍͑ࠓޙ֦ॆ͞Ε͍ͯ͘ͱࢥ͏ͷͰΞοϓσʔτʹظ଴ʂ

Slide 22

Slide 22 text

Reference • https://developer.apple.com/videos/play/ wwdc2016/414/ • Xcode Source Editor ExtensionΛࢼ͢
 http://qiita.com/_tid_/items/ 271c1973b3043f6c55cb • http://www.russbishop.net/xcode-extensions