Cal*_*l S 7 variables cocoa pointers objective-c
我只是在学习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
Run Code Online (Sandbox Code Playgroud)
任何帮助将不胜感激,谢谢!
Jac*_*kin 20
在你的代码,charsSinceLastUpdate是一个指针,你需要定义它没有的*:
int charsSinceLastUpdate = 0;
当然,除非您打算将其定义为指针,在这种情况下,您需要使用解除引用运算符来检索它指向的值,如下所示:
if(*charsSinceLastUpdate >= 36) {
//...
}
Run Code Online (Sandbox Code Playgroud)