Since there's no way to directly create .mm (Objective-C++) files in Xcode, I just create a CocoaTouch Class with Objective-C and rename the .m file to .mm file. However it seems the code autocomplete functionality is completely broken for .mm files, for example after I type "NS" I expect a list of "NSArray", "NSString", "NSLog", etc. etc. to show up, but now it only shows some code snippet shortcuts like "nsenum" and "nsoptions". If I rename the extension back to .m, the autocomplete works correctly again.
Now I wonder if there's some setting in Xcode to make the code autocomplete functionality work correctly for .mm files? So far I have not found any relevant setting, and keep renaming the extension back and forth between m and mm is really tedious after a while.
Related
Xcode has the "helpful feature" (in quotes because its not helpful to me) of generating a swift-syntax representation of objective-c headers.
When using shortcuts to open a .h file (cmd-shift-o then type Foo.h) -- sometimes you are taken to the swift representation of Foo.h and sometimes to the objective-c representation.
The vast majority of the time, I want the objective-c representation -- is there a way to quickly switch to it with keyboard shortcuts when Xcode dumps you in the swift version?
As of Xcode 9 (possibly before), you can select the "counterpart" from the four square menu. One is labeled as the swift representation, one is the ObjC representation.
As far as I know, if you are currently in a Swift file and open a header, you'll see the Swift-generated header. If you want to see the Objective-C version, you can open a .m file first, then open the header file.
I'm trying to make a plugin that writes something to the .h file with a key command (⌥ + c) while I'm coding in the .m file, e.g. I'm writing a method I want to be exposed, I press my key command at the end of the line, and it automatically writes it in the .h file without me having to copy and paste it over.
If I've just made a change in the .h file and go to my .m file and use my plugin directly after, I get an error popup saying that Xcode has changes that is not on the disk yet, and then asks which version I want to use, the one on the disk or the one in Xcode.
If I'm only coding in the .m file and adding the methods and properties to the .h file with my plugin it works fine (because I haven't made any changes to the .h file manually it seems), it's only when I've just made some changes myself to the .h file and go back to the .m file and use my plugin directly after that I get an error.
If I'm changing something in the .h file and then press ⌘+s and go over to my .m file and use my plugin it also works as intended because I force it to save whatever is in the .h file now. But pressing the ⌘+s command from the .m doesn't help my case, so it seems to only save the current file you're working on, be it the .h file or the .m file.
So my question is, is it possible to make some lines of code which forces Xcode to save all files (or just the .h file if that's possible, I have the, I have the filename and the path), so I'm sure Xcode and the data on hard disk matches up before the plugin makes any changes to what is already on the hard disk?
When creating a new class in Xcode the default behaviour is to open and display the implementation file (.m). How do I make Xcode open the header (.h) file first? I often find myself often needing to alter the .h file first to change things such as member variables, properties and inheritance.
As you add a new class, the first file that gets added is .h and last file is .m.
And XCode opens the latest file for you which is .m. Now you want to make it reverse, which is not possible unless some tweak or hack, but the best practice would be to use Ctrl+Cmd+Up or Down to switch between .h and .m
Is there any way to get proper syntax highlighting, autocompletion, etc. for objective-C code in lex and yacc input files with XCode? Specifically, I'm even using .lm and .ym files, and XCode 4.2. XCode sort of recognizes them, since it builds them properly and formats them in a monospace font, but no help coding at all. Code sense is working properly in normal source files.
I've looked around and I did find the XCCodeSenseAllowAutoCompletionInPlainFiles user default, which helps a little, but it's still not anywhere close to real code sense.
I've implemented a tabbed UITableViewController. There are some tabs on the top which reload the contents of the table. Based on the selected tab different cells are shown to the user. It all works nice, but I end up with a source file which contains 3 different implementations, and gets a little bit bulky and confusing, even using pragmas to mark sections of the source code.
I've thought of creating selectors at runtime from strings based on the selected tab, then splitting the .m file into several putting there the renamed methods, but then there's the forced #end and the end of a file and the compiler telling you that there are missing methods to be implemented.
Really, it looks like objective-c wasn't designed to split the source though several files. Is there any design pattern that can be used for this? Somehow I managed to emulate all this using #include <otherfile.m> before the #end of the main class, but it doesn't look pretty. Also, Xcode gets confused as hell if I try to include that file into the project, since it tries to compile it separately (at least I can include the files in the project and disable their inclusion in the target).
In the end I have resorted to the basic preprocessor, meaning includes and some trickery. Here's what I've done:
Given a table view controller filename.m, create one filename_functionality.m file for each tab/group of related functionality and put there the methods. Note that there is no #implementation, #end, #include, etc, in this file, just the methods.
In your Xcode project add the new files, but be careful to not mark them as required for any build targets. If you forget, select later the files and uncheck the mark which includes them into your target. This is to avoid Xcode trying to compile these rouge files alone.
At the end of your main filename.m and just before your #end marker, add the necessary #include "filename_functionality.m", which effectively tells the preprocessor to treat all the files as a single one.
For the cases where your main file is calling methods of your subordinate files, add to the top of your main file an anonymous interface and declare there the prototypes of the methods you moved to the other files.
At this point, it works! However, Xcode is still really annoying, despite being able to open the subordinate source code files, it doesn't parse them properly, so you can't use the quick navigation bar to jump between methods, for example. Again, to solve this more preprocessor trickery:
At the beginning of each subordinate file, add (yuck, wiki formatting is broken):
#ifndef _INCLUDE_FILES
#implementation BIEntity_view_controller
#endif
At the end of your subordinate file repeat with ifndef enclosing an #end.
At the end of your main file and before the includes, define _INCLUDE_FILES.
What this does is trick XCode into thinking that this is a proper implementation file, so syntax highlighting, completion and navigation bars work as expected.
The only minor nit with this setup is that Xcode doesn't properly report errors for lines in your subordinate files, it just points to the include and stays there. Now I have to right click on the line error and select Reveal in Log which sows the full console message where the correct line numbers are.
This is not a big problem if you don't write bad code (hehe) or use an external text editor anyway, but will seriously hurt if you are used to the quick fix keys to jump from one error line to another.
With this trick I've split a 1151 line file into four of sizes 558, 342, 55 and 145 lines each. Now the functionality is better related and the code can still work as part of the same class, so I don't need to write accessors as would be the case if using a language construct like classes or interfaces.