SECRET OF CSS

Exploring Apple’s DeveloperToolsSupport Framework | by Marco Eidinger | Aug, 2022


A look at the lesser-known framework

0*TdETJ368u98qPomn
Photo by Mia Baker on Unsplash

DeveloperToolsSupport is a little framework introduced in iOS 14+ / macOS 11+. It contains only one protocol and one structure. It enables developers to add custom SwiftUI views and view modifiers to the Xcode library.

Open the Xcode library with keyboard shortcut shift-command-L (⇧⌘L)

0*U3CnDPz 7 khXo86

Important: The views and modifier tabs in the Xcode library are only visible if the Canvas is opened (⌥⌘
)

To add items to the library, create a structure that conforms to the LibraryContentProvider protocol and encapsulate any items you want to add as LibraryItem instances. Implement the views computed property to add library items containing views. Implement the modifiers(base:) method to add items containing view modifiers. Xcode harvests items from all of the library content providers in your project as you work, and makes them available to you in its library.

Unfortunately, Xcode will not generate a default implementation when using the SwiftUI View file template. Instead you can use the menu option in Xcode to create a default implementation.

0*HZ24E19vpaa20 qQ
Example for a view and its LibraryContentProvider implementation.

You can provide additional characteristics, like an alternative, through the LibraryItem initializer.

Note: You don’t have to write an additional import statement for this framework. DeveloperToolsSupport is imported under the hood when you write the import SwiftUI statement.

Opening the Xcode library (⇧⌘L) will list this custom view.

0*0wiBvVjKsDWOWlDg

I was pretty excited when I heard first about this feature. Quickly I realized that there are significant caveats:

  • Xcode will not use your markdown documentation for your SwiftUI view as the description of the library item 😭 there is also no other way to add a description.
  • You cannot add an image or thumbnail to the library item.
  • Limited formatting options for the code snippet in your library item. This is only relevant if your init function takes multiple parameters and their types require deeply nested initialization. I recommend you specify everything inline.

I still believe it makes sense to create library content providers for your reusable SwiftUI views because I hope that Apple and the Xcode team will overcome these shortcomings in the long run.

In my next blog post, I’ll show you how to add a custom file template to Xcode, which allows you to create a new SwiftUI file with a view, preview, and library content provider implementation in one shot.



News Credit

%d bloggers like this: