Xcode code formatter Swift
I'm trying to auto format my code written in Swift. It's necessary if you're working in team and using CVS and don't want to merge for hours.
Ctrl+I just makes an indent, but for example i want to have proper space between functions and just all the stuff like in Clang.
The reformat code command in xcode is Ctrl-I, not Cmd-I and it appears to work just fine.
I write a extension app about format Swift code: Swimat
There has two way to install
brew cask install swimat
Clone this repo, build and restart Xcode.
See https://github.com/Jintin/Swimat for more information
In Xcode [Objective-C, Swift]:
1) ⌘+A: highlight code
2) ⌃+I: reformat code
⌃+i: reformat code
Ctrl+i: fix code indentation
Eventhough it has good IntelliSense, xcode sucks at code formatting generally.Wish it was better.
Apple has to spend so much effort in this in order to make it VS level good.
Xcode and Swift 2.0: Per file, Cmd-A then Ctl-I. Ctl-I works for what is highlighted, Cmd-A highlights all. Just be careful not to accidentally delete all your source after the Ctl-A!
SwiftFormat is a popular code formatter for swift. It has an extension for Xcode, which can be accessed from the Editor menu.
Quote from the SwiftFormat readme:
SwiftFormat is a code library and command-line tool for reformatting
swift code on macOS or Linux.
SwiftFormat goes above and beyond what you might expect from a code
formatter. In addition to adjusting white space it can insert or
remove implicit self, remove redundant parentheses, and correct many
other deviations from the standard Swift idioms.
(I know the question says Xcode but)
personally this is another reason to use AppCode alongside Xcode.
While Xcode only fixes the indentation for Swift code, as mentioned in the question, AppCode at least handles spaces too. Somewhat relatedly it removes indents from empty lines by default.
AppCode's Swift support definitely isn't perfect yet and you still need to check the code in both, but it's still worth it because of better navigation and things like this.
The command in xcode is ctrl-i to format code, not cmd-i.
I would suggest using sublime text with the swift plugin, which will allow you to hotkey the code formatting.
https://sublime.wbond.net/packages/Swift
Select lines, then press Ctrl+I.
Related
I'm a total beginner in Swift, so I always find the quick help in Swift 2.2, Xcode 7 very useful(You hold down option key and click on something and the Xcode would give you a pop-up window containing all sorts of information about the variable you are clicking on). However, after I updated to Swift 3.0 the pop-up window becomes blank and a message "No Quick help". I'm wondering if someone could tell me how to bring the quick help back, thank you!
This is an Xcode question. Quick Help only works with Swift when you don't have any errors before or in the current line you want to open the Quick Help from.
I also experienced this and it seems this feature worked much better with older Xcode versions. But I guess this has to do with Swift because it is such a fast growing programming language. The integrated interpreter of Xcode isn't able to keep up with it.
To work around it, just comment out all failing lines of code and then comment in one line by one while fixing the errors. Sometimes it is helpful to also clean up the generated project files. And if this also doesn't work, just restart Xcode.
The last step is often necessary when you're in a Playground.
Basically its not auto completing the code. i have also deleted the derived data of Xcode than also Auto code is not working.
it should work like this
But it is working like this
It's not a bug, it's a feature. From the release notes:
Code completion enhancements in the Xcode source editor help you enter symbols, methods, and property names with less typing. Code completion now provides more intelligent suggestions by using partial matches and the first letter of each word, in addition to prefix matching.
I want to ask you if there is any code arrangement functionality in XCode 7+?
I ran into a situation where I want to make my code more readable and organized. For this I have to (re-)sort some functions and elements (Swift) inside some source code files.
Being also an Eclipse and IntelliJ user I am used to have a couple of options, like rules-based auto-sorting or an outline view where I can move functions etc. around. Haven't seen something like this in XCode so far - sure, there is an "outline view" (aka. Symbol Navigator), but it is really only a viewer. -.-
I also know about the CMD+Opt+[ / ] shortcuts but this kind of manual sorting is tedious.
Is there a better way? Thank you.
Martin
As of Xcode 7.3, there is no better way of doing what you want. As an IntelliJ user, you may want to try AppCode by JetBrains.
Is there a program that can take a .storyboard file, and convert it into Swift code? An example being: If I have a UIViewImage element in my storyboard can the program identify the element and convert it into swift, then insert that code into my viewcontroller.swift file? (Just an example.)
AFAIK, no.
There's this rather old nib2objc tool that convert from NIBs to Objective C code. But no Storyboard -> Swift tool that I know of.
Searching for the same thing, I discovered this tool and ran the demo on one of my Xcode 10-14 era storyboard files. It seems to work as advertised, including having an interactive mode that allows highlighting sections to find what corresponds:
https://www.microcodingapps.com/
Disclaimer: I have no connection with this app or whoever makes it and have only taken a cursory look at it so far, but I was impressed enough with what I saw to come back to this SO article and add an answer.
Is there any way to have Vim key bindings in Xcode?
If you still interested in Vim keybinding plugin for Xcode I made one. Here it is.
http://programming.jugglershu.net/softwares/xvim.html
This is currently developed for personal (my) use. So you may feel bad with some lack of implementation. Give me a feed back(feature request) then. I'll add some keybinds if I have enough time.
#pkamb's answer is correct, but slightly out of date. In the Xcode 13 GM, Apple tweaked how to enable Vim keybindings.
To improve access to the Vim Mode, Xcode replaced the Enable Vim key bindings preference with an Edit > Vim Mode menu item. (75491567)
The next best thing is JetBrains' excellent Objective-C IDE AppCode. They have a great Vim plugin called IdeaVIM that is actively maintained.
I use MacVim as editor instead xcode. For code completion i use vim plugin named clang_complete - awesome plugin.
Xcode 13 in 2021 has added native Vim key binding support:
Xcode > Preferences > Text Editing > Editing > Enable Vim key bindings
https://developer.apple.com/xcode/
Vim mode
Many common key combinations and editing modes familiar to Vim users are supported directly within the code editor, using the new bottom bar to show mode indicators.
I myself use and really appreciate Shu's XVim, but for completeness and in case you're still interested, just recently viemu has opened its beta for Xcode, you can check it out at
http://www.viemu.com/blog/2013/05/01/viemu-for-xcode-public-beta-available/
I haven't tried it yet, but there's also $20 ViCiOUS. Right now, I use Shu's XVim plugin (cf. his answer), and recommend it.
ViEmu is finally available for Xcode, check it out. It uses the same vim emulation engine that they are using for their Visual Studio plug-in, so all the polishing that has been added to the product since 2005 is there.
I'm not affiliated to them, but I tested ViEmu and if it was available for Xcode 5, I would have paid for a license.
Try Editor -> Vim Mode in Xcode =>13.
It supports most of the common key bindings but there are exceptions, which is understandable given it's a relatively new feature. Most notably for me is the lack of support for a .vimrc file so if you want that (or just want more features) you can still use https://github.com/XVimProject/XVim2. Judging by the current maintainer it might eventually be made redundant by Xcode's default vim support but it still works well for me.