Skip to content

Commit f529da0

Browse files
authored
Add function to gul secure encoding to encode multiple calsses (#4282)
* style * secure coding
1 parent d056539 commit f529da0

File tree

3 files changed

+32
-6
lines changed

3 files changed

+32
-6
lines changed

GoogleUtilities/Example/Tests/SecureCoding/GULSecureCodingTests.m

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ @interface GULSecureCodingTests : XCTestCase
3636

3737
@implementation GULSecureCodingTests
3838

39-
- (void)testArchiveUnarchive {
39+
- (void)testArchiveUnarchiveSingleClass {
4040
NSDictionary *objectToArchive = @{@"key1" : @"value1", @"key2" : @(2)};
4141

4242
NSError *error;
@@ -51,6 +51,22 @@ - (void)testArchiveUnarchive {
5151
XCTAssert([objectToArchive isEqualToDictionary:unarchivedObject]);
5252
}
5353

54+
- (void)testArchiveUnarchiveMultipleClasses {
55+
NSDictionary *objectToArchive = @{@"key1" : [NSDate date], @"key2" : @(2)};
56+
57+
NSError *error;
58+
NSData *archiveData = [GULSecureCoding archivedDataWithRootObject:objectToArchive error:&error];
59+
XCTAssertNil(error);
60+
XCTAssertNotNil(archiveData);
61+
62+
NSDictionary *unarchivedObject = [GULSecureCoding
63+
unarchivedObjectOfClasses:[NSSet setWithArray:@[ NSDictionary.class, NSDate.class ]]
64+
fromData:archiveData
65+
error:&error];
66+
XCTAssertNil(error);
67+
XCTAssert([objectToArchive isEqualToDictionary:unarchivedObject]);
68+
}
69+
5470
- (void)testArchivingIncompatibleObjectError {
5571
SecureCodingIncompatibleObject *objectToArchive = [[SecureCodingIncompatibleObject alloc] init];
5672

GoogleUtilities/SecureCoding/GULSecureCoding.m

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,21 @@
1818

1919
@implementation GULSecureCoding
2020

21-
+ (nullable id)unarchivedObjectOfClass:(Class)class
22-
fromData:(NSData *)data
23-
error:(NSError **)outError {
21+
+ (nullable id)unarchivedObjectOfClasses:(NSSet<Class> *)classes
22+
fromData:(NSData *)data
23+
error:(NSError **)outError {
2424
id object;
2525
#if __has_builtin(__builtin_available)
2626
if (@available(macOS 10.13, iOS 11.0, tvOS 11.0, *)) {
27-
object = [NSKeyedUnarchiver unarchivedObjectOfClass:class fromData:data error:outError];
27+
object = [NSKeyedUnarchiver unarchivedObjectOfClasses:classes fromData:data error:outError];
2828
} else
2929
#endif // __has_builtin(__builtin_available)
3030
{
3131
@try {
3232
NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
3333
unarchiver.requiresSecureCoding = YES;
3434

35-
object = [unarchiver decodeObjectOfClass:class forKey:NSKeyedArchiveRootObjectKey];
35+
object = [unarchiver decodeObjectOfClasses:classes forKey:NSKeyedArchiveRootObjectKey];
3636
} @catch (NSException *exception) {
3737
if (outError) {
3838
*outError = [self archivingErrorWithException:exception];
@@ -50,6 +50,12 @@ + (nullable id)unarchivedObjectOfClass:(Class)class
5050
return object;
5151
}
5252

53+
+ (nullable id)unarchivedObjectOfClass:(Class)class
54+
fromData:(NSData *)data
55+
error:(NSError **)outError {
56+
return [self unarchivedObjectOfClasses:[NSSet setWithObject:class] fromData:data error:outError];
57+
}
58+
5359
+ (nullable NSData *)archivedDataWithRootObject:(id<NSCoding>)object error:(NSError **)outError {
5460
NSData *archiveData;
5561
#if __has_builtin(__builtin_available)

GoogleUtilities/SecureCoding/Public/GULSecureCoding.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ NS_ASSUME_NONNULL_BEGIN
2121
*/
2222
@interface GULSecureCoding : NSObject
2323

24+
+ (nullable id)unarchivedObjectOfClasses:(NSSet<Class> *)classes
25+
fromData:(NSData *)data
26+
error:(NSError **)outError;
27+
2428
+ (nullable id)unarchivedObjectOfClass:(Class)class
2529
fromData:(NSData *)data
2630
error:(NSError **)outError;

0 commit comments

Comments
 (0)