Skip to content

Commit 7080b5d

Browse files
committed
🔨 Fixed an issue in writeUpdates and added a convenience method for removing file wrappers with a filename.
1 parent 38ea37b commit 7080b5d

File tree

2 files changed

+50
-28
lines changed

2 files changed

+50
-28
lines changed

LSFileWrapper.h

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#if TARGET_OS_IPHONE || TARGET_OS_SIMULATOR
1111
#import <AssetsLibrary/AssetsLibrary.h>
1212
#import <UIKit/UIKit.h>
13-
#else
13+
#elif TARGET_OS_OSX
1414
#import <AppKit/AppKit.h>
1515
#endif
1616

@@ -166,10 +166,19 @@ FOUNDATION_EXPORT const unsigned char LSFileWrapperVersionString[];
166166
*
167167
* @warning Should only be called on the LSFileWrapper of type Directory.
168168
*
169-
* @param fileWrapper Child wrapper which should be added to current LSFileWrapper as LSFileWrapper.
169+
* @param fileWrapper Child wrapper which should be removed from the current LSFileWrapper as LSFileWrapper.
170170
*/
171171
- (void)removeFileWrapper:(nonnull LSFileWrapper *)fileWrapper;
172172

173+
/**
174+
* @brief Removes the child wrapper with supplied name from the current LSFileWrapper.
175+
*
176+
* @warning Should only be called on the LSFileWrapper of type Directory.
177+
*
178+
* @param filename Child wrapper's filename which should be removed from the current LSFileWrapper.
179+
*/
180+
- (void)removeFileWrapperWithFilename:(nonnull NSString *)filename NS_SWIFT_NAME(removeFileWrapper(with:));
181+
173182
/**
174183
* @brief Adds a new child wrapper of type File with the supplied name to the current LSFileWrapper. If a wrapper is already present with the same name, then the new wrapper will be saved under the returned named to prevent collisions.
175184
*

LSFileWrapper.m

Lines changed: 39 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ - (UIImage *)image
159159
}
160160
return nil;
161161
}
162-
#else
162+
#elif TARGET_OS_OSX
163163
- (NSImage *)image {
164164
if (content == nil && writtenURL != nil) {
165165
content = [[NSImage alloc] initWithContentsOfFile:writtenURL.path];
@@ -275,12 +275,22 @@ - (void)removeFileWrapper:(LSFileWrapper *)fileWrapper {
275275
userInfo:nil];
276276
return;
277277
}
278-
LSFileWrapper *existing = [fileWrappers objectForKey:fileWrapper.filename];
278+
[self removeFileWrapperWithFilename:fileWrapper.filename];
279+
}
280+
281+
- (void)removeFileWrapperWithFilename:(NSString *)filename_ {
282+
if (!isDirectory) {
283+
@throw [NSException exceptionWithName:NSInternalInconsistencyException
284+
reason:@"LSFileWrapper file is not a directory."
285+
userInfo:nil];
286+
return;
287+
}
288+
LSFileWrapper *existing = [fileWrappers objectForKey:filename_];
279289
if (existing.writtenURL) {
280290
existing.deleted = YES;
281291
}
282292
else {
283-
[fileWrappers removeObjectForKey:fileWrapper.filename];
293+
[fileWrappers removeObjectForKey:filename_];
284294
}
285295
updated = YES;
286296
}
@@ -551,7 +561,7 @@ - (BOOL)writeImage:(UIImage *)image_ toURL:(NSURL *)url error:(NSError *__autore
551561
return [imageData writeToURL:url options:NSDataWritingAtomic error:outError];
552562
}
553563

554-
#else
564+
#elif TARGET_OS_OSX
555565
- (BOOL)writeImage:(NSImage *)image_ toURL:(NSURL *)url error:(NSError *__autoreleasing *)outError
556566
{
557567
NSData *imageData;
@@ -650,35 +660,38 @@ - (BOOL)writeUpdates:(NSArray *)updates filemanager:(NSFileManager *)fileManager
650660
success = success && [fileManager createDirectoryAtURL:fileURL withIntermediateDirectories:NO attributes:nil error:outError];
651661
}
652662
}
653-
else if ([fileContent isKindOfClass:[NSData class]]) {
663+
else {
654664
// Create the directories if they're missing
655665
NSURL* directoryURL = [fileURL URLByDeletingLastPathComponent];
656666
if ([directoryURL checkResourceIsReachableAndReturnError:nil] == NO) {
657667
[[NSFileManager defaultManager] createDirectoryAtURL:directoryURL withIntermediateDirectories:YES attributes:@{NSFileExtensionHidden: @YES} error:outError];
658668
}
659-
success = [(NSData*)fileContent writeToURL:fileURL options:NSDataWritingAtomic error:outError];
660-
}
661-
else if ([fileContent isKindOfClass:[NSString class]]) {
662-
success = [(NSString*)fileContent writeToURL:fileURL atomically:YES encoding:NSUTF8StringEncoding error:outError];
663-
}
669+
if ([fileContent isKindOfClass:[NSData class]]) {
670+
671+
success = [(NSData*)fileContent writeToURL:fileURL options:NSDataWritingAtomic error:outError];
672+
}
673+
else if ([fileContent isKindOfClass:[NSString class]]) {
674+
success = [(NSString*)fileContent writeToURL:fileURL atomically:YES encoding:NSUTF8StringEncoding error:outError];
675+
}
664676
#if TARGET_OS_IPHONE || TARGET_OS_SIMULATOR
665-
else if ([fileContent isKindOfClass:[UIImage class]]) {
666-
success = [self writeImage:(UIImage *)fileContent toURL:fileURL error:outError];
667-
}
668-
else if ([fileContent isKindOfClass:[ALAsset class]]) {
669-
success = [self writeAsset:(ALAsset *)fileContent toURL:fileURL fileManager:fileManager error:outError];
670-
}
671-
#else
672-
else if ([fileContent isKindOfClass:[NSImage class]]) {
673-
success = [self writeImage:(NSImage *)fileContent toURL:fileURL error:outError];
674-
}
677+
else if ([fileContent isKindOfClass:[UIImage class]]) {
678+
success = [self writeImage:(UIImage *)fileContent toURL:fileURL error:outError];
679+
}
680+
else if ([fileContent isKindOfClass:[ALAsset class]]) {
681+
success = [self writeAsset:(ALAsset *)fileContent toURL:fileURL fileManager:fileManager error:outError];
682+
}
683+
#elif TARGET_OS_OSX
684+
else if ([fileContent isKindOfClass:[NSImage class]]) {
685+
success = [self writeImage:(NSImage *)fileContent toURL:fileURL error:outError];
686+
}
675687
#endif
676-
else {
677-
NSData *data = [NSPropertyListSerialization dataWithPropertyList:fileContent
678-
format:NSPropertyListBinaryFormat_v1_0
679-
options:0
680-
error:NULL];
681-
success = data && [data writeToURL:fileURL atomically:YES];
688+
else {
689+
NSData *data = [NSPropertyListSerialization dataWithPropertyList:fileContent
690+
format:NSPropertyListBinaryFormat_v1_0
691+
options:0
692+
error:NULL];
693+
success = data && [data writeToURL:fileURL atomically:YES];
694+
}
682695
}
683696

684697
if (success == NO) {

0 commit comments

Comments
 (0)