@@ -160,7 +160,8 @@ - (UIImage *)image
160160 return nil ;
161161}
162162#elif TARGET_OS_OSX
163- - (NSImage *)image {
163+ - (NSImage *)image
164+ {
164165 if (content == nil && writtenURL != nil ) {
165166 content = [[NSImage alloc ] initWithContentsOfFile: writtenURL.path];
166167 cacheFile = YES ;
@@ -262,7 +263,8 @@ - (void)setFileWrapper:(LSFileWrapper *)fileWrapper withFilename:(NSString *)fil
262263 [self setFileWrapper: fileWrapper filename: filename_ replace: YES ];
263264}
264265
265- - (void )removeFileWrapper : (LSFileWrapper *)fileWrapper {
266+ - (void )removeFileWrapper : (LSFileWrapper *)fileWrapper
267+ {
266268 if (!isDirectory) {
267269 @throw [NSException exceptionWithName: NSInternalInconsistencyException
268270 reason: @" LSFileWrapper file is not a directory."
@@ -275,24 +277,26 @@ - (void)removeFileWrapper:(LSFileWrapper *)fileWrapper {
275277 userInfo: nil ];
276278 return ;
277279 }
278- [self removeFileWrapperWithFilename : fileWrapper.filename];
280+ [self removeFileWrapperWithPath : fileWrapper.filename];
279281}
280282
281- - (void )removeFileWrapperWithFilename : (NSString *)filename_ {
282- if (!isDirectory) {
283- @throw [NSException exceptionWithName: NSInternalInconsistencyException
284- reason: @" LSFileWrapper file is not a directory."
285- userInfo: nil ];
283+ - (void )removeFileWrapperWithPath : (NSString *)path
284+ {
285+ NSString *dirpath = [path stringByDeletingLastPathComponent ];
286+ NSString *filename_ = [path lastPathComponent ];
287+ LSFileWrapper *dirFileWrapper = [self walkDirectoryPath: dirpath create: NO ];
288+
289+ if (!dirFileWrapper) {
286290 return ;
287291 }
288- LSFileWrapper *existing = [fileWrappers objectForKey: filename_];
292+ LSFileWrapper *existing = [dirFileWrapper. fileWrappers objectForKey: filename_];
289293 if (existing.writtenURL ) {
290294 existing.deleted = YES ;
291295 }
292296 else {
293- [fileWrappers removeObjectForKey: filename_];
297+ [[dirFileWrapper fileWrappers ] removeObjectForKey: filename_];
294298 }
295- updated = YES ;
299+ dirFileWrapper. updated = YES ;
296300}
297301
298302- (NSString *)addContent : (id <NSObject >)content_ withFilename : (NSString *)filename_
@@ -332,7 +336,8 @@ - (BOOL)writeUpdatesToURL:(NSURL *)url error:(NSError *__autoreleasing *)outErro
332336 return [self writeUpdates: updates filemanager: [[NSFileManager alloc ] init ] error: outError];
333337}
334338
335- - (BOOL )writeToURL : (NSURL *)url error : (NSError *__autoreleasing *)outError {
339+ - (BOOL )writeToURL : (NSURL *)url error : (NSError *__autoreleasing *)outError
340+ {
336341 NSMutableArray *updates = [[NSMutableArray alloc ] init ];
337342
338343 if (parent) {
@@ -346,7 +351,8 @@ - (BOOL)writeToURL:(NSURL *)url error:(NSError *__autoreleasing *)outError {
346351}
347352
348353#if TARGET_OS_OSX
349- - (BOOL )writeToURL : (NSURL *)url forSaveOperation : (NSSaveOperationType )saveOperation originalContentsURL : (nullable NSURL *)absoluteOriginalContentsURL backupDocumentURL : (nullable NSURL *)backupFileURL error : (NSError *__autoreleasing *)outError {
354+ - (BOOL )writeToURL : (NSURL *)url forSaveOperation : (NSSaveOperationType )saveOperation originalContentsURL : (nullable NSURL *)absoluteOriginalContentsURL backupDocumentURL : (nullable NSURL *)backupFileURL error : (NSError *__autoreleasing *)outError
355+ {
350356 switch (saveOperation) {
351357 case NSAutosaveInPlaceOperation:
352358 // Auto overwrite
@@ -483,8 +489,10 @@ - (NSString *)setFileWrapper:(LSFileWrapper *)fileWrapper filename:(NSString *)f
483489 format = [NSString stringWithFormat: @" %@ %% d" , basename];
484490 }
485491 while (true ) {
492+ // Checking both cases for iOS and macOS interoperability
486493 LSFileWrapper *existing = [fileWrappers objectForKey: filename_];
487- if (!existing || existing.deleted ) {
494+ LSFileWrapper *existingCase = [fileWrappers objectForKey: [filename_ lowercaseString ]];
495+ if ((!existing || existing.deleted ) && (!existingCase || existingCase.deleted )) {
488496 break ;
489497 }
490498 filename_ = [NSString stringWithFormat: format, ++num];
0 commit comments