试图使代码可读

时间:2010-12-04 19:31:40

标签: iphone c xcode c-preprocessor

我正在尝试创建一个像这样的定义

#define ICONS   [NSArray arrayWithObjects: [NSNumber numberWithFloat:33.0f], [NSNumber numberWithFloat:93.0f], [NSNumber numberWithFloat:149.0f], [NSNumber numberWithFloat:205.0f], [NSNumber numberWithFloat:314.0f], [NSNumber numberWithFloat:370.0f], [NSNumber numberWithFloat:426.0f], [NSNumber numberWithFloat:482.0f], nil]

但是,我想按行编写它 - 使代码可读,如

#define ICONS   [NSArray arrayWithObjects: 
[NSNumber numberWithFloat:33.0f], 
[NSNumber numberWithFloat:93.0f], 
[NSNumber numberWithFloat:149.0f], 
[NSNumber numberWithFloat:205.0f], 
[NSNumber numberWithFloat:314.0f],
[NSNumber numberWithFloat:370.0f],
[NSNumber numberWithFloat:426.0f],
[NSNumber numberWithFloat:482.0f],
nil]

但是当我打破行中的定义时,Xcode就像疯了一样大喊。

有没有办法打破界限并按我的意愿组织代码?

2 个答案:

答案 0 :(得分:12)

要跨行分割#define,您需要将\附加到这些行。

#define ICONS   [NSArray arrayWithObjects:               \
                    [NSNumber numberWithFloat:33.0f],    \
                    [NSNumber numberWithFloat:93.0f],    \
                    [NSNumber numberWithFloat:149.0f],   \ 
                    [NSNumber numberWithFloat:205.0f],   \
                    [NSNumber numberWithFloat:314.0f],   \
                    [NSNumber numberWithFloat:370.0f],   \
                    [NSNumber numberWithFloat:426.0f],   \
                    [NSNumber numberWithFloat:482.0f],   \
                    nil]

答案 1 :(得分:2)

根据C规范,多行定义在每行的末尾需要一个'\':

#define ICONS   [NSArray arrayWithObjects: \
                    [NSNumber numberWithFloat:33.0f],  \
                    [NSNumber numberWithFloat:93.0f],  \
                    [NSNumber numberWithFloat:149.0f], \
                    [NSNumber numberWithFloat:205.0f], \
                    [NSNumber numberWithFloat:314.0f], \
                    [NSNumber numberWithFloat:370.0f], \
                    [NSNumber numberWithFloat:426.0f], \
                    [NSNumber numberWithFloat:482.0f], \
                    nil]