Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add PFObject.fromDictionary to create an object from dictionary #1834

Merged
merged 7 commits into from
Jan 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions Parse/Parse/Source/PFObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -880,6 +880,19 @@ NS_REQUIRES_PROPERTY_DEFINITIONS
withName:(NSString *)name
block:(nullable PFBooleanResultBlock)block;

///--------------------------------------
#pragma mark - Serialization
///--------------------------------------

/**
Creates a PFObject from a dictionary object.
@param dictionary Undecoded dictionary.
@param defaultClassName The className of the resulting object if none is given by the dictionary.
*/
+ (id)fromDictionary:(NSDictionary *)dictionary
defaultClassName:(NSString *)defaultClassName;

@end

NS_ASSUME_NONNULL_END
7 changes: 7 additions & 0 deletions Parse/Parse/Source/PFObject.m
Original file line number Diff line number Diff line change
Expand Up @@ -809,6 +809,13 @@ - (BOOL)resolveLocalId:(NSError *__autoreleasing*)error {
return YES;
}

+ (id)fromDictionary:(NSDictionary *)dictionary
defaultClassName:(NSString *)defaultClassName {
return [self _objectFromDictionary:dictionary
defaultClassName:defaultClassName
completeData:YES];
}

+ (id)_objectFromDictionary:(NSDictionary *)dictionary
defaultClassName:(NSString *)defaultClassName
completeData:(BOOL)completeData {
Expand Down
9 changes: 9 additions & 0 deletions Parse/Tests/Unit/ObjectUnitTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -361,4 +361,13 @@ -(void)testLocalRESTEncoding {
XCTAssertNil(error);
}

- (void)testFromDictionary {
NSDictionary *dict = @{ @"objectId": @"XYZ", @"score" : @1.0 };
PFObject *object = [PFObject fromDictionary:dict
defaultClassName:@"Test"];

XCTAssertEqualObjects(dict[@"objectId"], object.objectId);
XCTAssertEqualObjects(dict[@"score"], object[@"score"]);
}

@end