Skip to content

Commit 2c17859

Browse files
committed
Non failable initializer
1 parent 1fdc6f7 commit 2c17859

File tree

3 files changed

+26
-8
lines changed

3 files changed

+26
-8
lines changed

Example/Example.xcodeproj/project.pbxproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
E22195AC1B7C6EDA00F58D3F /* CopyFiles */ = {
1717
isa = PBXCopyFilesBuildPhase;
1818
buildActionMask = 12;
19-
dstPath = .;
19+
dstPath = Test.xcodeproj;
2020
dstSubfolderSpec = 16;
2121
files = (
2222
E262D4431B7E20DB0057CCEE /* project.pbxproj in CopyFiles */,

Example/Example/main.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@
88

99
import Foundation
1010

11-
// The pbxproj file to load, test this with your own project!
12-
let projectFile = "project.pbxproj"
11+
// The xcodeproj file to load, test this with your own project!
12+
let xcodeproj = "Test.xcodeproj"
1313

1414

1515
// Parse JSON to XCProjectFile object
16-
let proj = try! XCProjectFile(filename: projectFile)!
16+
let proj = try! XCProjectFile(xcodeprojPath: xcodeproj)
1717

1818
// Print paths for all files in Resources build phases
1919
for target in proj.project.targets {

Xcode/Xcode/XCProjectFile.swift

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,26 +40,44 @@ public class AllObjects {
4040
}
4141
}
4242

43+
enum ProjectFileError : ErrorType, CustomStringConvertible {
44+
case InvalidData
45+
case MissingPbxproj
46+
47+
var description: String {
48+
switch self {
49+
case .InvalidData:
50+
return "Data in .pbxproj file not in expected format"
51+
case .MissingPbxproj:
52+
return "project.pbxproj file missing"
53+
}
54+
}
55+
}
56+
4357
public class XCProjectFile {
4458
public let project: PBXProject
4559
let dict: JsonObject
4660
let openStepFormat: Bool
4761
let allObjects = AllObjects()
4862

49-
public convenience init?(filename: String) throws {
63+
public convenience init(xcodeprojPath: String) throws {
5064

51-
guard let data = NSData(contentsOfFile: filename) else { return nil }
65+
guard let data = NSData(contentsOfFile: xcodeprojPath + "/project.pbxproj") else {
66+
throw ProjectFileError.MissingPbxproj
67+
}
5268

5369
try self.init(propertyListData: data)
5470
}
5571

56-
public convenience init?(propertyListData data: NSData) throws {
72+
public convenience init(propertyListData data: NSData) throws {
5773

5874
let options = NSPropertyListReadOptions.Immutable
5975
var format: NSPropertyListFormat = NSPropertyListFormat.BinaryFormat_v1_0
6076
let obj = try NSPropertyListSerialization.propertyListWithData(data, options: options, format: &format)
6177

62-
guard let dict = obj as? JsonObject else { return nil }
78+
guard let dict = obj as? JsonObject else {
79+
throw ProjectFileError.InvalidData
80+
}
6381

6482
self.init(dict: dict, openStepFormat: format == NSPropertyListFormat.OpenStepFormat)
6583
}

0 commit comments

Comments
 (0)