-
-
Notifications
You must be signed in to change notification settings - Fork 320
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improved README examples #212
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -155,24 +155,66 @@ try project.write(path: "MyApp.xcodeproj") | |
#### Adding `Home` group inside `Sources` group | ||
|
||
```swift | ||
guard var sourcesGroup = project.pbxproj.objects.groups.first(where: {$0.value.name == "Sources"})?.value else { return } | ||
let homeGroup = PBXGroup(reference: "xxx", children: [], sourceTree: .group, path: "Home") | ||
guard var sourcesGroup = project.pbxproj.objects.groups.first(where: {$0.value.name == "Sources" || $0.value.path == "Sources"})?.value else { return } | ||
let homeGroup = PBXGroup(children: [], sourceTree: .group, path: "Home") | ||
let groupRef = pbxproj.objects.generateReference(homeGroup, "Home") | ||
sourcesGroup.children.append(homeGroup, reference: groupRef) | ||
project.pbxproj.objects.addObject(groupRef) | ||
``` | ||
|
||
<details> | ||
<summary>Versions <2.0</summary> | ||
|
||
```swift | ||
guard var sourcesGroup = project.pbxproj.objects.groups.first(where: {$0.value.name == "Sources" || $0.value.path == "Sources"})?.value else { return } | ||
let homeGroup = PBXGroup(reference: project.pbxproj.generateUUID(for: PBXGroup.self), children: [], sourceTree: .group, path: "Home") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've spent some time searching for a way to generate references until found this method. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah we introduced it in the version 2.0 but we forgot to update the readme. The function is
The second argument must be a string that uniquely identifies the object. In this case we can just use "home". There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, looks like I was using 1.7.0 version There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unfortunately it means that readme examples are completely out of date. I would suggest to keep section with examples for 1.x version and with 2.x There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thank you very much @ilyapuchka |
||
sourcesGroup.children.append(homeGroup.reference) | ||
project.pbxproj.objects.addObject(homeGroup) | ||
``` | ||
|
||
</details> | ||
|
||
#### Add `HomeViewController.swift` file inside `HomeGroup` | ||
|
||
```swift | ||
let homeGroup = PBXGroup(reference: "xxx", children: [], sourceTree: .group, path: "Home") | ||
let homeViewController = PBXFileReference(reference: "xxx", sourceTree: .group, path: "HomeViewController.swift") | ||
let homeViewController = PBXFileReference(sourceTree: .group, name: "HomeViewController.swift", path: "HomeViewController.swift") | ||
let fileRef = pbxproj.objects.generateReference(homeViewController, "HomeViewController.swift") | ||
homeGroup.children.append(fileRef) | ||
project.pbxproj.objects.addObject(homeViewController, reference: fileRef) | ||
``` | ||
|
||
<details> | ||
<summary>Versions <2.0</summary> | ||
|
||
```swift | ||
let homeViewController = PBXFileReference(reference: project.pbxproj.generateUUID(for: PBXFileReference.self), sourceTree: .group, name: "HomeViewController.swift", path: "HomeViewController.swift") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same comment regarding the |
||
homeGroup.children.append(homeViewController.reference) | ||
project.pbxproj.objects.addObject(homeViewController) | ||
``` | ||
|
||
</details> | ||
|
||
#### Add `HomeViewController.swift` file to `MyApp` target | ||
|
||
```swift | ||
let homeViewController = PBXFileReference(reference: "xxx", sourceTree: .group, path: "HomeViewController.swift") | ||
guard let sourcesBuildPhase = project.pbxproj | ||
.objects.nativeTargets | ||
.values | ||
.first(where: {$0.name == "MyApp"}) | ||
.flatMap({ target -> PBXSourcesBuildPhase? in | ||
return project.pbxproj.objects.sourcesBuildPhases.first(where: { target.buildPhases.contains($0.key) })?.value | ||
}) else { return } | ||
// PBXBuildFile is a proxy model that allows specifying some build attributes to the files | ||
let buildFile = PBXBuildFile(fileRef: fileRef) | ||
let buildFileRef = project.pbxproj.objects.generateReference(buildFile, "HomeViewController.swift") | ||
project.pbxproj.objects.addObject(buildFile, reference: buildFileRef) | ||
sourcesBuildPhase.files.append(buildFileRef) | ||
``` | ||
|
||
<details> | ||
<summary>Versions <2.0</summary> | ||
|
||
```swift | ||
guard let sourcesBuildPhase = project.pbxproj | ||
.objects.nativeTargets | ||
.values | ||
|
@@ -181,11 +223,13 @@ guard let sourcesBuildPhase = project.pbxproj | |
return project.pbxproj.objects.sourcesBuildPhases.values.first(where: { target.buildPhases.contains($0.reference) }) | ||
}) else { return } | ||
// PBXBuildFile is a proxy model that allows specifying some build attributes to the files | ||
let buildFile = PBXBuildFile(reference: "yyy", fileRef: homeViewController.reference) | ||
let buildFile = PBXBuildFile(reference: project.pbxproj.generateUUID(for: PBXBuildFile.self), fileRef: homeViewController.reference) | ||
project.pbxproj.objects.addObject(buildFile) | ||
sourcesBuildPhase.files.append(buildFile.reference) | ||
``` | ||
|
||
</details> | ||
|
||
## Documentation 📄 | ||
You can check out the documentation on the following [link](https://xcodeswift.github.io/xcproj/index.html). The documentation is automatically generated in every release by using [Jazzy](https://github.com/realm/jazzy) from [Realm](https://realm.io). | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In Xcode 9 groups are created with paths by default, unless they are added via "New Group without Folder". So
name
property is not stored in project xml and then is not decoded.But before Xcode 9 when creating folder with File-New-Group menu (instead of dragging folder into the project) only
name
property will be set andpath
will be nil.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the clarification. Makes sense to me! 👍