[英]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.