Cannot read .swift from SwiftDocs
Open
Nobody has claimed this yet.
question
- Dominant language
- Swift
- Stars
- 2.4k
- Forks
- 237
- PR merge metrics
- No merged PRs in 30d
Description
I am trying to extract document JSON structure from a swift file using SwiftDocs class. I have imported Sourcekitten framework and below is the code snippet of the same.
import Cocoa
import SourceKittenFramework
class ViewController: NSViewController {
var sourcekitten: SwiftDocs?
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
sourcekitten = SwiftDocs(file: File(path: "/Users/.../sample.swift")!, arguments: [])!
print(sourcekitten!.docsDictionary) // Should print document json structure
}
}
Expecting it to print a JSON string similar to below one
{
"key.substructure" : [
{
"key.kind" : "source.lang.swift.decl.struct",
"key.offset" : 0,
"key.nameoffset" : 7,
"key.namelength" : 1,
"key.bodyoffset" : 10,
"key.bodylength" : 13,
"key.length" : 24,
"key.substructure" : [
{
"key.kind" : "source.lang.swift.decl.function.method.instance",
"key.offset" : 11,
"key.nameoffset" : 16,
"key.namelength" : 3,
"key.bodyoffset" : 21,
"key.bodylength" : 0,
"key.length" : 11,
"key.substructure" : [
],
"key.name" : "b()"
}
],
"key.name" : "A"
}
],
"key.offset" : 0,
"key.diagnostic_stage" : "source.diagnostic.stage.swift.parse",
"key.length" : 24
}
Instead, I am getting cannot read file error.
When I did a 'po' this is what I got
Could not read contents of `/Users/.../SwiftyP/SwiftyP/SwiftyP/sample.swift`
Fatal error: Unexpectedly found nil while unwrapping an Optional value
2017-12-28 15:27:27.960442-0600 SwiftyP[70384:15566179] Fatal error: Unexpectedly found nil while unwrapping an Optional value
(lldb) po File(path: "/Users/.../SwiftyP/SwiftyP/SwiftyP/sample.swift")!
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the ViewController example and the sample.swift path shown in the report, then inspect the File(path:) result before SwiftDocs is initialized. Confirm that the file can be read and that docsDictionary produces the expected document structure without force-unwrapping nil.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- swift
- Domain
- devtools
- Issue type
- Bug
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 35/100