2010-06-13 111 views
7

我刚刚学习Cocoa(来自C#),并且对于看起来非常简单的事情我收到了一个奇怪的错误。 (charsSinceLastUpdate >= 36指针和整数之间的比较

#import "CSMainController.h" 

@implementation CSMainController 
//global vars 
int *charsSinceLastUpdate = 0; 
NSString *myString = @"Hello world"; 
// 

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification { 
... 
} 

//other functions 
- (void)textDidChange:(NSNotification *)aNotification { 
    NSLog(@"charsSinceLastUpdate=%i",charsSinceLastUpdate); 
    if (charsSinceLastUpdate>=36) { // <- THIS line returns the error: Comparison between pointer and integer 
     charsSinceLastUpdate=0; 
     [statusText setStringValue:@"Will save now!"]; 
    } else { 
     charsSinceLastUpdate++; 
     [statusText setStringValue:@"Not saving"]; 
    } 

} 

//my functions 
- (void)showNetworkErrorAlert:(BOOL)showContinueWithoutSavingOption { 
... 
} 
// 

@end 

任何帮助将不胜感激,谢谢!

回答

20

在你的代码,charsSinceLastUpdate指针,你需要定义它没有*

int charsSinceLastUpdate = 0;

除非,当然,你意思将其定义为一个指针,在这种情况下,您需要使用dereference operator来检索它指向的值,如下所示:

if(*charsSinceLastUpdate >= 36) { 
    //... 
} 
+0

谢谢,我以为*只是一个常见的命名约定 – 2010-06-13 08:06:10

相关问题