Macos NSTimer“循环”中的NSString比较
我目前正在Mac OS X中进行字符串比较 我想用一个循环来检查,在我的例子中,如果一个文件改变了它的大小,我选择了一个计时器。 为此,我采取以下措施:Macos NSTimer“循环”中的NSString比较,macos,cocoa,nsstring,nstimer,Macos,Cocoa,Nsstring,Nstimer,我目前正在Mac OS X中进行字符串比较 我想用一个循环来检查,在我的例子中,如果一个文件改变了它的大小,我选择了一个计时器。 为此,我采取以下措施: - (void)applicationDidFinishLaunching:(NSNotification *)aNotification { fileSize = [[NSString stringWithFormat:@""] retain]; fileSizeSrc = [[NSString stringWithForma
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
fileSize = [[NSString stringWithFormat:@""] retain];
fileSizeSrc = [[NSString stringWithFormat:@""] retain];
fileManager = [NSFileManager defaultManager];
fileAtts = [fileManager attributesOfItemAtPath:@"~/Desktop/test.txt" error:NULL];
fileSizeSrc = [NSString stringWithFormat:@"%llu", [fileAtts fileSize]];
[self startWatching:nil];
}
- (void)startWatching:(id)sender
{
timer = [NSTimer scheduledTimerWithTimeInterval:2
target:self
selector:@selector(checkForUpdates)
userInfo:nil
repeats:YES];
[timer fire];
}
- (void)checkForUpdates
{
fileAtts = [fileManager attributesOfItemAtPath:@"~/Desktop/test.txt" error:NULL];
fileSize = [NSString stringWithFormat:@"%llu", [fileAtts fileSize]];
if([fileSize isEqualToString:fileSizeSrc])
{
NSLog(@"%@", fileSize);
fileSizeSrc = [NSString stringWithFormat:@"%@", fileSizeSrc];
}
}
- (void)applicationWillTerminate:(NSNotification *)notification
{
[timer invalidate];
timer = nil;
[fileSize release];
[fileSizeSrc release];
}
我可以检查一下尺寸。
因此,当我的代码运行时,它会给我一个输出,两秒钟后,当我的程序第二次以EXC_BAD_访问终止时,选择器被触发
当我试图通过改变if来反转if语句时![fileSize isEqualToString:FILESIZESC]它在启动后立即终止
第一次出现问题后,我保留并释放了字符串,因为我意识到我必须这样做
我希望有人能帮助我!
谢谢,
你好,朱利安。如果您不使用ios5中提供的自动保留计数器: 您需要创建一个属性来轻松地保留您的信息,或者自己保留它
@interface ...
{
NSString* _fileSizeSrc;
}
@property (retain)NSString* fileSizeSrc;
@end
@implementation ...
@synthesise fileSizeSrc = _fileSizeSrc;
...
@end
要设置需要写入的属性,请执行以下操作:
self.fileSizeSrc = [NSString stringWithFormat:@"%llu", [fileAtts fileSize]];
还有两件事:
不要忘记将属性设置为nil in-void dealloc
如果创建属性-不要直接使用变量。