#pragma mark not showing in methods in Xcode 4.0
In Xcode Version 4.0 I notice that #pragma marks within methods are no longer showing up in the Jump Bar. The only #pragma marks that are showing up are those that are between methods.
I was using these #pragma marks to make it easy to quickly organize and get to information that appears in different sections of my tableviews 开发者_运维知识库and I would really like to get that functionality back.
Anyone know how to get them to appear again?
I'm also experiencing this problem. A pragma mark added before the first method will not show up. For example, this will not work:
@implementation RandomClass
#pragma mark - Getter Methods
- (void) firstMethod
{
}
@end
Here are some quick-dirty workarounds to make the pragma mark before the first method show up. You can add an empty block before it or you can just put the pragma mark inside the block itself.
Using an empty block:
@implementation RandomClass
{}
#pragma mark - Getter Methods
- (void) firstMethod
{
}
@end
Adding the pragma mark inside the empty block itself:
@implementation RandomClass
{
#pragma mark - Getter Methods
}
- (void) firstMethod
{
}
@end
It doesn't look too pretty but it works. I hope that helps.
I had this problem while using a syntax that used to work on xcode 3.x and does not work for xcode 4 anymore.
I was using just "pragma"
#pragma - My Pragma Text
That used to work fine for xcode 3 but, for xcode 4, the complete syntax has to be used. This means that #pragma must always be followed by the "mark" keyword.
#pragma mark - My Pragma Text
EDIT
Xcode 6 (fixed in beta 4) uses // MARK:
syntax for swift
files
Did #pragma marks work inside of methods in the previous xcode? I really would like to use them, but like you said, it only works in between methods but not within a method, which would be really useful.
BUMP if anybody has any ideas on ways to achieve adding code to a section within a method that would add it to the XCode jump bar...
And apparently, they will not work before your first method. This, for example, will not work:
#import "Person.h"
@implementation Person
#pragma mark - Convenience
// ===========================================================
// Class/Convenience Methods
// ===========================================================
+ (Person *)personWithName:(NSString *)name hourlyRate:(double)rate
{
Person *person = [[[Person alloc] initWithName:name rate:rate] autorelease];
return person;
}
If I am wrong, let me know, but I don't think I am. If this should work, I hope it gets fixed!!
Apparently there are some issues in the pragma parser in Xcode. In my case I could make the #pragma
s re-appear by unificating their syntax to the new one.
Instead of having not uniform and messy chunks of code with some pragmas in it:
// Code ...
#pragma mark -
#pragma mark UITextViewDelegate Protocol
// More code ...
# pragma mark - Important stuff
// Even more code ...
I changed everything to:
// Code ...
#pragma mark - UITextViewDelegate Protocol
// More code ...
# pragma mark - Important stuff
// Even more code ...
Basically, I made sure...
- there is one blank line before and after the
#pragma
line. - there is only one space before and another after the
-
. - the pragma line does not end with an space.
UPDATE
I've realized that sometimes above rules are not enough if the project is broken somewhere. ( I tried moving some sources to a new project and they showed correctly). So what I did is close my project, delete its derived data from the organizer and also delete these two folders
MyProject.xcodeproj/project.xcworkspace/
MyProject.xcodeproj/xcuserdata/
So next time I open my project Xcode will re-generate them :)
Now ALL my sources are OK again :)
For marks within the methods (or anywhere else) you can now use keyword TODO: as comment.
like this:
// TODO: some text
I'm not sure about xcode version this became available (probably 4.5+), but it's available in latest one.
For what it is worth, I have filed a bug report (12895229) on this given I could not find one having been filed.
Thanks for the work-around ideas. Assigned to a macro allows for easy removal in the future:
#define WORK_AROUND_PRAGMA {}
I tend to have global Macro files for helpful things like this. You know, quick encode/decode, accessors (for the days prior to properties) and the like.
I encountered the same problem. I now insert a dummy method after @implementation ...
and before the first 'real' method. I define this method in the project's ...-Prefic.pch
file so it does not show up in the Jump Bar.
Add the following to your ...-Prefix.pch:
#define PLEASE_LET_PRAGMA_MARK_WORK - (void) pleaseLetPragmaMarkWork {}
And insert the request in your omplementations:
@implementation anyClass
PLEASE_LET_PRAGMA_MARK_WORK
#pragma mark - This will show up.
Works like a charm.
I realize there is probably a very small percentage of uses for whom what I'm about to say applies, but just in case...
If you use Doxygen to generate documentation from your source code comments, and you tend to comment your methods in the implementation file (i.e. .m), then adding an empty section after @implementation will cause Doxygen to fail to parse your Doxygen comments.
@implementation ViewController
- (void)pragmaStartHack {} // Since pragma only works when after at least one method
#pragma mark This pragma will render in Xcode
// Your code here...
@end
user1639164 - At the risk of being pedantic, I'd say you're "95%" correct. I found it is possible to get Xcode to display a pragma before your first method. If the first chunk of code in a source file happens to be a regular procedure rather than a method, then you will get the pragma showing up. E.g.
NSString * localizedString (NSString * key)
{
// My code here…
}
# pragma mark - app-delegate startup
- (id) init
{
self = [super init];
if (self)… etc
This is an extract from my boilerplate app delegate code. In this case, you will see the pragma show up before the first method. But not before your first chunk of code! :-) So it's as if the pragma parsing machinery can't get its ass in gear until it's seen at least one routine - method or otherwise. As you say, hope it's fixed soon.
To clarify the correct answer by RantriX…
If your first #pragma mark
fails to appear in the jump bar's context menu, add a pair of braces. (Xcode version 4)
For example, change this:
@implementation MainMenu
#pragma mark Lifecycle
to this (note the curly braces):
@implementation MainMenu
{
}
#pragma mark Lifecycle
For showing pragma marks in Jump Bar try this :
- Just remove spaces between ' //MARK ' and ' : '.
- That should be like this ' //MARK: '.
精彩评论