2010-08-24 33 views
0

我有一個寫封裝式文檔的工具。它使用NSDocument實施,覆蓋以下NSDocument方法:製作可可NSDocument包的配方和svn一起玩的很好嗎?

- (NSFileWrapper *)fileWrapperOfType:(NSString *)typeName 
           error:(NSError **)outError; 

- (BOOL)readFromFileWrapper:(NSFileWrapper *)fileWrapper 
        ofType:(NSString *)typeName 
        error:(NSError **)outError; 

這一切都是可愛的,除非我保存的文檔版本控制之下的。 (.svn目錄不會保留,等等)

是否有一個好的配方讓我的文檔能夠很好地與svn搭配?

回答

3

我猜你的代碼通過創建一個新的文件包裝器每次調用-fileWrapperOfType:error:。這很方便,但會忽略軟件包中磁盤上可能存在的任何其他文件。

相反,如果您是通過創建/使用引用磁盤上現有內容的文件包裝器開始的。修改該包裝器以匹配文檔的當前狀態,並返回結果。當這個包裝器被寫入磁盤時,應該適當地維護svn文件。

+0

不錯,謝謝!是的,重構所有的東西來完成這項工作有點麻煩......但是一旦你知道了這個要求,那真是一塊蛋糕! – 2010-08-25 00:40:16

1

這是我的解決方案基於邁克的答案!

我的文件包捆,與通常的分層結構......因此,有我在突變四個目錄節省:

  1. 的保存創建一個新的頂級(My.bundle)
  2. 的內容目錄被改變(My.bundle /目錄)
  3. 資源目錄改變(My.bundle /內容/資源)
  4. 本地化的資源被更新(My.bundle /內容/資源/ en.lproj)

配方首先爲文檔類添加可變字典槽以保留這些目錄中的每一個的內容。

@interface LMDocument : NSDocument { 
@private 
    // All this is for preserving SCM artifacts across saves… 
    NSMutableDictionary * bundleWrappers; 
    NSMutableDictionary * contentsWrappers; 
    NSMutableDictionary * resourcesWrappers; 
    NSMutableDictionary * localizedWrappers; 
} 

當創建一個新文檔時,這些文檔從空字典開始。

- (id)init; 
{ 
    if ((self = [super init]) != nil) { 
     bundleWrappers = [[NSMutableDictionary alloc] initWithCapacity:0]; 
     contentsWrappers = [[NSMutableDictionary alloc] initWithCapacity:0]; 
     resourcesWrappers = [[NSMutableDictionary alloc] initWithCapacity:0]; 
     localizedWrappers = [[NSMutableDictionary alloc] initWithCapacity:0]; 
    } 
    return self; 
} 

在現有文檔中讀取時,將其替換爲相關fileWrapper內容的可變副本。

- (BOOL)readFromFileWrapper:(NSFileWrapper *)fileWrapper 
        ofType:(NSString *)typeName 
         error:(NSError **)outError; 
{ 
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; 
    bundleWrappers = [[fileWrapper fileWrappers] mutableCopy]; 
    contentsWrappers = [[[bundleWrappers objectForKey:@"Contents"] fileWrappers] mutableCopy]; 
    resourcesWrappers = [[[contentsWrappers objectForKey:@"Resources"] fileWrappers] mutableCopy]; 
    localizedWrappers = [[[resourcesWrappers objectForKey:@"en.lproj"] fileWrappers] mutableCopy]; 
    NSFileWrapper * infoPlistWrapper = [contentsWrappers objectForKey:@"Info.plist"]; 
    [contentsWrappers removeObjectForKey:@"Info.plist"]; // Replaced during save… 
    // … 
    NSMutableDictionary * localizedWrappersCopy = [localizedWrappers mutableCopy]; 
    [localizedWrappers enumerateKeysAndObjectsUsingBlock:^(id key, 
                  id obj, 
                  BOOL * stop) 
    { 
     if (mumble) { // If it's a file that will be replaced during save… 
      [localizedWrappersCopy removeObjectForKey:key]; // Replaced during save… 
      // … 
     } 
    }]; 
    localizedWrappers = localizedWrappersCopy; 
    [pool drain]; 
    return YES; 
} 

而且最後,保存文檔使用的字典苦心準備的時候。

- (NSFileWrapper *)fileWrapperOfType:(NSString *)typeName 
           error:(NSError **)outError; 
{ 
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; 
    NSFileWrapper * localizedWrapper = 
    [[NSFileWrapper alloc] initDirectoryWithFileWrappers:localizedWrappers]; 
    [resourcesWrappers setObject:localizedWrapper 
          forKey:@"en.lproj"]; 
    NSFileWrapper * resourcesWrapper = 
    [[NSFileWrapper alloc] initDirectoryWithFileWrappers:resourcesWrappers]; 
    [contentsWrappers setObject:resourcesWrapper 
         forKey:@"Resources"]; 
    NSFileWrapper * contentsWrapper = 
    [[NSFileWrapper alloc] initDirectoryWithFileWrappers:contentsWrappers]; 
    // … 
    for (id item in mumble) { 
     NSString * filename = [item filename]; 
     NSData * data = [item data]; 
     [localizedWrapper addRegularFileWithContents:data 
            preferredFilename:filename]; 
    } 
    [contentsWrapper addRegularFileWithContents:[self infoPlistData] 
           preferredFilename:@"Info.plist"]; 
    [pool drain]; 
    [bundleWrappers setObject:contentsWrapper 
         forKey:@"Contents"]; 
    NSFileWrapper * bundleWrapper = 
    [[[NSFileWrapper alloc] initDirectoryWithFileWrappers:bundleWrappers] autorelease]; 
    return bundleWrapper; 
} 

現在,當一個包文件進行編輯,應用程序保留它沒有添加到軟件包,其中包括供應鏈管理的文物和「其他」的本地化的文件!

相關問題