diff --git a/Groot.xcodeproj/project.pbxproj b/Groot.xcodeproj/project.pbxproj index 84eb79b..28b1480 100644 --- a/Groot.xcodeproj/project.pbxproj +++ b/Groot.xcodeproj/project.pbxproj @@ -465,7 +465,9 @@ B4DC1AE31AA9CA5E00F67403 /* Project object */ = { isa = PBXProject; attributes = { - LastUpgradeCheck = 0610; + LastSwiftMigration = 0700; + LastSwiftUpdateCheck = 0700; + LastUpgradeCheck = 0700; ORGANIZATIONNAME = "Guillermo Gonzalez"; TargetAttributes = { B40873611B5AB6930063F150 = { @@ -653,6 +655,7 @@ INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks"; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks @loader_path/Frameworks"; MACOSX_DEPLOYMENT_TARGET = 10.9; + PRODUCT_BUNDLE_IDENTIFIER = "com.gonzalezreal.$(PRODUCT_NAME:rfc1034identifier)"; PRODUCT_NAME = Groot; SDKROOT = macosx; SKIP_INSTALL = YES; @@ -675,6 +678,7 @@ INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks"; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks @loader_path/Frameworks"; MACOSX_DEPLOYMENT_TARGET = 10.9; + PRODUCT_BUNDLE_IDENTIFIER = "com.gonzalezreal.$(PRODUCT_NAME:rfc1034identifier)"; PRODUCT_NAME = Groot; SDKROOT = macosx; SKIP_INSTALL = YES; @@ -698,6 +702,7 @@ INFOPLIST_FILE = GrootTests/Info.plist; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks @loader_path/../Frameworks"; MACOSX_DEPLOYMENT_TARGET = 10.10; + PRODUCT_BUNDLE_IDENTIFIER = "com.gonzalezreal.$(PRODUCT_NAME:rfc1034identifier)"; PRODUCT_NAME = GrootTests; SDKROOT = macosx; }; @@ -717,6 +722,7 @@ INFOPLIST_FILE = GrootTests/Info.plist; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks @loader_path/../Frameworks"; MACOSX_DEPLOYMENT_TARGET = 10.10; + PRODUCT_BUNDLE_IDENTIFIER = "com.gonzalezreal.$(PRODUCT_NAME:rfc1034identifier)"; PRODUCT_NAME = GrootTests; SDKROOT = macosx; }; @@ -743,6 +749,7 @@ COPY_PHASE_STRIP = NO; CURRENT_PROJECT_VERSION = 1; ENABLE_STRICT_OBJC_MSGSEND = YES; + ENABLE_TESTABILITY = YES; GCC_C_LANGUAGE_STANDARD = gnu99; GCC_DYNAMIC_NO_PIC = NO; GCC_OPTIMIZATION_LEVEL = 0; @@ -817,6 +824,7 @@ INFOPLIST_FILE = Groot/Info.plist; INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks"; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; + PRODUCT_BUNDLE_IDENTIFIER = "com.gonzalezreal.$(PRODUCT_NAME:rfc1034identifier)"; PRODUCT_NAME = "$(TARGET_NAME)"; SKIP_INSTALL = YES; SWIFT_OPTIMIZATION_LEVEL = "-Onone"; @@ -835,6 +843,7 @@ INFOPLIST_FILE = Groot/Info.plist; INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks"; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; + PRODUCT_BUNDLE_IDENTIFIER = "com.gonzalezreal.$(PRODUCT_NAME:rfc1034identifier)"; PRODUCT_NAME = "$(TARGET_NAME)"; SKIP_INSTALL = YES; TARGETED_DEVICE_FAMILY = "1,2"; @@ -855,6 +864,7 @@ ); INFOPLIST_FILE = GrootTests/Info.plist; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; + PRODUCT_BUNDLE_IDENTIFIER = "com.gonzalezreal.$(PRODUCT_NAME:rfc1034identifier)"; PRODUCT_NAME = "$(TARGET_NAME)"; SWIFT_OPTIMIZATION_LEVEL = "-Onone"; }; @@ -870,6 +880,7 @@ ); INFOPLIST_FILE = GrootTests/Info.plist; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; + PRODUCT_BUNDLE_IDENTIFIER = "com.gonzalezreal.$(PRODUCT_NAME:rfc1034identifier)"; PRODUCT_NAME = "$(TARGET_NAME)"; }; name = Release; diff --git a/Groot.xcodeproj/xcshareddata/xcschemes/Groot-Mac.xcscheme b/Groot.xcodeproj/xcshareddata/xcschemes/Groot-Mac.xcscheme index c0643d6..2415f62 100644 --- a/Groot.xcodeproj/xcshareddata/xcschemes/Groot-Mac.xcscheme +++ b/Groot.xcodeproj/xcshareddata/xcschemes/Groot-Mac.xcscheme @@ -1,6 +1,6 @@ + shouldUseLaunchSchemeArgsEnv = "YES"> @@ -62,15 +62,18 @@ ReferencedContainer = "container:Groot.xcodeproj"> + + + shouldUseLaunchSchemeArgsEnv = "YES"> @@ -62,15 +62,18 @@ ReferencedContainer = "container:Groot.xcodeproj"> + + *)objectsWithEntityName:(NSString *)entityName + fromJSONData:(NSData *)data + inContext:(NSManagedObjectContext *)context + error:(NSError * __nullable * __nullable)error; /** Creates or updates a managed object from a JSON dictionary. @@ -57,10 +57,10 @@ NS_ASSUME_NONNULL_BEGIN @return A managed object, or `nil` if an error occurs. */ -+ (nullable id)objectWithEntityName:(NSString *)entityName - fromJSONDictionary:(NSDictionary *)JSONDictionary - inContext:(NSManagedObjectContext *)context - error:(NSError * __nullable * __nullable)error; ++ (nullable __kindof NSManagedObject *)objectWithEntityName:(NSString *)entityName + fromJSONDictionary:(NSDictionary *)JSONDictionary + inContext:(NSManagedObjectContext *)context + error:(NSError * __nullable * __nullable)error; /** Creates or updates a set of managed objects from a JSON array. @@ -73,10 +73,10 @@ NS_ASSUME_NONNULL_BEGIN @return An array of managed objects, or `nil` if an error occurs. */ -+ (nullable NSArray *)objectsWithEntityName:(NSString *)entityName - fromJSONArray:(NSArray *)JSONArray - inContext:(NSManagedObjectContext *)context - error:(NSError * __nullable * __nullable)error; ++ (nullable NSArray<__kindof NSManagedObject *> *)objectsWithEntityName:(NSString *)entityName + fromJSONArray:(NSArray *)JSONArray + inContext:(NSManagedObjectContext *)context + error:(NSError * __nullable * __nullable)error; /** Converts a managed object into a JSON representation. diff --git a/Groot/GRTJSONSerialization.m b/Groot/GRTJSONSerialization.m index a3ae820..af5d896 100644 --- a/Groot/GRTJSONSerialization.m +++ b/Groot/GRTJSONSerialization.m @@ -30,10 +30,10 @@ @implementation GRTJSONSerialization -+ (nullable NSArray *)objectsWithEntityName:(NSString *)entityName - fromJSONData:(NSData *)data - inContext:(NSManagedObjectContext *)context - error:(NSError *__autoreleasing __nullable * __nullable)outError ++ (nullable NSArray<__kindof NSManagedObject *> *)objectsWithEntityName:(NSString *)entityName + fromJSONData:(NSData *)data + inContext:(NSManagedObjectContext *)context + error:(NSError *__autoreleasing __nullable * __nullable)outError { NSError *error = nil; id parsedJSON = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error]; @@ -55,10 +55,10 @@ + (nullable NSArray *)objectsWithEntityName:(NSString *)entityName return [self objectsWithEntityName:entityName fromJSONArray:array inContext:context error:outError]; } -+ (nullable id)objectWithEntityName:(NSString *)entityName - fromJSONDictionary:(NSDictionary *)JSONDictionary - inContext:(NSManagedObjectContext *)context - error:(NSError *__autoreleasing __nullable * __nullable)outError ++ (nullable __kindof NSManagedObject *)objectWithEntityName:(NSString *)entityName + fromJSONDictionary:(NSDictionary *)JSONDictionary + inContext:(NSManagedObjectContext *)context + error:(NSError *__autoreleasing __nullable * __nullable)outError { NSError *error = nil; NSEntityDescription *entity = [NSEntityDescription grt_entityForName:entityName inContext:context error:&error]; @@ -75,10 +75,10 @@ + (nullable id)objectWithEntityName:(NSString *)entityName error:outError].firstObject; } -+ (nullable NSArray *)objectsWithEntityName:(NSString *)entityName - fromJSONArray:(NSArray *)JSONArray - inContext:(NSManagedObjectContext *)context - error:(NSError *__autoreleasing __nullable * __nullable)outError ++ (nullable NSArray<__kindof NSManagedObject *> *)objectsWithEntityName:(NSString *)entityName + fromJSONArray:(NSArray *)JSONArray + inContext:(NSManagedObjectContext *)context + error:(NSError *__autoreleasing __nullable * __nullable)outError { NSError *error = nil; NSEntityDescription *entity = [NSEntityDescription grt_entityForName:entityName inContext:context error:&error]; diff --git a/Groot/GRTManagedStore.m b/Groot/GRTManagedStore.m index 4d96034..14b83f5 100644 --- a/Groot/GRTManagedStore.m +++ b/Groot/GRTManagedStore.m @@ -60,6 +60,10 @@ - (NSURL *)URL { return store.URL; } +- (instancetype)init { + return [self initWithURL:nil model:[NSManagedObjectModel mergedModelFromBundles:nil] error:NULL]; +} + - (nullable instancetype)initWithURL:(nullable NSURL *)URL model:(NSManagedObjectModel *)managedObjectModel error:(NSError *__autoreleasing __nullable * __nullable)outError diff --git a/Groot/Groot.swift b/Groot/Groot.swift index ce5fb4f..3fe68f2 100644 --- a/Groot/Groot.swift +++ b/Groot/Groot.swift @@ -36,9 +36,8 @@ extension NSManagedObject { internal class func entityInManagedObjectContext(context: NSManagedObjectContext) -> NSEntityDescription { let className = NSStringFromClass(self) let model = context.managedObjectModel() - let entities = model.entities as! [NSEntityDescription] - for entity in entities { + for entity in model.entities { if entity.managedObjectClassName == className { return entity } @@ -51,29 +50,29 @@ extension NSManagedObject { /** Creates or updates a set of managed objects from JSON data. - :param: entityName The name of an entity. - :param: fromJSONData A data object containing JSON data. - :param: inContext The context into which to fetch or insert the managed objects. - :param: error If an error occurs, upon return contains an NSError object that describes the problem. + - parameter entityName: The name of an entity. + - parameter fromJSONData: A data object containing JSON data. + - parameter inContext: The context into which to fetch or insert the managed objects. - :return: An array of managed objects, or `nil` if an error occurs. + - returns: An array of managed objects */ -public func objectsWithEntityName(name: String, fromJSONData data: NSData, inContext context: NSManagedObjectContext, error outError: NSErrorPointer) -> [NSManagedObject]? { - return GRTJSONSerialization.objectsWithEntityName(name, fromJSONData: data, inContext: context, error: outError) as? [NSManagedObject] +public func objectsWithEntityName(name: String, fromJSONData data: NSData, inContext context: NSManagedObjectContext) throws -> [NSManagedObject] { + return try GRTJSONSerialization.objectsWithEntityName(name, fromJSONData: data, inContext: context) } /** Creates or updates a set of managed objects from JSON data. - :param: fromJSONData A data object containing JSON data. - :param: inContext The context into which to fetch or insert the managed objects. - :param: error If an error occurs, upon return contains an NSError object that describes the problem. + - parameter fromJSONData: A data object containing JSON data. + - parameter inContext: The context into which to fetch or insert the managed objects. - :return: An array of managed objects, or `nil` if an error occurs. + - returns: An array of managed objects. */ -public func objectsFromJSONData(data: NSData, inContext context: NSManagedObjectContext, error outError: NSErrorPointer) -> [T]? { +public func objectsFromJSONData(data: NSData, inContext context: NSManagedObjectContext) throws -> [T] { let entity = T.entityInManagedObjectContext(context) - return objectsWithEntityName(entity.name!, fromJSONData: data, inContext: context, error: outError) as? [T] + let managedObjects = try objectsWithEntityName(entity.name!, fromJSONData: data, inContext: context) + + return managedObjects as! [T] } public typealias JSONDictionary = [String: AnyObject] @@ -83,15 +82,14 @@ public typealias JSONDictionary = [String: AnyObject] This method converts the specified JSON dictionary into a managed object of a given entity. - :param: entityName The name of an entity. - :param: fromJSONDictionary A dictionary representing JSON data. - :param: inContext The context into which to fetch or insert the managed objects. - :param: error If an error occurs, upon return contains an NSError object that describes the problem. + - parameter entityName: The name of an entity. + - parameter fromJSONDictionary: A dictionary representing JSON data. + - parameter inContext: The context into which to fetch or insert the managed objects. - :return: A managed object, or `nil` if an error occurs. + - returns: A managed object. */ -public func objectWithEntityName(name: String, fromJSONDictionary dictionary: JSONDictionary, inContext context: NSManagedObjectContext, error outError: NSErrorPointer) -> NSManagedObject? { - return GRTJSONSerialization.objectWithEntityName(name, fromJSONDictionary: dictionary, inContext: context, error: outError) as? NSManagedObject +public func objectWithEntityName(name: String, fromJSONDictionary dictionary: JSONDictionary, inContext context: NSManagedObjectContext) throws -> NSManagedObject { + return try GRTJSONSerialization.objectWithEntityName(name, fromJSONDictionary: dictionary, inContext: context) } /** @@ -99,15 +97,16 @@ public func objectWithEntityName(name: String, fromJSONDictionary dictionary: JS This method converts the specified JSON dictionary into a managed object. - :param: fromJSONDictionary A dictionary representing JSON data. - :param: inContext The context into which to fetch or insert the managed objects. - :param: error If an error occurs, upon return contains an NSError object that describes the problem. + - parameter fromJSONDictionary: A dictionary representing JSON data. + - parameter inContext: The context into which to fetch or insert the managed objects. - :return: A managed object, or `nil` if an error occurs. + - returns: A managed object. */ -public func objectFromJSONDictionary(dictionary: JSONDictionary, inContext context: NSManagedObjectContext, error outError: NSErrorPointer) -> T? { +public func objectFromJSONDictionary(dictionary: JSONDictionary, inContext context: NSManagedObjectContext) throws -> T { let entity = T.entityInManagedObjectContext(context) - return objectWithEntityName(entity.name!, fromJSONDictionary: dictionary, inContext: context, error: outError) as? T; + let managedObject = try objectWithEntityName(entity.name!, fromJSONDictionary: dictionary, inContext: context) + + return managedObject as! T } public typealias JSONArray = [AnyObject] @@ -115,35 +114,35 @@ public typealias JSONArray = [AnyObject] /** Creates or updates a set of managed objects from a JSON array. - :param: entityName The name of an entity. - :param: fromJSONArray An array representing JSON data. - :param: context The context into which to fetch or insert the managed objects. - :param: error If an error occurs, upon return contains an NSError object that describes the problem. + - parameter entityName: The name of an entity. + - parameter fromJSONArray: An array representing JSON data. + - parameter context: The context into which to fetch or insert the managed objects. - :return: An array of managed objects, or `nil` if an error occurs. + - returns: An array of managed objects. */ -public func objectsWithEntityName(name: String, fromJSONArray array: JSONArray, inContext context: NSManagedObjectContext, error outError: NSErrorPointer) -> [NSManagedObject]? { - return GRTJSONSerialization.objectsWithEntityName(name, fromJSONArray: array, inContext: context, error: outError) as? [NSManagedObject] +public func objectsWithEntityName(name: String, fromJSONArray array: JSONArray, inContext context: NSManagedObjectContext) throws -> [NSManagedObject] { + return try GRTJSONSerialization.objectsWithEntityName(name, fromJSONArray: array, inContext: context) } /** Creates or updates a set of managed objects from a JSON array. - :param: fromJSONArray An array representing JSON data. - :param: context The context into which to fetch or insert the managed objects. - :param: error If an error occurs, upon return contains an NSError object that describes the problem. + - parameter fromJSONArray: An array representing JSON data. + - parameter context: The context into which to fetch or insert the managed objects. - :return: An array of managed objects, or `nil` if an error occurs. + - returns: An array of managed objects. */ -public func objectsFromJSONArray(array: JSONArray, inContext context: NSManagedObjectContext, error outError: NSErrorPointer) -> [T]? { +public func objectsFromJSONArray(array: JSONArray, inContext context: NSManagedObjectContext) throws -> [T] { let entity = T.entityInManagedObjectContext(context) - return objectsWithEntityName(entity.name!, fromJSONArray: array, inContext: context, error: outError) as? [T] + let managedObjects = try objectsWithEntityName(entity.name!, fromJSONArray: array, inContext: context) + + return managedObjects as! [T] } /** Converts a managed object into a JSON representation. - :param: object The managed object to use for JSON serialization. + - parameter object: The managed object to use for JSON serialization. :return: A JSON dictionary. */ @@ -154,7 +153,7 @@ public func JSONDictionaryFromObject(object: NSManagedObject) -> JSONDictionary /** Converts an array of managed objects into a JSON representation. - :param: objects The array of managed objects to use for JSON serialization. + - parameter objects: The array of managed objects to use for JSON serialization. :return: A JSON array. */ diff --git a/Groot/Info.plist b/Groot/Info.plist index fa61c01..d3de8ee 100644 --- a/Groot/Info.plist +++ b/Groot/Info.plist @@ -7,7 +7,7 @@ CFBundleExecutable $(EXECUTABLE_NAME) CFBundleIdentifier - com.gonzalezreal.$(PRODUCT_NAME:rfc1034identifier) + $(PRODUCT_BUNDLE_IDENTIFIER) CFBundleInfoDictionaryVersion 6.0 CFBundleName diff --git a/Groot/NSValueTransformer+Groot.swift b/Groot/NSValueTransformer+Groot.swift index b3c164b..22adf5a 100644 --- a/Groot/NSValueTransformer+Groot.swift +++ b/Groot/NSValueTransformer+Groot.swift @@ -26,8 +26,8 @@ extension NSValueTransformer { /** Registers a value transformer with a given name and transform function. - :param: name The name of the transformer. - :param: transform The function that performs the transformation. + - parameter name: The name of the transformer. + - parameter transform: The function that performs the transformation. */ public class func setValueTransformerWithName(name: String, transform: (T) -> (U?)) { grt_setValueTransformerWithName(name) { value in @@ -40,9 +40,9 @@ extension NSValueTransformer { /** Registers a reversible value transformer with a given name and transform functions. - :param: name The name of the transformer. - :param: transform The function that performs the forward transformation. - :param: reverseTransform The function that performs the reverse transformation. + - parameter name: The name of the transformer. + - parameter transform: The function that performs the forward transformation. + - parameter reverseTransform: The function that performs the reverse transformation. */ public class func setValueTransformerWithName(name: String, transform: (T) -> (U?), reverseTransform: (U) -> (T?)) { grt_setValueTransformerWithName(name, transformBlock: { value in @@ -62,8 +62,8 @@ extension NSValueTransformer { Dictionary transformers can be associated with Core Data entities in the user info dictionary by using the `JSONDictionaryTransformerName` key. - :param: name The name of the transformer. - :param: transform The function that performs the transformation. + - parameter name: The name of the transformer. + - parameter transform: The function that performs the transformation. */ public class func setDictionaryTransformerWithName(name: String, transform: ([String: AnyObject]) -> ([String: AnyObject]?)) { grt_setDictionaryTransformerWithName(name) { value in @@ -82,8 +82,8 @@ extension NSValueTransformer { Entity mappers can be associated with abstract core data entities in the user info dictionary by using the `entityMapperName` key. - :param: name The name of the mapper. - :param: map The function that performs the mapping. + - parameter name: The name of the mapper. + - parameter map: The function that performs the mapping. */ public class func setEntityMapperWithName(name: String, map: ([String: AnyObject]) -> (String?)) { grt_setEntityMapperWithName(name) { value in diff --git a/Groot/Private/GRTValueTransformer.m b/Groot/Private/GRTValueTransformer.m index 0b53fdf..3094ec9 100644 --- a/Groot/Private/GRTValueTransformer.m +++ b/Groot/Private/GRTValueTransformer.m @@ -52,7 +52,7 @@ + (Class)transformedValueClass { return NSObject.class; } -- (id)transformedValue:(id)value { +- (nullable id)transformedValue:(nullable id)value { if (value != nil) { return self.transformBlock(value); } @@ -87,7 +87,7 @@ + (BOOL)allowsReverseTransformation { return YES; } -- (id)reverseTransformedValue:(id)value { +- (nullable id)reverseTransformedValue:(nullable id)value { if (value != nil) { return self.reverseTransformBlock(value); } diff --git a/GrootTests/Info.plist b/GrootTests/Info.plist index b76f4ac..ba72822 100644 --- a/GrootTests/Info.plist +++ b/GrootTests/Info.plist @@ -7,7 +7,7 @@ CFBundleExecutable $(EXECUTABLE_NAME) CFBundleIdentifier - com.gonzalezreal.$(PRODUCT_NAME:rfc1034identifier) + $(PRODUCT_BUNDLE_IDENTIFIER) CFBundleInfoDictionaryVersion 6.0 CFBundleName diff --git a/GrootTests/NSValueTransformerTests.swift b/GrootTests/NSValueTransformerTests.swift index 150a449..e0a3179 100644 --- a/GrootTests/NSValueTransformerTests.swift +++ b/GrootTests/NSValueTransformerTests.swift @@ -19,10 +19,17 @@ class NSValueTransformerTests: XCTestCase { NSValueTransformer.setValueTransformerWithName("testTransformer", transform: toString) let transformer = NSValueTransformer(forName: "testTransformer")! - XCTAssertFalse(transformer.dynamicType.allowsReverseTransformation(), "should not allow reverse transformation") - XCTAssertEqual("42", transformer.transformedValue(42) as! String, "should call the transform function") - XCTAssertNil(transformer.transformedValue(nil), "should handle nil values") - XCTAssertNil(transformer.transformedValue("unexpected"), "should handle unsupported values") + let reversible = transformer.dynamicType.allowsReverseTransformation() + XCTAssertFalse(reversible, "should not allow reverse transformation") + + let fortyTwo = transformer.transformedValue(42) as? String + XCTAssertEqual("42", fortyTwo, "should call the transform function") + + let nilValue = transformer.transformedValue(nil) + XCTAssertNil(nilValue, "should handle nil values") + + let unexpected = transformer.transformedValue("unexpected") + XCTAssertNil(unexpected, "should handle unsupported values") } func testReversibleValueTransformer() { @@ -31,19 +38,32 @@ class NSValueTransformerTests: XCTestCase { } func toInt(value: String) -> Int? { - return value.toInt() + return Int(value) } NSValueTransformer.setValueTransformerWithName("testReversibleTransformer", transform: toString, reverseTransform: toInt) let transformer = NSValueTransformer(forName: "testReversibleTransformer")! - XCTAssertTrue(transformer.dynamicType.allowsReverseTransformation(), "should not allow reverse transformation") - XCTAssertEqual("42", transformer.transformedValue(42) as! String, "should call the transform function") - XCTAssertNil(transformer.transformedValue(nil), "should handle nil values") - XCTAssertNil(transformer.transformedValue("unexpected"), "should handle unsupported values") - XCTAssertEqual(42, transformer.reverseTransformedValue("42") as! Int, "should call the reverse transform function") - XCTAssertNil(transformer.reverseTransformedValue(nil), "should handle nil values") - XCTAssertNil(transformer.reverseTransformedValue("not a number"), "should handle unsupported values") + let reversible = transformer.dynamicType.allowsReverseTransformation() + XCTAssertTrue(reversible, "should allow reverse transformation") + + let fortyTwo = transformer.transformedValue(42) as? String + XCTAssertEqual("42", fortyTwo, "should call the transform function") + + let nilValue = transformer.transformedValue(nil) + XCTAssertNil(nilValue, "should handle nil values") + + let unexpected = transformer.transformedValue("unexpected") + XCTAssertNil(unexpected, "should handle unsupported values") + + let reversedFortyTwo = transformer.reverseTransformedValue("42") as? Int + XCTAssertEqual(42, reversedFortyTwo, "should call the reverse transform function") + + let reversedNilValue = transformer.reverseTransformedValue(nil) + XCTAssertNil(reversedNilValue, "should handle nil values") + + let reversedUnexpected = transformer.reverseTransformedValue("not a number") + XCTAssertNil(reversedUnexpected, "should handle unsupported values") } func testDictionaryTransformer() { @@ -85,8 +105,8 @@ class NSValueTransformerTests: XCTestCase { NSValueTransformer.setEntityMapperWithName("testEntityMapper", map: entityForJSONDictionary) let transformer = NSValueTransformer(forName: "testEntityMapper")! - XCTAssertEqual("ConcreteA", transformer.transformedValue(["type": "A"]) as! String, "should call the transform function") - XCTAssertEqual("ConcreteB", transformer.transformedValue(["type": "B"]) as! String, "should call the transform function") + XCTAssertEqual("ConcreteA", transformer.transformedValue(["type": "A"]) as? String, "should call the transform function") + XCTAssertEqual("ConcreteB", transformer.transformedValue(["type": "B"]) as? String, "should call the transform function") XCTAssertNil(transformer.transformedValue(nil), "should handle nil values") } } diff --git a/README.md b/README.md index eeb26c5..92cc1b5 100644 --- a/README.md +++ b/README.md @@ -157,7 +157,7 @@ let batmanJSON: JSONObject = [ ] ] -let batman: Character = objectFromJSONDictionary(batmanJSON, inContext: context, error: &error) +let batman: Character = try objectFromJSONDictionary(batmanJSON, inContext: context) ``` ```objc