我刚开始学习 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
任何帮助将不胜感激,谢谢!
最佳答案
在您的代码中,charsSinceLastUpdate
是一个指针,您需要定义它没有 *
:
int charsSinceLastUpdate = 0;
当然,除非您打算将其定义为指针,在这种情况下您需要使用 dereference operator检索它指向的值,如下所示:
if(*charsSinceLastUpdate >= 36) {
//...
}
关于objective-c - 指针与整数的比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3031378/