26

I have created an iOS mobile application. Is there a way to generate a UML class diagram for the Swift programming language?

I have tried Omni Graffle, but it keeps saying that the project doesn't contain Objective-C interfaces. And Omni Graffle only does this for Objective-C.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Abdul Sacranie
  • 291
  • 1
  • 3
  • 7

3 Answers3

29

I remember looking at this question a while ago and was disappointed that there wasn't any tool like this so I created one myself with the help of a colleague. It's free, it's open source, it's looking for contributors...

https://github.com/yoshimkd/swift-auto-diagram

And here's a tutorial on how to use it (don't worry it's very simple): https://martinmitrevski.com/2016/10/12/swift-class-diagrams-and-more/

Happy diagraming :)

Jovan Jovanovski
  • 603
  • 8
  • 16
  • 1
    Thank you @jovan it works with swift 5 as well. ( I was fooled by 2016 blogpost and assumed it wont work, due to swift nature) – palaniraja Jun 27 '19 at 16:26
  • @jovan I tested this out and it opens on Safari by default. But I can only see the green navigational arrows on the bottom left, and other controls on botton right. I don't see any class diagrams. Is there a way to resolve this? I opened the file in Chrome but I get the same result. – timman Apr 10 '20 at 20:17
  • 1
    @timman We're aware that there are many cases where the diagram may not be rendered properly for various reasons. The project is very rarely maintained in recent times, so the best advice I could think of is that you could try diagraming smaller portions of your project in order to avoid parts of the code that may cause the rendering to fail. – Jovan Jovanovski Apr 11 '20 at 21:18
15

The answer unfortunately is: you can't do it. At least not automatically. Swift as compiler itself has enough flaws left. Leave alone some tool will be able to swallow any Swift code. Your only alternative is the good old manual way.

I know that Enterprise Architect has a possibility to define a language syntax based on BNF. But Swift is (again unfortunately) not context free. (In fact it's so context sensitive that it often swallows its own rear.) So that won't work either.

P.S.: Now that Swift is open source someone might take the opportunity to tap the compiler's output for the class interfaces. I guess that should not be too difficult, but it's a lot of work still.

qwerty_so
  • 35,448
  • 8
  • 62
  • 86
  • 1
    Why complain that Swift is not context free? "[There are hardly any real-world programming languages that are context-free in any meaning of the word](https://stackoverflow.com/a/17686190/187663)". – Cfr Feb 28 '19 at 11:34
  • 1
    @Cfr Yes, of course many PLs use context "somewhere" but most just do it only in rarely used edges. You can implement a compiler by just following an EBNF syntax. Not so Swift. Even for the simplest constructs you need tons of context information. Just look at a simple assignment where Swift tries to guess the type of the RHS - and fails for even simple cases. It's just that people got used to that nightmare and avoid such constructs intuitively. Believe me, I know a lot of PLs and for me Swift is definitely on the "to be avoided" side. – qwerty_so Feb 28 '19 at 11:51
7

Inspired by swift-auto-diagram and similar open-source tools I started to work on such utility with the goal to write it in Swift (to make contribution easier by Swift developers) and to integrate it in Xcode and the Swift ecosystem.

Xcode extension: https://github.com/MarcoEidinger/SwiftPlantUML-Xcode-Extension

You are able to generate a class diagram from selected lines of code or from a whole file displayed in Xcode. The class diagram will then be opened in your browser. There you can modify the diagram with PlantUML notation

To generate a class diagram from multiple source files you can use the underlying CLI tool and Swift Package: https://github.com/MarcoEidinger/SwiftPlantUML

There is a size limitation to the visible diagram content (but you can delete content in the browser with PlantUML notation)

Marco Eidinger
  • 151
  • 2
  • 4