开发者

How do I deal with typing nested message calls in ObjC source from only the keyboard?

开发者 https://www.devze.com 2022-12-13 07:18 出处:网络
I think over the last year and a half, while experimenting with Cocoa and Cocoa Touch development, I\'ve been struggling with getting Xcode to support a kind of key macro to handle this specific codin

I think over the last year and a half, while experimenting with Cocoa and Cocoa Touch development, I've been struggling with getting Xcode to support a kind of key macro to handle this specific coding scenario...

I frequently find myself not knowing ahead of time how many message dispatches to chain together while typing a new line of code. For example, I might type this... (Pipe character represents cursor location)

someVariable = [someObject someMessage]|

...I then realize that I need to chain a message from the returned object (since messaging nil is safe in ObjC, this is a common idiom.) So I would need to change the line of code to look like this...

someVariable = [[someObject someMessage] |

The big problem is having to prepend a left bracket before the bracket pair I'm at the end of, which would entail using the mouse or lots of repeated key tapping each time I needed to do this. After wasting about 40 or so hours off and on trying to find any kind of pre-existing keyboard shortcut or script (I investigated Perl, shell scripting, and even a bit of emacs Lisp scripting in my Googling.), I hacked together this monstrosity in AppleScript, of all things...

tell application "System Events"
    delay 0.2
    set editMenu to menu "Edit" of menu bar item "Edit" in menu bar 1 of
         process "Xcode"
    set kLeftArr开发者_Go百科owKey to 123
    key code kLeftArrowKey
    set theMenu to menu item "Balance" in menu "Format" of menu item
         "Format" in editMenu
    click theMenu
    set theMenu to menu item "Bracket Expression" in menu "Objective C"
         of menu item "Objective C" in menu "Insert Text Macro" of menu
         item "Insert Text Macro" in editMenu
    click theMenu
    key code kLeftArrowKey
end tell

There has got to be a better way.

I count at least five problems with the above... (which I'll comment on if asked.) I see frequent online complaints from new ObjC developers all the time about this syntax, and I suspect this one typing issue is the major sore point. While I like how the syntax reads (so much so that I avoid the dot notation for properties out of coding consistency), how it's first written kind of sucks. (Just like AppleScript, ironically enough, given my current hacked implementation.)

Help me end my suffering, and you'll probably end the suffering of a lot of other ObjC developers as well...


What version of Xcode are you using? In my version, left brackets are prepended automatically if I type a right-bracket after a valid selector name.

In Xcode 3.2, go to Preferences, then "Text Editing", then choose "Insert opening bracket".


If you're stuck on pre-Snow Leopard, you might try using TextMate as your editor. It will automatically insert matching braces and supports versions of OS X back to 10.4. It's a tad bit pricey (less so than BBEdit, but more than Xcode, which is free), but it's a really nice editor just the same.


Prior to Xcode 3.2, I had to deal with this same problem frequently. Something that eased the pain for me was using the Option key with the left/right arrows to skip by word. Much better than waiting for it to skip by letter.

0

精彩评论

暂无评论...
验证码 换一张
取 消