I love the "Organize Imports" command in Eclipse to implicitly add and remove classes imported into a source file (as in Java or ActionScript).
Is there a command in Xcode to update the #import directives at the top of.m Objective-C files based on the classes referenced within the file?
You can do this by creating an Automator action and use that in Xcode as well as everywhere in Mac OS X. To do that, do the following:
Start Automator -> New
Choose "Quick Action" (or "Service" on older MacOS/Automator versions)
add a "run shell script" action
use sort | uniq as the script and check the "output replaces selected text" checkbox
save and give it a name (e.g. "sort & unique")
check "Output replaces selected text"
After you saved it, you can just select your imports in Xcode, right click and choose your "sort & unique" action to organize your imports.
This is not as good as the organize import actions in Eclipse or IntelliJ, because it doesn't removes unused stuff etc. but it's better than nothing.
PS: Got that from WWDC 2012 Sessions - Session 402 Working Effeciently with Xcode (starting at 6:15)
EDIT
I started using AppCode, the Objective-C IDE from JetBrains, and it has features like "organise imports". You should check it out: http://www.jetbrains.com/objc/.
I'm resolving this as not currently a feature of Xcode :(
I've found Cedar Shortcuts to at least be usable. It's not as good as Eclipse's import feature (it doesn't organize imports), but it can keep you from having to go to the top of a class file and typing an import statement. With this plugin you just place the cursor on the class that needs imported and press Ctrl + Option + I. Here's the github project. https://github.com/cppforlife/CedarShortcuts
I made a small Xcode plugin to sort the headers and remove duplicates of the file with a shortcut, you can check it out! - https://github.com/insanoid/CleanHeaders-Xcode
AppCode from Jetbrains can do this.
Yup. Like in Eclipse, this would be an awesome feature since developing in XCode (Cocoa Touch) does require quite a bit of class imports which are not easy to remember and Android development in Eclipse sorts this out with a simple keystroke combination that saves so much time ! Hope there is a way to do this in XCode soon !
WordService (freeware) from Devon Technologies, provides a service that can be used in any Cocoa app that can (amongst others) sort lines.
Alternatively, you can use an Xcode Extension, such as Imp
Swiftlint has an opt-in rule which if opted, will automatically sort the imports alphabetically.
Add - sorted_imports to your .swiftlint.yml file under opt_in_rules.
Run swiftlint autocorrect terminal command on project root directory (same where swiftlint.yml is stored).
Related
I'm working on a project and went to add a new file, but the Finder window is different than the usual window when adding a file. When I go File->New File->Cocoa Touch Class->Set up name, superclass and language, I get this:
This is only the case for Objective-C files. For Swift files I get the usual window where I can choose the target membership and Group I want the file to be in
Well this sounds like a great bug to report at bugreport.apple.com . Not much we can do other than wait for the next beta and hope they solve it then.
I am using OS X 10.9 (Mavericks) and need to work on a Perl project. I enjoy using TextMate, Atom, and BBEdit, but would like to try using Xcode instead, as it has good integration with git, a clean look, and I am already intimately familiar with the UI and syntax highlighting colour scheme.
Is it possible to use Xcode as an IDE to develop and run Perl scripts, in a way that puts it on par, or ahead of, existing text editors?
Update
I used Xcode 5 as an IDE for Perl for more than a month, and found it an excellent alternative to regular text editors like Atom and TextMate. However, like pure text editors, Xcode lacks support for debugging in Perl. I have since discovered Komodo IDE, a really nice IDE for Perl (and similar languages) that supports graphical Perl debugging, plus remote Perl debugging. I have since switched from using Xcode for Perl development to using Komodo IDE.
After some experimentation, it seems that Xcode makes for a fairly decent environment for developing Perl. Here is a screenshot of Xcode showing some Perl, project navigator, Git integration, and command-line output from a Perl script, as run by Xcode.
Xcode 5's built-in syntax highlighting works fine with Perl (.pl and .pm) files, right out of the box. But to use Xcode to write Perl more efficiently, you'll want to set up a new Xcode project:
Create a new Xcode workspace (File > New > Workspace) and select the folder you want to use for the Perl project.
Enable the Xcode navigator, so you can see the files in your project (View > Navigators > Show Navigators). Notice that Xcode does not show you a list of files in the workspace folder by default. If you're reading this, you're probably already an Xcode user, already knew that.
Manually add any files, or folders (groups, in Xcode parlance), that you want to see in the project (right-click on the Project Navigator pane and select Add files to "Project Name). Create groups to mirror your folder structure and add any files in subfolders to the groups. This can be a bit of work, depending on the size of your Perl project, but once you're set up it should not change much.
Click on your files in the Project Navigator to view the code. If you are using a Git or Subversion, Xcode will generate diffs as normal in the Xcode version editor.
To get your Perl script running when you hit Cmd-R:
Create a new scheme (Product > Scheme > New Scheme), configure Target to None and assign a name like Run Perl.
After creating the scheme, hit Edit Scheme.
In the Run perl scheme, set the Executable to /usr/bin/perl (select Other then press Shift-Cmd-G, enter /usr/bin/perl, and press Choose).
Go to the Arguments tab and ensure that your main script is the first argument. Add more arguments and environment variables as necessary.
Go to the Options tab and set Use custom working directory to your project folder. Deselect XPC services and any other options related to iOS or OS X development.
Press OK and when you press Cmd-R in Xcode, Xcode will call Perl, run your script, and show you the output.
To get Perl snippets and templates in Xcode, see How to create project templates in Xcode 4.
Extra tips:
If you are working with files that have extensions for an language that Xcode does not recognise, such as .sql files, you may be able to use the generic syntax highlighting. Go to Editor > Syntax Highlighting > Generic.
I'm talking about a functionality such that you can just quickly open a .cpp file (C++ console application, nothing regarding Apple ecosystem), work on it, compile and run, without all the project creating mumbo-jumbo. When I open a file with code, all the "Product" options are greyed out for me but after some googling, I found posts claiming that Xcode 5 was to be the first one offering such functionality. Has it been implemented and it's just available elsewhere or were they false rumors and you still have to create a project and import a file there?
Or if you want a shortcut: CTRL + COMMAND + B.
In the top menu of XCode "File - Edit - View" you will see Product.
Click Product->Perform Action->Compile "myfile.xxx"
If you want a shortcut for that command, click XCode -> Preferences-> Key Bindings and set one up.
I created a new command line tool (the way to create a Mac OS X command line app) using Xcode 4.5, but I now want to add a framework to it. The frameworks list in the build phases is empty.
I can add .h/.m objective C class header and implementation files that I have written but I don't know how to make a "Command Line Tool" into what the old Xcode probably had a different template for; A command line application (not Cocoa app) which can access Core Foundation framework, or any other framework I choose to add to it.
Also, there used to be a "Core Foundation Tool" option in Xcode 3, where is it now?
(Update: I was clicking "+" inside Target Dependencies, which seemed sensible, but which gives me only an empty list of things to add. I figured there must be something somewhere else you had to change to make things show up in Target Dependencies, which if I may say, is not exactly clear to new users. Thus this public service message, question, and answer.
Hey Apple, how about you change this to "Link libraries or Frameworks" so that the word Framework shows up there. Making us guess if we're adding a library or a dependency when the only word we know about is Framework, is confusing.)
Go to the "Link Binary with Libraries" option and click +. It's found on the Build Phases page of your main Xcode project settings. You might want to collapse the Target Dependency and Compile Sources sections, so that the Link Binaries section is fully visible on your screen.
Footnote:
My mistake was clicking "+" in the Target Dependencies section, which was where I thought I might "add a dependency on CoreFoundation", which isn't what you do. I also found it confusing that you Link Binary with Libraries, when I thought that Xcode didn't use Framework and Library terms interchangeably, and in this case, confusingly. It also doesn't help that the plus icon is hidden and you have to scroll to access it, or else collapse the two top sections. Xcode is full of "User Interface is very confusing" areas, in my opinion:
Also, add #import <CoreFoundation/CoreFoundation.h> to your implementation (.m) files, as needed.
I find that in various situations Objective-C code in Xcode 3.1 (Leopard) can fail to get appropriate syntax coloring after typing or lose coloring that it had.
This isn't just a "refresh" issue with new custom symbols -- but affects Cocoa framework symbols as well.
Sometimes CMD-a to select all text on the code page will make the coloring (re)appear, sometimes double-clicking on a line to select it will work, sometimes I have to add/delete a space in a symbol to get that symbol to (re)color. Rebuilding, or closing/reopening the project may or may not work.
Is this a known issue with Xcode? For something so annoying to me, I'm not finding the plentiful discussions of it on SO and elsewhere I'd expect.
And is there any command to force global syntax recoloring?
1.) Go to Project --> Build Settings --> Header Search Paths
2.) Add "$(SRCROOT)/**"
3.) Close and reload the project
4.) Go to Xcode --> Window --> Organizer --> Projects --> $YourProject --> Delete Derived Data
5.) Wait a moment until XCode rebuild all indicies
To get more information activate verbose logging:
1.) Increase Xcode log level in Terminal app:
defaults write com.apple.dt.Xcode IDEIndexingClangInvocationLogLevel 3
2.) Open Console app. Search for "xcode"
3.) I saw "file not found" errors for header files mentioned in the pch file (therefore the fix above)
I had the same issue with a project that was fine in Xcode 3 and for which the syntax highlighting and code completion has broken in Xcode 4. It took me the better half of the day to figure it out but I found that the following steps will reproduce / fix the issue 100% for me.
After trying all the aforementioned solutions to no avail, I tried creating the project from scratch and reimporting every file and folder painstakingly. Every time I imported a couple of files I checked and syntax highlighting still worked. Even after the last few of them it still did. Then I hit build which failed because I forgot to include a couple of common header files in the project .pch file that were there in the old project. And that was when CodeSense broke again.
After further investigation we found that if you import a header file that in turn imports the same framework header file than the .pch already does, then CodeSense will break. The same setup did not cause any issues in Xcode 3.
Example:
project.pch
#import <Foundation/Foundation.h>
#import "projectConfig.h"
projectConfig.h
#import <Foundation/Foundation.h>
#import "one.h"
#import "two.h"
So by removing the wholly unnecessary
#import <Foundation/Foundation.h>
directive from the 'projectConfig.h' you can fix the highlighting and code completion will come back for you as well. If your issue is the same as mine that is.
Just select the file who lost the syntax coloring , press Editor > Syntax Coloring > ( choose your language (Objective c++ for ex) instead of 'Default'
I have found the best way to regain syntax coloring is just to quit Xcode and re-launch it. I couldn't tell you why, but that works every time.
I find that the following will often do the trick…
"Reselect" both "Base SDK" - and "Architecture" - in "Build Settings".
(CLick on blue project icon in top left, while in "Browser mode", aka ⌘1, then click project settings and mess with the stuff there.
Make sure they all line up / it doesn't hurt to do a little toggly-wogglying up in there, while you're in there… ya know just mix it up a bit. You'll notice xCode has a teensy-tiny mini-stroke - every time you switch between 64 and 32 bit… This is usually when I know if code highlighting is dead for the night, or if I'm good to go.
Often, I will have a project that gets highlighted instantly in 32 bit mode, and just sits there in 64… Who knows.. Ahh, Xcode, you wacky botch.
Select your Project -> Build Settings tab -> Apple LLVM Language 5.0 -> Precompile Prefix Header to YES. NSLog was not highlighting. Currently running Xcode 5.0.1 Hope it helps =) Thanks #rebelzach
I can confirm a 100% fix for this problem, based on a question I had asked (and actually answered), as pictured below. One thing i will add is that sometimes it is "errant whitespace" that can also contribute to the loss of highlighting. Either clean it up yourself, or use the Goggle Toolbox for Mac Xcode 4 plugin, which adds an entry in the XCode Edit menu called Clean Up Whitespace.
This appears to be a similar error from this question
I was able to correct this issue by setting Precompile Prefix Header to No in the build settings.
I recently had a similar issue, all coloring and autocomplete stopped working for built-in frameworks, my own classes still worked. Someone recommended to me that I turn off "Run Static Analyzer" in the build settings. It's weird, but it worked.
For me it was that I renamed my project, and Tests target was still pointing to the old pch location, but my App target was pointing to the new one.
If your app delegate still has code sense, this might be the case for you too. Go into project, select the 'Tests' target search 'prefix' and set it to the equivalent value in the 'app' target.
Open the project settings (Project > Edit Project Settings menu) and then click the Rebuild Code Sense Index button in the General tab. This will recreate the index that Xcode uses for syntax coloring.
This process can take a while for larger projects, so you can check the progress in the Activity window (Window > Activity menu).
Moreover, in XCode4 there is no button to rebuild the code sense index.
I had the same problem and solved this by updating my build settings to use the system default compiler, LLVM GCC 4.2.
It seems there are multiple factors.
I experience this issue as well, and have tried the solutions either "rebuilding the index", "relaunch XCode", "change the compiler setting to LLVM GCC 4.2". Those didn't help me out.
So I tried to add the following code right away along with the fresh creation of header file.
#include "header.h"
using namespace cocos2d
Then, the code added earlier in header file is colorized, and it can recognize classes and stuff.
I've used cocos2d-x, so my syntax is based on c++. I don't test it with Objective-c yet though.
For someone still have don't know why. Let open with XCode 8.2, it should work