IOS中的C风格常量及其值

时间:2013-01-13 16:22:20

标签: ios objective-c xcode const c-preprocessor

首先,我是IOS app开发的新手,但是因为我来自Java世界,所以熟悉C风格的语言。我按照本书http://books.google.kz/books/about/Foundation_iPhone_App_Development.html?id=68L0dC2Sl8IC&redir_esc=y学习为IOS开发。我的问题来自第9章。

第一个问题是当我尝试构建时,它说“将Objective-C指针类型”UIColor“隐式转换为C指针类型”GCColorRef“(又名'struct CGColor *')需要桥接转换”。代码如下(错误行标有注释):

#define kFontLightOnDarkTextColour [UIColor colorWithRed: 255.0/255 green:251.0/255 blue: 218.0/255 alpha:1.0];
#define kFontDarkOnLightTextColour [UIColor colorWithRed: 1.0/255 green:1.0/255 blue:1.0/255 alpha: 1.0];

#define kFontNavigationTextColour [UIColor colorWithRed: 106.f/255.f green:62.f/255.f blue:29.f/255.f alpha: 1.f];
#define kFontNavigationDisabledTextColour [UIColor colorWithRed: 106.f/255.f green: 62.f/255.f blue: 39.f/255.f alpha: 0.6f];
#define kNavigationButtonBackgroundColour [UIColor colorWithRed: 255.f/255.f green: 245.f/255.f blue: 225.f/255.f alpha: 1.f];
#define kToolbarButtonBackgroundColour [UIColor colorWithRed: 39.f/255.f green: 17.f/255.f blue: 5.f/255.f alpha: 1.f];
#define kLargeButtonTextColour [UIColor whiteColor];

#define kFontNavigation [UIFont fontWithName: @"HelveticaNeue-Bold" size:18.f];
#define kFontName [UIFont fontWithName: @"HelveticaNeue-Bold" size:30.f];
#define kFontBirthdayDate [UIFont fontWithName: @"HelveticaNeue" size:13.f];
#define kFontDaysUntilBirthday [UIFont fontWithName: @"HelveticaNeue-Bold" size:25.f];
#define kFontDaysUntilBirthdaySubText [UIFont fontWithName: @"HelveticaNeue" size:9.f];
#define kFontLarge [UIFont fontWithName: @"HelveticaNeue-Bold" size:17.f];
#define kFontButton [UIFont fontWithName: @"HelveticaNeue-Bold" size:30.f];
#define kFontNotes [UIFont fontWithName: @"HelveticaNeue" size:16.f];
#define kFontPicPhoto [UIFont fontWithName: @"HelveticaNeue-Bold" size:12.f];
#define kFontDropShadowColour [UIColor colorWithRed:1.0/255 green:1.0/255 blue:1.0/255 alpha:0.75];

+(void) styleLabel: (UILabel *)label withType:(TSKLabelType) labelType {
switch(labelType) {
    case TSKLabelTypeName:
        label.font = kFontName;
        label.layer.shadowColor = kFontDropShadowColour.CGColor;//error is here, when I try to use CGColor
        label.layer.shadowOffset = CGSizeMake(1.0f, 1.0f);
        label.layer.shadowRadius = 0.0f;
        label.layer.masksToBounds = NO;
        label.textColor = kFontLightOnDarkTextColour;
        break; }}

第二个问题的代码如下:

+(void) initStyles {
NSDictionary* titleTextAttributes = [NSDictionary dictionaryWithObjectsAndKeys: kFontNavigationTextColour, UITextAttributeTextColor, [UIColor whiteColor], UITextAttributeTextShadowColor, [NSValue valueWithUIOffset:UIOffsetMake(0, 2)], UITextAttributeTextShadowOffset, kFontNavigation, UITextAttributeFont, nil];
[[UINavigationBar appearance] setTitleTextAttributes:titleTextAttributes];
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"navigation-bar-background.png"] forBarMetrics:UIBarMetricsDefault];}

“titleTextAttributes”定义的行通过引用“kFontNavigation”const告诉我“Expected']'”。但是,该行没有错误,而是使用“kFontNavigationTextColour”的行称我“预期']'”。

这些问题是什么?我正在编写并运行与本书中所写相同的代码。也许问题出在IDE或编译器中?我的Xcode是4.5.2和OS X Lion 10.7.5。

P.S。:所有consttants(define)和静态方法都在一个类中定义。

1 个答案:

答案 0 :(得分:2)

删除常量定义末尾的半冒号:

#define kFontNavigationTextColour [UIColor colorWithRed: 106.f/255.f green:62.f/255.f blue:29.f/255.f alpha: 1.f];

#define语句不需要终止半冒号。如果添加它们,编译器会将它们识别为您尝试分配给宏变量的字符串的一部分。