指针和整数之间的比较

时间:2010-06-13 07:07:13

标签: objective-c cocoa variables pointers

我只是在学习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

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

1 个答案:

答案 0 :(得分:20)

在您的代码中,charsSinceLastUpdate是一个指针,您需要定义而不 *

int charsSinceLastUpdate = 0;

当然,除非意味着将其定义为指针,否则您需要使用dereference operator来检索它指向的值,就像这样:

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