NSDocument isLocked implementation for 10.7?

自古美人都是妖i 提交于 2019-12-25 18:14:12

问题


How can I check if a document isLocked in 10.7?

NSDocument has a method isLocked, but it available only on 10.8.


回答1:


Here is my implementation:

+ (BOOL)isDocumentLocked:(NSDocument*)doc
{
  if (doc == nil)
  {
    return NO;
  }
  else if ([doc respondsToSelector:@selector(isLocked)]) // 10.8
  {
    return [doc isLocked];
  }
  else // OS X version < 10.8
  {
    NSError * error;
    BOOL isAutosavingSafe = [doc checkAutosavingSafetyAndReturnError:&error];
    if (!isAutosavingSafe)
    {
      return YES;
    }

    if (doc.fileURL == nil)
      return NO;

    NSFileManager* fm = [NSFileManager defaultManager];
    NSString* path = doc.fileURL.absoluteURL.path;

    if (![fm isWritableFileAtPath:path])
      return YES; // No writing permissions

    NSDictionary *attributes = [fm attributesOfItemAtPath:path error:&error];
    BOOL isLocked = [[attributes objectForKey:NSFileImmutable] boolValue];
    if (isLocked)
    {
      return YES;
    }
  }
  return NO;
}


来源:https://stackoverflow.com/questions/12059241/nsdocument-islocked-implementation-for-10-7

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!