Skip to content
This repository was archived by the owner on Feb 8, 2023. It is now read-only.

Commit ed25409

Browse files
committed
Initial conversion to Swift 3
1 parent 860289d commit ed25409

18 files changed

+321
-272
lines changed

CoreDataKit.xcodeproj/project.pbxproj

+4-4
Original file line numberDiff line numberDiff line change
@@ -518,7 +518,7 @@
518518
PRODUCT_NAME = "$(TARGET_NAME)";
519519
SKIP_INSTALL = YES;
520520
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
521-
SWIFT_VERSION = 2.3;
521+
SWIFT_VERSION = 3.0;
522522
};
523523
name = Debug;
524524
};
@@ -537,7 +537,7 @@
537537
PRODUCT_BUNDLE_IDENTIFIER = "nl.mathijskadijk.${PRODUCT_NAME:rfc1034identifier}";
538538
PRODUCT_NAME = "$(TARGET_NAME)";
539539
SKIP_INSTALL = YES;
540-
SWIFT_VERSION = 2.3;
540+
SWIFT_VERSION = 3.0;
541541
};
542542
name = Release;
543543
};
@@ -557,7 +557,7 @@
557557
METAL_ENABLE_DEBUG_INFO = YES;
558558
PRODUCT_BUNDLE_IDENTIFIER = "nl.mathijskadijk.${PRODUCT_NAME:rfc1034identifier}";
559559
PRODUCT_NAME = "$(TARGET_NAME)";
560-
SWIFT_VERSION = 2.3;
560+
SWIFT_VERSION = 3.0;
561561
};
562562
name = Debug;
563563
};
@@ -573,7 +573,7 @@
573573
METAL_ENABLE_DEBUG_INFO = NO;
574574
PRODUCT_BUNDLE_IDENTIFIER = "nl.mathijskadijk.${PRODUCT_NAME:rfc1034identifier}";
575575
PRODUCT_NAME = "$(TARGET_NAME)";
576-
SWIFT_VERSION = 2.3;
576+
SWIFT_VERSION = 3.0;
577577
};
578578
name = Release;
579579
};

CoreDataKit/CDK.swift

+17-7
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@
99
import CoreData
1010

1111
public enum CDKLogLevel {
12-
case DEBUG
13-
case INFO
14-
case WARN
15-
case ERROR
12+
case debug
13+
case info
14+
case warn
15+
case error
1616
}
1717

1818
public typealias Logger = (CDKLogLevel, String) -> Void
@@ -22,7 +22,7 @@ public typealias Logger = (CDKLogLevel, String) -> Void
2222
*/
2323
public class CDK : NSObject
2424
{
25-
private struct Holder {
25+
fileprivate struct Holder {
2626
static var sharedStack: CoreDataStack?
2727
static var sharedLogger: Logger = { _, message in print("[CoreDataKit] \(message)") }
2828
}
@@ -82,11 +82,21 @@ public class CDK : NSObject
8282

8383
:see: NSManagedObjectContext.performBlock()
8484
*/
85-
public class func performBlockOnBackgroundContext(block: PerformBlock, completionHandler: PerformBlockCompletionHandler?) {
85+
public class func performOnBackgroundContext(block: PerformBlock, completionHandler: PerformBlockCompletionHandler?) {
8686
sharedStack!.performBlockOnBackgroundContext(block, completionHandler: completionHandler)
8787
}
8888

89-
public class func performBlockOnBackgroundContext(block: PerformBlock) {
89+
@available(*, unavailable, renamed: "performOnBackgroundContext(block:completionHandler:)")
90+
public class func performBlockOnBackgroundContext(_ block: PerformBlock, completionHandler: PerformBlockCompletionHandler?) {
91+
fatalError()
92+
}
93+
94+
public class func performOnBackgroundContext(_ block: PerformBlock) {
9095
sharedStack!.performBlockOnBackgroundContext(block, completionHandler: nil)
9196
}
97+
98+
@available(*, unavailable, renamed: "performOnBackgroundContext(block:)")
99+
public class func performBlockOnBackgroundContext(_ block: PerformBlock) {
100+
fatalError()
101+
}
92102
}

CoreDataKit/CoreDataStack.swift

+18-8
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public class CoreDataStack: NSObject {
3232
self.rootContext = NSManagedObjectContext(persistentStoreCoordinator: self.persistentStoreCoordinator)
3333
self.rootContext.mergePolicy = NSMergeByPropertyObjectTrumpMergePolicy
3434

35-
self.mainThreadContext = NSManagedObjectContext(concurrencyType: .MainQueueConcurrencyType, parentContext: rootContext)
35+
self.mainThreadContext = NSManagedObjectContext(concurrencyType: .mainQueueConcurrencyType, parentContext: rootContext)
3636

3737
self.backgroundContext = self.mainThreadContext.createChildContext()
3838

@@ -56,23 +56,33 @@ public class CoreDataStack: NSObject {
5656

5757
:see: NSManagedObjectContext.performBlock()
5858
*/
59-
public func performBlockOnBackgroundContext(block: PerformBlock, completionHandler: PerformBlockCompletionHandler?) {
59+
public func performOnBackgroundContext(block: PerformBlock, completionHandler: PerformBlockCompletionHandler?) {
6060
backgroundContext.performBlock(block, completionHandler: completionHandler)
6161
}
6262

63-
public func performBlockOnBackgroundContext(block: PerformBlock) {
63+
@available(*, unavailable, renamed: "performOnBackgroundContext(block:completionHandler:)")
64+
public func performBlockOnBackgroundContext(_ block: PerformBlock, completionHandler: PerformBlockCompletionHandler?) {
65+
fatalError()
66+
}
67+
68+
public func performOnBackgroundContext(block: PerformBlock) {
6469
backgroundContext.performBlock(block, completionHandler: nil)
6570
}
6671

72+
@available(*, unavailable, renamed: "performOnBackgroundContext(block:)")
73+
public func performBlockOnBackgroundContext(_ block: PerformBlock, completionHandler: PerformBlockCompletionHandler?) {
74+
fatalError()
75+
}
76+
6777
/**
6878
Dumps some debug info about this stack to the console
6979
*/
7080
public func dumpStack() {
71-
CDK.sharedLogger(.DEBUG, "Stores: \(persistentStoreCoordinator.persistentStores)")
72-
CDK.sharedLogger(.DEBUG, " - Store coordinator: \(persistentStoreCoordinator.debugDescription)")
73-
CDK.sharedLogger(.DEBUG, " |- Root context: \(rootContext.debugDescription)")
74-
CDK.sharedLogger(.DEBUG, " |- Main thread context: \(mainThreadContext.debugDescription)")
75-
CDK.sharedLogger(.DEBUG, " |- Background context: \(backgroundContext.debugDescription)")
81+
CDK.sharedLogger(.debug, "Stores: \(persistentStoreCoordinator.persistentStores)")
82+
CDK.sharedLogger(.debug, " - Store coordinator: \(persistentStoreCoordinator.debugDescription)")
83+
CDK.sharedLogger(.debug, " |- Root context: \(rootContext.debugDescription)")
84+
CDK.sharedLogger(.debug, " |- Main thread context: \(mainThreadContext.debugDescription)")
85+
CDK.sharedLogger(.debug, " |- Background context: \(backgroundContext.debugDescription)")
7686
}
7787

7888
// MARK: Notification observers

CoreDataKit/Importing/CoreDataStack+Importing.swift

+8-8
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@ extension CoreDataStack {
1616
let entityUserInfoKeys = [IdentifierUserInfoKey]
1717

1818
for (entityName, entity) in persistentStoreCoordinator.managedObjectModel.entitiesByName {
19-
CDK.sharedLogger(.DEBUG, " ")
20-
CDK.sharedLogger(.DEBUG, "\(entityName):")
19+
CDK.sharedLogger(.debug, " ")
20+
CDK.sharedLogger(.debug, "\(entityName):")
2121

2222
for (_key, value) in entity.userInfo! {
2323
let key = _key as! String
2424

2525
if !entityUserInfoKeys.contains(key) {
26-
CDK.sharedLogger(.DEBUG, "\(key)\(value)")
26+
CDK.sharedLogger(.debug, "\(key)\(value)")
2727
}
2828
}
2929

@@ -44,7 +44,7 @@ extension CoreDataStack {
4444
}
4545
}
4646

47-
private func dumpPropertyDescription(property: NSPropertyDescription, asIdentifyingAttribute: Bool = false) {
47+
fileprivate func dumpPropertyDescription(_ property: NSPropertyDescription, asIdentifyingAttribute: Bool = false) {
4848
var propertyUserInfoKeys = [MappingUserInfoKey]
4949
for i in 0...MaxNumberedMappings+1 {
5050
propertyUserInfoKeys.append(MappingUserInfoKey + ".\(i)")
@@ -54,20 +54,20 @@ extension CoreDataStack {
5454
let relationshipUserInfoKeys = [RelationTypeUserInfoKey]
5555

5656
let identifying = asIdentifyingAttribute ? "" : " "
57-
let indexed = property.indexed ? "" : ""
58-
let optional = property.optional ? "?" : ""
57+
let indexed = property.isIndexed ? "" : ""
58+
let optional = property.isOptional ? "?" : ""
5959
let relationshipType = (property as? NSRelationshipDescription)?.relationType.rawValue
6060
let relationshipTypeDescription = relationshipType == nil ? "" : "\(relationshipType!)"
6161

62-
CDK.sharedLogger(.DEBUG, "\(identifying)\(indexed)\(property.name)\(optional)\(property.mappings)\(relationshipTypeDescription)")
62+
CDK.sharedLogger(.debug, "\(identifying)\(indexed)\(property.name)\(optional)\(property.mappings)\(relationshipTypeDescription)")
6363

6464
for (_key, value) in property.userInfo! {
6565
let key = _key as! String
6666

6767
if !propertyUserInfoKeys.contains(key) {
6868
if (property is NSAttributeDescription && !attributeUserInfoKeys.contains(key)) ||
6969
(property is NSRelationshipDescription && !relationshipUserInfoKeys.contains(key)) {
70-
CDK.sharedLogger(.DEBUG, "\(key)\(value)")
70+
CDK.sharedLogger(.debug, "\(key)\(value)")
7171
}
7272
}
7373
}

CoreDataKit/Importing/JsonDecode+Importing.swift

+16-16
Original file line numberDiff line numberDiff line change
@@ -10,58 +10,58 @@ import Foundation
1010

1111
// We use the decode functions from the JsonDecode+Importing.swift file
1212
extension String {
13-
static func decode(json : AnyObject) -> String? {
13+
static func decode(_ json : AnyObject) -> String? {
1414
return json as? String
1515
}
1616
}
1717

1818
extension Bool {
19-
static func decode(json : AnyObject) -> Bool? {
19+
static func decode(_ json : AnyObject) -> Bool? {
2020
return json as? Bool
2121
}
2222
}
2323

2424
extension Int {
25-
static func decode(json : AnyObject) -> Int? {
25+
static func decode(_ json : AnyObject) -> Int? {
2626
return json as? Int
2727
}
2828
}
2929

3030
extension Int64 {
31-
static func decode(json : AnyObject) -> Int64? {
31+
static func decode(_ json : AnyObject) -> Int64? {
3232
let number = json as? NSNumber
33-
return number.map { $0.longLongValue }
33+
return number.map { $0.int64Value }
3434
}
3535
}
3636

3737
extension Double {
38-
static func decode(json : AnyObject) -> Double? {
38+
static func decode(_ json : AnyObject) -> Double? {
3939
return json as? Double
4040
}
4141
}
4242

43-
extension NSData {
44-
class func decode(json: AnyObject) -> NSData? {
45-
return json as? NSData
43+
extension Data {
44+
static func decode(_ json: AnyObject) -> Data? {
45+
return json as? Data
4646
}
4747
}
4848

49-
extension NSDate {
49+
extension Date {
5050
struct DateFormatter {
51-
static let withTimeZone : NSDateFormatter = {
52-
let formatter = NSDateFormatter()
51+
static let withTimeZone : Foundation.DateFormatter = {
52+
let formatter = Foundation.DateFormatter()
5353
formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZZZZZ"
54-
formatter.locale = NSLocale(localeIdentifier: "en_US_POSIX")
54+
formatter.locale = Locale(identifier: "en_US_POSIX")
5555

5656
return formatter
5757
}()
5858
}
5959

60-
class func decode(json : AnyObject) -> NSDate? {
61-
if let date = json as? NSDate {
60+
static func decode(_ json : AnyObject) -> Date? {
61+
if let date = json as? Date {
6262
return date
6363
} else if let dateString = json as? String {
64-
return DateFormatter.withTimeZone.dateFromString(dateString)
64+
return DateFormatter.withTimeZone.date(from: dateString)
6565
}
6666

6767
return nil

CoreDataKit/Importing/NSAttributeDescription+Importing.swift

+22-17
Original file line numberDiff line numberDiff line change
@@ -17,45 +17,50 @@ extension NSAttributeDescription
1717

1818
- returns: The transformed value or nil if the value can't for into this attribute
1919
*/
20-
func transformValue(value: AnyObject) -> AnyObject? {
20+
func transform(value: AnyObject) -> AnyObject? {
2121
switch attributeType {
22-
case .Integer16AttributeType:
22+
case .integer16AttributeType:
2323
fallthrough
24-
case .Integer32AttributeType:
24+
case .integer32AttributeType:
2525
return Int.decode(value)
2626

27-
case .Integer64AttributeType:
27+
case .integer64AttributeType:
2828
if let int64 = Int64.decode(value) {
29-
return NSNumber(longLong: int64)
29+
return NSNumber(value: int64 as Int64)
3030
} else {
3131
return nil
3232
}
3333

34-
case .DecimalAttributeType:
34+
case .decimalAttributeType:
3535
fallthrough
36-
case .DoubleAttributeType:
36+
case .doubleAttributeType:
3737
fallthrough
38-
case .FloatAttributeType:
38+
case .floatAttributeType:
3939
return Double.decode(value)
4040

41-
case .StringAttributeType:
41+
case .stringAttributeType:
4242
return String.decode(value)
4343

44-
case .BooleanAttributeType:
44+
case .booleanAttributeType:
4545
return Bool.decode(value)
4646

47-
case .DateAttributeType:
48-
return NSDate.decode(value)
47+
case .dateAttributeType:
48+
return Date.decode(value)
4949

50-
case .BinaryDataAttributeType:
51-
return NSData.decode(value)
50+
case .binaryDataAttributeType:
51+
return Data.decode(value)
5252

53-
case .UndefinedAttributeType:
53+
case .undefinedAttributeType:
5454
fallthrough
55-
case .TransformableAttributeType:
55+
case .transformableAttributeType:
5656
fallthrough
57-
case .ObjectIDAttributeType:
57+
case .objectIDAttributeType:
5858
return nil
5959
}
6060
}
61+
62+
@available(*, unavailable, renamed: "transform(value:)")
63+
func transformValue(_ value: AnyObject) -> AnyObject? {
64+
fatalError()
65+
}
6166
}

CoreDataKit/Importing/NSEntityDescription+Importing.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@ extension NSEntityDescription
2121
return identifyingAttribute
2222
}
2323

24-
let error = CoreDataKitError.ImportError(description: "Found \(IdentifierUserInfoKey) with value '\(identifyingAttributeName)' but that isn't a valid attribute name")
24+
let error = CoreDataKitError.importError(description: "Found \(IdentifierUserInfoKey) with value '\(identifyingAttributeName)' but that isn't a valid attribute name")
2525
throw error
2626
} else if let superEntity = self.superentity {
2727
return try superEntity.identifyingAttribute()
2828
}
2929

30-
let error = CoreDataKitError.ImportError(description: "No \(IdentifierUserInfoKey) value found on \(name)")
30+
let error = CoreDataKitError.importError(description: "No \(IdentifierUserInfoKey) value found on \(name)")
3131
throw error
3232
}
3333
}

0 commit comments

Comments
 (0)