-1

首先,我是 IOS 应用程序开发的新手,但熟悉 C 风格的语言,因为我来自 Java 世界。我按照这本书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];}

通过引用“kFontNavigation”常量,定义“titleTextAttributes”的行说我“预期']'”。但是,该行没有错误,而是带有“kFontNavigationTextColour”的行说我“预期']'”。

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

PS:所有常量(定义)和静态方法都定义在一个类中。

4

1 回答 1

2

删除常量定义末尾的分号:

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

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

于 2013-01-13T16:26:20.013 回答