Skip to content

Commit 424db8f

Browse files
committed
Extended example
1 parent 2dc72dd commit 424db8f

File tree

19 files changed

+626
-406
lines changed

19 files changed

+626
-406
lines changed

CoreDataMigration-Example.xcodeproj/project.pbxproj

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@
77
objects = {
88

99
/* Begin PBXBuildFile section */
10-
3D70ED0721ED328D001C2516 /* Migration2to3ModelMapping.xcmappingmodel in Sources */ = {isa = PBXBuildFile; fileRef = 3D70ED0621ED328D001C2516 /* Migration2to3ModelMapping.xcmappingmodel */; };
1110
3D8E52F521E0EF2800FE1D35 /* FileManager+Helper.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3D8E52F421E0EF2800FE1D35 /* FileManager+Helper.swift */; };
1211
3D8E52F721E0F98500FE1D35 /* NSManagedObjectContext+Helper.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3D8E52F621E0F98500FE1D35 /* NSManagedObjectContext+Helper.swift */; };
12+
3DACAAD221EE9D5D00309A75 /* PostSectionWriterTableViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3DACAAD121EE9D5D00309A75 /* PostSectionWriterTableViewCell.swift */; };
13+
3DD99ED021F1118700CB4B6E /* Migration2to3ModelMapping.xcmappingmodel in Sources */ = {isa = PBXBuildFile; fileRef = 3DD99ECF21F1118700CB4B6E /* Migration2to3ModelMapping.xcmappingmodel */; };
1314
3DDB26C921EBF87E00388AEE /* PostWriterViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3DDB26C821EBF87E00388AEE /* PostWriterViewController.swift */; };
1415
3DDB26CB21EC00FE00388AEE /* PostViewerViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3DDB26CA21EC00FE00388AEE /* PostViewerViewController.swift */; };
1516
431DCEAE1F67EC9E00CF6316 /* CoreDataMigration_Example.xcdatamodeld in Sources */ = {isa = PBXBuildFile; fileRef = 431DCEAA1F67EC7100CF6316 /* CoreDataMigration_Example.xcdatamodeld */; };
@@ -27,7 +28,7 @@
2728
43370DBE1F66F0DF006188EC /* CoreDataManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 43370DBB1F66F0C0006188EC /* CoreDataManager.swift */; };
2829
4345D4EE1F67E0FE00027D11 /* UIColor+Hex.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4345D4ED1F67E0FE00027D11 /* UIColor+Hex.swift */; };
2930
4345D4F01F67E10700027D11 /* UIColor+Random.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4345D4EF1F67E10700027D11 /* UIColor+Random.swift */; };
30-
4345D4F51F67E1FC00027D11 /* CGFloat+Random.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4345D4F41F67E1FC00027D11 /* CGFloat+Random.swift */; };
31+
C23BD45A21F08A350039A36B /* PostSectionViewerTableViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = C23BD45921F08A350039A36B /* PostSectionViewerTableViewCell.swift */; };
3132
C28553DF21DCF5000004C7BA /* CoreDataMigrationVersion.swift in Sources */ = {isa = PBXBuildFile; fileRef = C28553DE21DCF5000004C7BA /* CoreDataMigrationVersion.swift */; };
3233
C28553E221DD14090004C7BA /* NSManagedObjectModel+Compatible.swift in Sources */ = {isa = PBXBuildFile; fileRef = C28553E121DD14090004C7BA /* NSManagedObjectModel+Compatible.swift */; };
3334
C28553E421DD1D7B0004C7BA /* NSManagedObjectModel+Resource.swift in Sources */ = {isa = PBXBuildFile; fileRef = C28553E321DD1D7B0004C7BA /* NSManagedObjectModel+Resource.swift */; };
@@ -50,9 +51,10 @@
5051
/* End PBXContainerItemProxy section */
5152

5253
/* Begin PBXFileReference section */
53-
3D70ED0621ED328D001C2516 /* Migration2to3ModelMapping.xcmappingmodel */ = {isa = PBXFileReference; lastKnownFileType = wrapper.xcmappingmodel; path = Migration2to3ModelMapping.xcmappingmodel; sourceTree = "<group>"; };
5454
3D8E52F421E0EF2800FE1D35 /* FileManager+Helper.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "FileManager+Helper.swift"; sourceTree = "<group>"; };
5555
3D8E52F621E0F98500FE1D35 /* NSManagedObjectContext+Helper.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "NSManagedObjectContext+Helper.swift"; sourceTree = "<group>"; };
56+
3DACAAD121EE9D5D00309A75 /* PostSectionWriterTableViewCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PostSectionWriterTableViewCell.swift; sourceTree = "<group>"; };
57+
3DD99ECF21F1118700CB4B6E /* Migration2to3ModelMapping.xcmappingmodel */ = {isa = PBXFileReference; lastKnownFileType = wrapper.xcmappingmodel; path = Migration2to3ModelMapping.xcmappingmodel; sourceTree = "<group>"; };
5658
3DDB26C821EBF87E00388AEE /* PostWriterViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PostWriterViewController.swift; sourceTree = "<group>"; };
5759
3DDB26CA21EC00FE00388AEE /* PostViewerViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PostViewerViewController.swift; sourceTree = "<group>"; };
5860
431DCEAB1F67EC7100CF6316 /* CoreDataMigration_Example 2.xcdatamodel */ = {isa = PBXFileReference; lastKnownFileType = wrapper.xcdatamodel; path = "CoreDataMigration_Example 2.xcdatamodel"; sourceTree = "<group>"; };
@@ -75,9 +77,9 @@
7577
43370DBB1F66F0C0006188EC /* CoreDataManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CoreDataManager.swift; sourceTree = "<group>"; };
7678
4345D4ED1F67E0FE00027D11 /* UIColor+Hex.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "UIColor+Hex.swift"; sourceTree = "<group>"; };
7779
4345D4EF1F67E10700027D11 /* UIColor+Random.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "UIColor+Random.swift"; sourceTree = "<group>"; };
78-
4345D4F41F67E1FC00027D11 /* CGFloat+Random.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "CGFloat+Random.swift"; sourceTree = "<group>"; };
7980
43AB8AE51F66E6A5003153B3 /* CoreDataMigration-Example.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = "CoreDataMigration-Example.app"; sourceTree = BUILT_PRODUCTS_DIR; };
8081
43AB8AFC1F66E6A5003153B3 /* CoreDataMigration-ExampleTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = "CoreDataMigration-ExampleTests.xctest"; sourceTree = BUILT_PRODUCTS_DIR; };
82+
C23BD45921F08A350039A36B /* PostSectionViewerTableViewCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PostSectionViewerTableViewCell.swift; sourceTree = "<group>"; };
8183
C28553DE21DCF5000004C7BA /* CoreDataMigrationVersion.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CoreDataMigrationVersion.swift; sourceTree = "<group>"; };
8284
C28553E121DD14090004C7BA /* NSManagedObjectModel+Compatible.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "NSManagedObjectModel+Compatible.swift"; sourceTree = "<group>"; };
8385
C28553E321DD1D7B0004C7BA /* NSManagedObjectModel+Resource.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "NSManagedObjectModel+Resource.swift"; sourceTree = "<group>"; };
@@ -121,6 +123,7 @@
121123
isa = PBXGroup;
122124
children = (
123125
3DDB26CA21EC00FE00388AEE /* PostViewerViewController.swift */,
126+
C23BD45921F08A350039A36B /* PostSectionViewerTableViewCell.swift */,
124127
);
125128
path = Viewer;
126129
sourceTree = "<group>";
@@ -129,6 +132,7 @@
129132
isa = PBXGroup;
130133
children = (
131134
3DDB26C821EBF87E00388AEE /* PostWriterViewController.swift */,
135+
3DACAAD121EE9D5D00309A75 /* PostSectionWriterTableViewCell.swift */,
132136
);
133137
path = Writer;
134138
sourceTree = "<group>";
@@ -148,7 +152,7 @@
148152
431DCEB31F67EE2600CF6316 /* Mappings */ = {
149153
isa = PBXGroup;
150154
children = (
151-
3D70ED0621ED328D001C2516 /* Migration2to3ModelMapping.xcmappingmodel */,
155+
3DD99ECF21F1118700CB4B6E /* Migration2to3ModelMapping.xcmappingmodel */,
152156
);
153157
path = Mappings;
154158
sourceTree = "<group>";
@@ -245,7 +249,6 @@
245249
4345D4EA1F67E0DD00027D11 /* Extensions */ = {
246250
isa = PBXGroup;
247251
children = (
248-
4345D4F31F67E1FC00027D11 /* CGFloat */,
249252
C28553E021DD13EF0004C7BA /* NSManagedObjectModel */,
250253
431DCEBC1F67F07100CF6316 /* NSPersistentStoreCoordinator */,
251254
4345D4EC1F67E0DD00027D11 /* UIColor */,
@@ -262,14 +265,6 @@
262265
path = UIColor;
263266
sourceTree = "<group>";
264267
};
265-
4345D4F31F67E1FC00027D11 /* CGFloat */ = {
266-
isa = PBXGroup;
267-
children = (
268-
4345D4F41F67E1FC00027D11 /* CGFloat+Random.swift */,
269-
);
270-
path = CGFloat;
271-
sourceTree = "<group>";
272-
};
273268
43AB8ADC1F66E6A5003153B3 = {
274269
isa = PBXGroup;
275270
children = (
@@ -491,14 +486,14 @@
491486
files = (
492487
43370DB01F66E7A6006188EC /* AppDelegate.swift in Sources */,
493488
431DCEC01F67F18100CF6316 /* CoreDataMigrator.swift in Sources */,
494-
3D70ED0721ED328D001C2516 /* Migration2to3ModelMapping.xcmappingmodel in Sources */,
495489
C28553E421DD1D7B0004C7BA /* NSManagedObjectModel+Resource.swift in Sources */,
496490
431DCECB1F67F93000CF6316 /* AppLoadingViewController.swift in Sources */,
497491
3DDB26CB21EC00FE00388AEE /* PostViewerViewController.swift in Sources */,
492+
3DD99ED021F1118700CB4B6E /* Migration2to3ModelMapping.xcmappingmodel in Sources */,
498493
C28553E221DD14090004C7BA /* NSManagedObjectModel+Compatible.swift in Sources */,
499-
4345D4F51F67E1FC00027D11 /* CGFloat+Random.swift in Sources */,
500494
C28553DF21DCF5000004C7BA /* CoreDataMigrationVersion.swift in Sources */,
501495
432EA5591F6C552800EFE008 /* NSPersistentStoreCoordinator+SQLite.swift in Sources */,
496+
C23BD45A21F08A350039A36B /* PostSectionViewerTableViewCell.swift in Sources */,
502497
4345D4F01F67E10700027D11 /* UIColor+Random.swift in Sources */,
503498
4345D4EE1F67E0FE00027D11 /* UIColor+Hex.swift in Sources */,
504499
431DCED21F6815A300CF6316 /* Post2ToPost3MigrationPolicy.swift in Sources */,
@@ -508,6 +503,7 @@
508503
431DCEBF1F67F18100CF6316 /* CoreDataMigrationStep.swift in Sources */,
509504
431DCEAE1F67EC9E00CF6316 /* CoreDataMigration_Example.xcdatamodeld in Sources */,
510505
3DDB26C921EBF87E00388AEE /* PostWriterViewController.swift in Sources */,
506+
3DACAAD221EE9D5D00309A75 /* PostSectionWriterTableViewCell.swift in Sources */,
511507
);
512508
runOnlyForDeploymentPostprocessing = 0;
513509
};
@@ -772,7 +768,7 @@
772768
431DCEAC1F67EC7100CF6316 /* CoreDataMigration_Example 3.xcdatamodel */,
773769
431DCEAD1F67EC7100CF6316 /* CoreDataMigration_Example.xcdatamodel */,
774770
);
775-
currentVersion = 431DCED71F68394200CF6316 /* CoreDataMigration_Example 4.xcdatamodel */;
771+
currentVersion = 431DCEAC1F67EC7100CF6316 /* CoreDataMigration_Example 3.xcdatamodel */;
776772
path = CoreDataMigration_Example.xcdatamodeld;
777773
sourceTree = "<group>";
778774
versionGroupType = wrapper.xcdatamodel;

CoreDataMigration-Example/CoreData/Migration/CoreDataMigrationVersion.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ enum CoreDataMigrationVersion: String, CaseIterable {
1313
case version1 = "CoreDataMigration_Example"
1414
case version2 = "CoreDataMigration_Example 2"
1515
case version3 = "CoreDataMigration_Example 3"
16-
case version4 = "CoreDataMigration_Example 4"
16+
// case version4 = "CoreDataMigration_Example 4"
1717

1818
// MARK: - Latest
1919

@@ -34,8 +34,8 @@ enum CoreDataMigrationVersion: String, CaseIterable {
3434
case .version2:
3535
return .version3
3636
case .version3:
37-
return .version4
38-
case .version4:
37+
// return .version4
38+
// case .version4:
3939
return nil
4040
}
4141
}

CoreDataMigration-Example/CoreData/Migration/Mappings/Migration2to3ModelMapping.xcmappingmodel/xcmapping.xml

Lines changed: 42 additions & 39 deletions
Large diffs are not rendered by default.

CoreDataMigration-Example/CoreData/Migration/Policies/Post2ToPost3MigrationPolicy.swift

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,27 +10,25 @@ import CoreData
1010

1111
final class Post2ToPost3MigrationPolicy: NSEntityMigrationPolicy {
1212

13-
override func createDestinationInstances(forSource sInstance: NSManagedObject, in mapping: NSEntityMapping, manager: NSMigrationManager) throws {
14-
try super.createDestinationInstances(forSource: sInstance, in: mapping, manager: manager)
13+
override func createDestinationInstances(forSource sourceInstance: NSManagedObject, in mapping: NSEntityMapping, manager: NSMigrationManager) throws {
14+
try super.createDestinationInstances(forSource: sourceInstance, in: mapping, manager: manager)
1515

16-
guard let destinationPost = manager.destinationInstances(forEntityMappingName: mapping.name, sourceInstances: [sInstance]).first else {
16+
guard let destinationPost = manager.destinationInstances(forEntityMappingName: mapping.name, sourceInstances: [sourceInstance]).first else {
1717
fatalError("was expected a post")
1818
}
1919

20-
let sourceBody = sInstance.value(forKey: "content") as? String
21-
let sourceTitle = sourceBody?.prefix(80)
22-
let sourceHexColor = sInstance.value(forKey: "hexColor")
20+
let sourceBody = sourceInstance.value(forKey: "content") as? String
21+
let sourceTitle = sourceBody?.prefix(4).appending("...")
2322

24-
let titleContent = NSEntityDescription.insertNewObject(forEntityName: "Content", into: destinationPost.managedObjectContext!)
25-
titleContent.setValue(sourceTitle, forKey: "content")
26-
titleContent.setValue(sourceHexColor, forKey: "hexColor")
27-
titleContent.setValue(destinationPost, forKey: "post")
28-
destinationPost.setValue(titleContent, forKey: "title")
23+
let section = NSEntityDescription.insertNewObject(forEntityName: "Section", into: destinationPost.managedObjectContext!)
24+
section.setValue(sourceTitle, forKey: "title")
25+
section.setValue(sourceBody, forKey: "body")
26+
section.setValue(destinationPost, forKey: "post")
27+
section.setValue(0, forKey: "index")
2928

30-
let bodyContent = NSEntityDescription.insertNewObject(forEntityName: "Content", into: destinationPost.managedObjectContext!)
31-
bodyContent.setValue(sourceBody, forKey: "content")
32-
bodyContent.setValue(sourceHexColor, forKey: "hexColor")
33-
bodyContent.setValue(destinationPost, forKey: "post")
34-
destinationPost.setValue(bodyContent, forKey: "body")
29+
var sections = Set<NSManagedObject>()
30+
sections.insert(section)
31+
32+
destinationPost.setValue(sections, forKey: "sections")
3533
}
3634
}

CoreDataMigration-Example/CoreData/Model/CoreDataMigration_Example.xcdatamodeld/.xccurrentversion

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33
<plist version="1.0">
44
<dict>
55
<key>_XCCurrentVersionName</key>
6-
<string>CoreDataMigration_Example 4.xcdatamodel</string>
6+
<string>CoreDataMigration_Example 3.xcdatamodel</string>
77
</dict>
88
</plist>
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
22
<model type="com.apple.IDECoreDataModeler.DataModel" documentVersion="1.0" lastSavedToolsVersion="14460.32" systemVersion="18C54" minimumToolsVersion="Automatic" sourceLanguage="Swift" userDefinedModelVersionIdentifier="">
3-
<entity name="Content" representedClassName="Content" syncable="YES" codeGenerationType="class">
4-
<attribute name="content" optional="YES" attributeType="String" syncable="YES"/>
5-
<attribute name="hexColor" optional="YES" attributeType="String" syncable="YES"/>
6-
<relationship name="post" optional="YES" maxCount="1" deletionRule="Nullify" destinationEntity="Post" syncable="YES"/>
7-
</entity>
83
<entity name="Post" representedClassName="Post" syncable="YES" codeGenerationType="class">
94
<attribute name="date" optional="YES" attributeType="Date" usesScalarValueType="NO" syncable="YES"/>
5+
<attribute name="hexColor" optional="YES" attributeType="String" syncable="YES"/>
106
<attribute name="postID" optional="YES" attributeType="String" syncable="YES"/>
11-
<relationship name="body" maxCount="1" deletionRule="Nullify" destinationEntity="Content" syncable="YES"/>
12-
<relationship name="title" optional="YES" maxCount="1" deletionRule="Nullify" destinationEntity="Content" syncable="YES"/>
7+
<relationship name="sections" toMany="YES" deletionRule="Cascade" destinationEntity="Section" inverseName="post" inverseEntity="Section" syncable="YES"/>
8+
</entity>
9+
<entity name="Section" representedClassName="Section" syncable="YES" codeGenerationType="class">
10+
<attribute name="body" optional="YES" attributeType="String" syncable="YES"/>
11+
<attribute name="index" optional="YES" attributeType="Integer 16" defaultValueString="0" usesScalarValueType="YES" syncable="YES"/>
12+
<attribute name="title" optional="YES" attributeType="String" syncable="YES"/>
13+
<relationship name="post" optional="YES" maxCount="1" deletionRule="Nullify" destinationEntity="Post" inverseName="sections" inverseEntity="Post" syncable="YES"/>
1314
</entity>
1415
<elements>
15-
<element name="Content" positionX="-63" positionY="9" width="128" height="90"/>
16+
<element name="Section" positionX="-63" positionY="9" width="128" height="105"/>
1617
<element name="Post" positionX="-63" positionY="-18" width="128" height="105"/>
1718
</elements>
1819
</model>

CoreDataMigration-Example/CoreData/Model/CoreDataMigration_Example.xcdatamodeld/CoreDataMigration_Example 4.xcdatamodel/contents

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,19 @@
22
<model type="com.apple.IDECoreDataModeler.DataModel" documentVersion="1.0" lastSavedToolsVersion="14460.32" systemVersion="18C54" minimumToolsVersion="Automatic" sourceLanguage="Swift" userDefinedModelVersionIdentifier="">
33
<entity name="Content" representedClassName="Content" syncable="YES" codeGenerationType="class">
44
<attribute name="content" optional="YES" attributeType="String" syncable="YES"/>
5-
<attribute name="hexColor" optional="YES" attributeType="String" syncable="YES"/>
6-
<relationship name="post" optional="YES" maxCount="1" deletionRule="Nullify" destinationEntity="Post" syncable="YES"/>
5+
<attribute name="index" optional="YES" attributeType="Integer 16" defaultValueString="0" usesScalarValueType="YES" syncable="YES"/>
6+
<attribute name="title" optional="YES" attributeType="String" syncable="YES"/>
7+
<relationship name="post" optional="YES" maxCount="1" deletionRule="Nullify" destinationEntity="Post" inverseName="contents" inverseEntity="Post" syncable="YES"/>
78
</entity>
89
<entity name="Post" representedClassName="Post" syncable="YES" codeGenerationType="class">
910
<attribute name="date" optional="YES" attributeType="Date" usesScalarValueType="NO" syncable="YES"/>
11+
<attribute name="hexColor" optional="YES" attributeType="String" syncable="YES"/>
1012
<attribute name="postID" optional="YES" attributeType="String" syncable="YES"/>
1113
<attribute name="softDeleted" attributeType="Boolean" defaultValueString="NO" usesScalarValueType="YES" syncable="YES"/>
12-
<relationship name="body" maxCount="1" deletionRule="Nullify" destinationEntity="Content" syncable="YES"/>
13-
<relationship name="title" optional="YES" maxCount="1" deletionRule="Nullify" destinationEntity="Content" syncable="YES"/>
14+
<relationship name="contents" maxCount="1" deletionRule="Nullify" destinationEntity="Content" inverseName="post" inverseEntity="Content" syncable="YES"/>
1415
</entity>
1516
<elements>
16-
<element name="Content" positionX="-63" positionY="9" width="128" height="90"/>
17+
<element name="Content" positionX="-63" positionY="9" width="128" height="105"/>
1718
<element name="Post" positionX="-63" positionY="-18" width="128" height="120"/>
1819
</elements>
1920
</model>

CoreDataMigration-Example/Extensions/CGFloat/CGFloat+Random.swift

Lines changed: 0 additions & 19 deletions
This file was deleted.

CoreDataMigration-Example/Extensions/UIColor/UIColor+Hex.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ extension UIColor {
3737
let red = CGFloat((hexNumber & 0xff0000) >> 16) / 255
3838
let green = CGFloat((hexNumber & 0x00ff00) >> 8) / 255
3939
let blue = CGFloat(hexNumber & 0x0000ff) / 255
40-
let alpha = CGFloat(0.2)
40+
let alpha = CGFloat(1)
4141

4242
return UIColor(red: red, green: green, blue: blue, alpha: alpha)
4343
}

CoreDataMigration-Example/Extensions/UIColor/UIColor+Random.swift

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,26 @@ extension UIColor {
1212

1313
// MARK: - Random
1414

15-
static var random: UIColor {
16-
return UIColor(red: .random(), green: .random(), blue: .random(), alpha: 1.0)
15+
static var randomPastelColor: UIColor {
16+
let mixColor = UIColor.white
17+
18+
let randomColorGenerator = { ()-> CGFloat in
19+
CGFloat(arc4random() % 256) / 256
20+
}
21+
22+
var red: CGFloat = randomColorGenerator()
23+
var green: CGFloat = randomColorGenerator()
24+
var blue: CGFloat = randomColorGenerator()
25+
26+
var mixRed: CGFloat = 0
27+
var mixGreen: CGFloat = 0
28+
var mixBlue: CGFloat = 0
29+
mixColor.getRed(&mixRed, green: &mixGreen, blue: &mixBlue, alpha: nil)
30+
31+
red = (red + mixRed) / 2
32+
green = (green + mixGreen) / 2
33+
blue = (blue + mixBlue) / 2
34+
35+
return UIColor(red: red, green: green, blue: blue, alpha: 1)
1736
}
1837
}

0 commit comments

Comments
 (0)