2010-05-02 30 views
15
CGContextSetFillColorWithColor(g, [UIColor greyColor].CGColor); 

Tôi đang cố gắng làm theo cuốn sách của O'Reilly, iPhone Phát triển trò chơi, nhưng trên trang 73 Chương 3 Tôi nhận được lỗi này:Cách truy cập thuộc tính CGColor của UIColor trong CGContextSetFillColorWithColor?

error: request for member 'CGColor' in something not a structure or union 

Theo cuốn sách của errata page đây là một errata chưa được xác nhận trong sách. Có thể thay thế dòng mã chức năng nào bằng?

Chi tiết bổ sung

Ví dụ dự án có thể được tải về here.

tôi đã gặp phải lỗi tại render chức năng bằng cách làm theo hướng dẫn của cuốn sách từ trang 72 đến trang 73 để xây dựng các lớp gsMain (khác nhau của nó từ pg77 dụ dự án) tại render chức năng của gsMain.m

đoạn mã rằng cuốn sách chỉ thị để xây dựng lớp gsMain là như sau:

//gsMain.h 
@interface gsTest : GameState { } 
@end 

//gsMain.m 
@implementation gsMain 

-(gsMain*) initWithFrame:(CGRect)frame andManager:(GameStateManager*)pManager 

    { 
     if (self = [super initWithFrame:frame andManager:pManager]) { 
     NSLog(@"gsTest init"); 
    } 
return self; 
} 

-(void) Render 
{ 
    CGContextRef g = UIGraphicsGetCurrentContext(); 
    //fill background with gray 
    CGContextSetFillColorWithColor(g, [UIColor greyColor].CGColor); //Error Occurs here 
    CGContextFillRect(g, CGRectMake(0, 0, self.frame.size.width, 
    self.frame.size.height)); 
//draw text in black 
CGContextSetFillColorWithColor(g, [UIColor blackColor].CGColor); 
[@"O'Reilly Rules!" drawAtPoint:CGPointMake(10.0,20.0) 
     withFont:[UIFont systemFontOfSize:[UIFont systemFontSize]]]; 
} 
-(void)touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event 
{ 
    UITouch* touch = [touches anyObject]; 
    NSUInteger numTaps = [touch tapCount]; 
    //todo: implement touch event code here 
} 
@end 

các Chapter3_Example_p77 là nghĩa vụ phải hiển thị các kết quả của các bài tập từ trang 71-77 nhưng nó rất khác so với những hướng dẫn được đưa ra trong 71 đến 77. Mã sau đây là lớp hoàn chỉnh, có thể biên dịch được tải xuống từ phần trên liên kết.

//gsMain.h 
#import <Foundation/Foundation.h> 
#import "GameState.h" 
@interface gsMain : GameState { 

} 

@end 

// gsMain.m 
// Example 
// Created by Joe Hogue and Paul Zirkle 

#import "gsMain.h" 
#import "gsTest.h" 
#import "Test_FrameworkAppDelegate.h" 

@implementation gsMain 

-(gsMain*) initWithFrame:(CGRect)frame andManager:(GameStateManager*)pManager { 
if (self = [super initWithFrame:frame andManager:pManager]) { 
    //do initializations here. 
} 
return self; 
} 

- (void) Render { 
[self setNeedsDisplay]; //this sets up a deferred call to drawRect. 
} 

- (void)drawRect:(CGRect)rect { 
CGContextRef g = UIGraphicsGetCurrentContext(); 
//fill background with gray 
CGContextSetFillColorWithColor(g, [UIColor grayColor].CGColor); 
CGContextFillRect(g, CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)); 
//draw text in black. 
CGContextSetFillColorWithColor(g, [UIColor blackColor].CGColor); 
[@"O'Reilly Rules!" drawAtPoint:CGPointMake(10.0, 20.0) withFont: 
    [UIFont systemFontOfSize: [UIFont systemFontSize]]]; 

//fps display from page 76 of iPhone Game Development 
int FPS = [((Test_FrameworkAppDelegate*)m_pManager) getFramesPerSecond]; 
NSString* strFPS = [NSString stringWithFormat:@"%d", FPS]; 
[strFPS drawAtPoint:CGPointMake(10.0, 60.0) withFont:[UIFont systemFontOfSize: 
    [UIFont systemFontSize]]]; 
} 

//this is missing from the code listing on page 77. 
-(void) touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event 
{ 
    UITouch* touch = [touches anyObject]; 
    NSUInteger numTaps = [touch tapCount]; 
    if(numTaps > 1) { 
    [m_pManager doStateChange:[gsTest class]]; 
} 
} 

@end 

Trả lời

6

Tôi đã biên soạn ví dụ p77 cho Simulator 3.1.3 và không chạy vào bất kỳ cảnh báo hoặc lỗi trình biên dịch nào.

Mã:

- (void)drawRect:(CGRect)rect { 
    CGContextRef g = UIGraphicsGetCurrentContext(); 
    //fill background with gray 
    CGContextSetFillColorWithColor(g, [UIColor grayColor].CGColor); 
    CGContextFillRect(g, CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)); 
    //draw text in black. 
    CGContextSetFillColorWithColor(g, [UIColor blackColor].CGColor); 
    [@"O'Reilly Rules!" drawAtPoint:CGPointMake(10.0, 20.0) withFont:[UIFont systemFontOfSize:[UIFont systemFontSize]]]; 

    //... 
} 

dường như biên dịch được. Có lẽ bạn có thể chỉ định cái nào trong ba ví dụ bạn đang biên dịch và phiên bản hệ điều hành đích nào?

+0

Nhờ mềm dùng để chạy mà biên dịch. Với sự giúp đỡ của bạn, tôi cuối cùng đã thấy vấn đề. Tôi đang biên soạn một phiên bản mà tôi đã tạo theo hướng dẫn của cuốn sách (trong các chi tiết bổ sung của câu hỏi). Mà được cho là kết quả trong ví dụ p77 mà bạn đã biên soạn. Hàm render trong 'kết quả p77' rất khác với hướng dẫn của sách trong p71-p73. Tôi nghĩ rằng tôi sẽ phải sao chép lớp gsMain của p77 thay vì làm theo hướng dẫn trên cuốn sách thực tế. – Azeworai

+0

Tôi bắt đầu tham khảo dự án ví dụ p71 và tôi đang biên soạn cho iPhoneOS 3.1.2 – Azeworai

6

Đảm bảo bạn #include <UIKit/UIKit.h>.

Và nó là -grayColor, không phải -greyColor.

+0

Yep đã cảm ơn. Tôi vừa sao chép văn bản để hiển thị hướng dẫn của cuốn sách. – Azeworai

21

Còn khoảng [ [ UIColor grayColor ] CGColor ] thì sao?

Bạn đang sử dụng SDK mới nhất? Tôi giả sử tiêu đề UIColor.h của bạn có thuộc tính cho CGColor? (Tôi sẽ kiểm tra các thiết lập SDK của bạn - khi tôi mở dự án mẫu nó được thiết lập để iOS 3.x)


Trong 3.x Swift: UIColor.gray.cgColor

Các vấn đề liên quan