[英]C-style constants and their values in IOS
首先,我是IOS应用程序开发的新手,但是由于我来自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];}
通过引用“ kFontNavigation” const,定义“ titleTextAttributes”的行将我说为“ Expected']'”。 但是,该行没有错误,而是带有“ kFontNavigationTextColour”的行告诉我“ Expected']'”。
这些问题是什么? 我正在编写和运行与书中相同的代码。 也许问题出在IDE或编译器中? 我的Xcode是4.5.2和OS X Lion 10.7.5。
PS:所有常量(定义的)和静态方法都在一个类中定义。
在常量定义的末尾删除分号:
#define kFontNavigationTextColour [UIColor colorWithRed: 106.f/255.f green:62.f/255.f blue:29.f/255.f alpha: 1.f];
#define语句不需要终止的半冒号。 如果确实添加了它们,则编译器会将它们识别为您尝试分配给宏变量的字符串的一部分。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.