如何为iOS编码配置Allman风格的Clang格式?

时间:2015-09-25 07:03:48

标签: ios code-formatting clang-format

我对Clang Format API感到很困惑。

  1. 我无法打开.clangformat文件,以便我可以查看并根据我进行配置。
  2. 我需要以Allman风格格式化我的代码。
  3. 我也看过很多关于Google和Stack Overflow的文档,但我没有得到任何帮助来实现Allman样式格式化。
  4. 我偶然遇到http://clangformat.com/,但我也没有得到任何帮助来实现奥尔曼的风格。

    这是我想要的问题和解决方案。

    问题#1:

    [[NSNotificationCenter defaultCenter]
          addObserver:self
             selector:@selector(captureSearchFiltersNotificationWithSelection:)
                 name:kSearchFiltersNotificationWithSelection
               object:nil];
    

    需要#1:

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(captureSearchFiltersNotificationWithSelection:)name:kSearchFiltersNotificationWithSelection object:nil];
    

    问题#2:

    - (id)initWithNibName:(NSString *)nibNameOrNil
                   bundle:(NSBundle *)nibBundleOrNil {
            self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    
            if (self) {
                    //        listings = [NSMutableArray new];
                    self.navTitle = @"Buy";
                    searchFilters = [SearchFilter new];
    
                    if ([LocationManager locationManager].location == nil) {
                            selectedSortOption = kSortBuyRefineOptionUndefined;
                    }
    
                    else {
                            selectedSortOption = kSortBuyRefineOptionNearMeAsc;
                    }
            }
            return self;
    }
    

    需要#2:

    - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle*)nibBundleOrNil 
    {
        self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
        if (self) 
        {
            listings = [NSMutableArray new];
            self.navTitle = @"Buy";
            searchFilters = [SearchFilter new];
    
            if ([LocationManager locationManager].location == nil) 
            {
                selectedSortOption = kSortBuyRefineOptionUndefined;
            }
    
            else 
            {
                selectedSortOption = kSortBuyRefineOptionNearMeAsc;
            }
        }
    
        return self;
    }
    

1 个答案:

答案 0 :(得分:3)

您需要在项目根目录中添加.clang格式的文件。根据要求,您可以在TextEditor中编辑此文件。以下是" Allman"的格式。式:

BasedOnStyle: None
AlignTrailingComments: true
BreakBeforeBraces: Allman
ColumnLimit: 0
IndentWidth: 4
KeepEmptyLinesAtTheStartOfBlocks: false
ObjCSpaceAfterProperty: true
ObjCSpaceBeforeProtocolList: true
PointerBindsToType: false
SpacesBeforeTrailingComments: 1
TabWidth: 8
UseTab: Never

以下是一些对您有所帮助的链接:

http://staxmanade.com/2015/01/how-to-install-clang-format-and-formatting-objective-c-files/

http://tonyarnold.com/2014/05/31/autoformatting-your-code.html

http://clang.llvm.org/docs/ClangFormatStyleOptions.html

http://blog.manbolo.com/2015/05/14/code-beautifier-in-xcode

生成并安装clang格式文件的Xcode项目链接: https://github.com/travisjeffery/ClangFormat-Xcode/blob/master/README.md

修复.clang格式问题: https://github.com/travisjeffery/ClangFormat-Xcode/issues/68

Apple的源代码格式化选项: https://developer.apple.com/legacy/library/documentation/DeveloperTools/Reference/XcodeUserDefaultRef/100-Xcode_User_Defaults/UserDefaultRef.html#//apple_ref/doc/uid/TP40005535-CH3-SW57

希望这有帮助!