【发布时间】:2011-12-15 04:10:21
【问题描述】:
我目前正在为 Mac OS X 中的字符串比较而苦苦挣扎
如果文件改变了它的大小,我想用一个循环检查(在我的例子中我选择了一个计时器)。 为此,我执行以下操作:
- (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];
}
我可以检查一次尺寸。
So when my code runs, it gives me an output and two seconds later (when the selector gets fired the second time) my program terminates with EXC_BAD_ACCESS
当我尝试通过更改为 if(![fileSize isEqualToString:fileSizeSrc]) 来反转 if() 语句时,它会在启动后立即终止。
我在第一次出现问题后保留并释放字符串,因为我突然意识到我必须这样做。
我希望有人可以帮助我! 非常感谢, 问候,朱利安。
【问题讨论】:
标签: macos cocoa nsstring nstimer