Skip to content

Commit

Permalink
Swift 3 (#70)
Browse files Browse the repository at this point in the history
* Update to Swift 3 syntax

* Rename some files

* Migrate tests to Swift

* Update copyright in headers

* Update sample code to Swift 3
  • Loading branch information
gonzalezreal committed Sep 21, 2016
1 parent 8987279 commit d5ca65e
Show file tree
Hide file tree
Showing 50 changed files with 1,197 additions and 1,265 deletions.
105 changes: 56 additions & 49 deletions Groot.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion Groot.xcodeproj/xcshareddata/xcschemes/Groot-Mac.xcscheme
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "0700"
LastUpgradeVersion = "0800"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand Down
2 changes: 1 addition & 1 deletion Groot.xcodeproj/xcshareddata/xcschemes/Groot.xcscheme
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "0700"
LastUpgradeVersion = "0800"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand Down
5 changes: 2 additions & 3 deletions Groot/GRTError.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// GRTError.h
//
// Copyright (c) 2014-2015 Guillermo Gonzalez
// Copyright (c) 2014-2016 Guillermo Gonzalez
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
Expand All @@ -26,6 +26,5 @@ extern NSString * const GRTErrorDomain;

typedef NS_ENUM(NSInteger, GRTError) {
GRTErrorEntityNotFound,
GRTErrorInvalidJSONObject,
GRTErrorIdentityNotFound __attribute__((deprecated("Not applicable")))
GRTErrorInvalidJSONObject
};
2 changes: 1 addition & 1 deletion Groot/GRTError.m
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// GRTError.m
//
// Copyright (c) 2014-2015 Guillermo Gonzalez
// Copyright (c) 2014-2016 Guillermo Gonzalez
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
Expand Down
30 changes: 1 addition & 29 deletions Groot/GRTJSONSerialization.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// GRTJSONSerialization.h
//
// Copyright (c) 2014-2015 Guillermo Gonzalez
// Copyright (c) 2014-2016 Guillermo Gonzalez
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -98,32 +98,4 @@ NS_ASSUME_NONNULL_BEGIN

@end

@interface GRTJSONSerialization (Deprecated)

+ (nullable id)insertObjectForEntityName:(NSString *)entityName
fromJSONDictionary:(NSDictionary *)JSONDictionary
inManagedObjectContext:(NSManagedObjectContext *)context
error:(NSError * __nullable * __nullable)error __attribute__((deprecated("Replaced by -objectWithEntityName:fromJSONDictionary:inContext:error:")));

+ (nullable NSArray *)insertObjectsForEntityName:(NSString *)entityName
fromJSONArray:(NSArray *)JSONArray
inManagedObjectContext:(NSManagedObjectContext *)context
error:(NSError * __nullable * __nullable)error __attribute__((deprecated("Replaced by -objectsWithEntityName:fromJSONArray:inContext:error:")));

+ (nullable id)mergeObjectForEntityName:(NSString *)entityName
fromJSONDictionary:(NSDictionary *)JSONDictionary
inManagedObjectContext:(NSManagedObjectContext *)context
error:(NSError * __nullable * __nullable)error __attribute__((deprecated("Replaced by -objectWithEntityName:fromJSONDictionary:inContext:error:")));

+ (nullable NSArray *)mergeObjectsForEntityName:(NSString *)entityName
fromJSONArray:(NSArray *)JSONArray
inManagedObjectContext:(NSManagedObjectContext *)context
error:(NSError * __nullable * __nullable)error __attribute__((deprecated("Replaced by -objectsWithEntityName:fromJSONArray:inContext:error:")));

+ (NSDictionary *)JSONDictionaryFromManagedObject:(NSManagedObject *)managedObject __attribute__((deprecated("Replaced by -JSONDictionaryFromObject:")));

+ (NSArray *)JSONArrayFromManagedObjects:(NSArray *)managedObjects __attribute__((deprecated("Replaced by -JSONArrayFromObjects:")));

@end

NS_ASSUME_NONNULL_END
2 changes: 1 addition & 1 deletion Groot/GRTJSONSerialization.m
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// GRTJSONSerialization.m
//
// Copyright (c) 2014-2015 Guillermo Gonzalez
// Copyright (c) 2014-2016 Guillermo Gonzalez
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
Expand Down
12 changes: 1 addition & 11 deletions Groot/GRTManagedStore.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// GRTManagedStore.h
//
// Copyright (c) 2014-2015 Guillermo Gonzalez
// Copyright (c) 2014-2016 Guillermo Gonzalez
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -95,14 +95,4 @@ NS_ASSUME_NONNULL_BEGIN

@end

@interface GRTManagedStore (Deprecated)

+ (instancetype)managedStoreWithModel:(nullable NSManagedObjectModel *)managedObjectModel __attribute__((deprecated("Replaced by -initWithModel:error:")));

+ (instancetype)managedStoreWithCacheName:(NSString *)cacheName __attribute__((deprecated("Replaced by +storeWithCacheName:error:")));

- (id)initWithPath:(nullable NSString *)path managedObjectModel:(nullable NSManagedObjectModel *)managedObjectModel __attribute__((deprecated("Replaced by -initWithURL:model:error:")));

@end

NS_ASSUME_NONNULL_END
2 changes: 1 addition & 1 deletion Groot/GRTManagedStore.m
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// GRTManagedStore.m
//
// Copyright (c) 2014-2015 Guillermo Gonzalez
// Copyright (c) 2014-2016 Guillermo Gonzalez
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
Expand Down
2 changes: 1 addition & 1 deletion Groot/Groot.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Groot.h
//
// Copyright (c) 2014 Guillermo Gonzalez
// Copyright (c) 2014-2016 Guillermo Gonzalez
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
Expand Down
200 changes: 97 additions & 103 deletions Groot/Groot.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Groot.swift
//
// Copyright (c) 2015 Guillermo Gonzalez
// Copyright (c) 2014-2016 Guillermo Gonzalez
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
Expand All @@ -23,140 +23,134 @@
import CoreData

extension NSManagedObjectContext {
internal func managedObjectModel() -> NSManagedObjectModel {
if let psc = persistentStoreCoordinator {
return psc.managedObjectModel

internal var managedObjectModel: NSManagedObjectModel? {
if let persistentStoreCoordinator = persistentStoreCoordinator {
return persistentStoreCoordinator.managedObjectModel
}

if let parent = parent {
return parent.managedObjectModel
}
return parentContext!.managedObjectModel()

return nil
}
}

extension NSManagedObject {
internal class func entityInManagedObjectContext(context: NSManagedObjectContext) -> NSEntityDescription {
let className = NSStringFromClass(self)
let model = context.managedObjectModel()


internal static func entity(inManagedObjectContext context: NSManagedObjectContext) -> NSEntityDescription {

guard let model = context.managedObjectModel else {
fatalError("Could not find managed object model for the provided context.")
}

let className = String(reflecting: self)

for entity in model.entities {
if entity.managedObjectClassName == className {
return entity
}
}

fatalError("Could not locate the entity for \(className).")
}
}

/**
Creates or updates a set of managed objects from JSON data.
- 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.
- returns: An array of managed objects
*/
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.
///
/// - parameter name: The name of an entity.
/// - parameter data: A data object containing JSON data.
/// - parameter context: The context into which to fetch or insert the managed objects.
///
/// - returns: An array of managed objects
public func objects(withEntityName name: String, fromJSONData data: Data, inContext context: NSManagedObjectContext) throws -> [NSManagedObject] {
return try GRTJSONSerialization.objects(withEntityName: name, fromJSONData: data, in: context)
}

/**
Creates or updates a set of managed objects from JSON data.
- parameter fromJSONData: A data object containing JSON data.
- parameter inContext: The context into which to fetch or insert the managed objects.
- returns: An array of managed objects.
*/
public func objectsFromJSONData<T: NSManagedObject>(data: NSData, inContext context: NSManagedObjectContext) throws -> [T] {
let entity = T.entityInManagedObjectContext(context)
let managedObjects = try objectsWithEntityName(entity.name!, fromJSONData: data, inContext: context)
/// Creates or updates a set of managed objects from JSON data.
///
/// - parameter data: A data object containing JSON data.
/// - parameter context: The context into which to fetch or insert the managed objects.
///
/// - returns: An array of managed objects.
public func objects<T: NSManagedObject>(fromJSONData data: Data, inContext context: NSManagedObjectContext) throws -> [T] {
let entity = T.entity(inManagedObjectContext: context)
let managedObjects = try objects(withEntityName: entity.name!, fromJSONData: data, inContext: context)

return managedObjects as! [T]
}

public typealias JSONDictionary = [String: AnyObject]

/**
Creates or updates a managed object from a JSON dictionary.
This method converts the specified JSON dictionary into a managed object of a given entity.
- 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.
- returns: A managed object.
*/
public func objectWithEntityName(name: String, fromJSONDictionary dictionary: JSONDictionary, inContext context: NSManagedObjectContext) throws -> NSManagedObject {
return try GRTJSONSerialization.objectWithEntityName(name, fromJSONDictionary: dictionary, inContext: context)
public typealias JSONDictionary = [String: Any]

/// Creates or updates a managed object from a JSON dictionary.
///
/// This method converts the specified JSON dictionary into a managed object of a given entity.
///
/// - parameter name: The name of an entity.
/// - parameter dictionary: A dictionary representing JSON data.
/// - parameter context: The context into which to fetch or insert the managed objects.
///
/// - returns: A managed object.
public func object(withEntityName name: String, fromJSONDictionary dictionary: JSONDictionary, inContext context: NSManagedObjectContext) throws -> NSManagedObject {
return try GRTJSONSerialization.object(withEntityName: name, fromJSONDictionary: dictionary, in: context)
}

/**
Creates or updates a managed object from a JSON dictionary.
This method converts the specified JSON dictionary into a managed object.
- parameter fromJSONDictionary: A dictionary representing JSON data.
- parameter inContext: The context into which to fetch or insert the managed objects.
- returns: A managed object.
*/
public func objectFromJSONDictionary<T: NSManagedObject>(dictionary: JSONDictionary, inContext context: NSManagedObjectContext) throws -> T {
let entity = T.entityInManagedObjectContext(context)
let managedObject = try objectWithEntityName(entity.name!, fromJSONDictionary: dictionary, inContext: context)
/// Creates or updates a managed object from a JSON dictionary.
///
/// This method converts the specified JSON dictionary into a managed object.
///
/// - parameter dictionary: A dictionary representing JSON data.
/// - parameter context: The context into which to fetch or insert the managed objects.
///
/// - returns: A managed object.
public func object<T: NSManagedObject>(fromJSONDictionary dictionary: JSONDictionary, inContext context: NSManagedObjectContext) throws -> T {
let entity = T.entity(inManagedObjectContext: context)
let managedObject = try object(withEntityName: entity.name!, fromJSONDictionary: dictionary, inContext: context)

return managedObject as! T
}

public typealias JSONArray = [AnyObject]

/**
Creates or updates a set of managed objects from a JSON array.
- 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.
- returns: An array of managed objects.
*/
public func objectsWithEntityName(name: String, fromJSONArray array: JSONArray, inContext context: NSManagedObjectContext) throws -> [NSManagedObject] {
return try GRTJSONSerialization.objectsWithEntityName(name, fromJSONArray: array, inContext: context)
public typealias JSONArray = [Any]

/// Creates or updates a set of managed objects from a JSON array.
///
/// - parameter name: The name of an entity.
/// - parameter array: An array representing JSON data.
/// - parameter context: The context into which to fetch or insert the managed objects.
///
/// - returns: An array of managed objects.
public func objects(withEntityName name: String, fromJSONArray array: JSONArray, inContext context: NSManagedObjectContext) throws -> [NSManagedObject] {
return try GRTJSONSerialization.objects(withEntityName: name, fromJSONArray: array, in: context)
}

/**
Creates or updates a set of managed objects from a JSON array.
- parameter fromJSONArray: An array representing JSON data.
- parameter context: The context into which to fetch or insert the managed objects.
- returns: An array of managed objects.
*/
public func objectsFromJSONArray<T: NSManagedObject>(array: JSONArray, inContext context: NSManagedObjectContext) throws -> [T] {
let entity = T.entityInManagedObjectContext(context)
let managedObjects = try objectsWithEntityName(entity.name!, fromJSONArray: array, inContext: context)
/// Creates or updates a set of managed objects from a JSON array.
///
/// - parameter array: An array representing JSON data.
/// - parameter context: The context into which to fetch or insert the managed objects.
///
/// - returns: An array of managed objects.
public func objects<T: NSManagedObject>(fromJSONArray array: JSONArray, inContext context: NSManagedObjectContext) throws -> [T] {
let entity = T.entity(inManagedObjectContext: context)
let managedObjects = try objects(withEntityName: entity.name!, fromJSONArray: array, inContext: context)

return managedObjects as! [T]
}

/**
Converts a managed object into a JSON representation.
- parameter object: The managed object to use for JSON serialization.
:return: A JSON dictionary.
*/
public func JSONDictionaryFromObject(object: NSManagedObject) -> JSONDictionary {
return GRTJSONSerialization.JSONDictionaryFromObject(object) as! JSONDictionary;
/// Converts a managed object into a JSON representation.
///
/// - parameter object: The managed object to use for JSON serialization.
///
/// - returns: A JSON dictionary.
public func json(fromObject object: NSManagedObject) -> JSONDictionary {
return GRTJSONSerialization.jsonDictionary(from: object) as! JSONDictionary;
}

/**
Converts an array of managed objects into a JSON representation.
- parameter objects: The array of managed objects to use for JSON serialization.
:return: A JSON array.
*/
public func JSONArrayFromObjects(objects: [NSManagedObject]) -> JSONArray {
return GRTJSONSerialization.JSONArrayFromObjects(objects)
/// Converts an array of managed objects into a JSON representation.
///
/// - parameter objects: The array of managed objects to use for JSON serialization.
///
/// - returns: A JSON array.
public func json(fromObjects objects: [NSManagedObject]) -> JSONArray {
return GRTJSONSerialization.jsonArray(from: objects)
}
2 changes: 1 addition & 1 deletion Groot/NSValueTransformer+Groot.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// NSValueTransformer+Groot.h
//
// Copyright (c) 2014-2015 Guillermo Gonzalez
// Copyright (c) 2014-2016 Guillermo Gonzalez
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
Expand Down
2 changes: 1 addition & 1 deletion Groot/NSValueTransformer+Groot.m
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// NSValueTransformer+Groot.h
//
// Copyright (c) 2014-2015 Guillermo Gonzalez
// Copyright (c) 2014-2016 Guillermo Gonzalez
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
Expand Down
Loading

0 comments on commit d5ca65e

Please sign in to comment.