Apple’s WWDC (Worldwide Developer Conference) is under way, where the company has put a strong emphasis on improvements to the Swift language and Swift-related frameworks, which now includes SwiftData for full object-relational mapping.
The new features are in beta and will not be supported until the arrival of iOS, iPadOS and tvOS 17, or macOS 14, or watchOS 10.
Swift is acquiring a range of interdependent new features, which make extensive use of macros. Macros are notorious in C and C++ for adding complexity to those languages; but are also a powerful feature. Swift macros are of two kinds: freestanding macros which begin with the traditional # symbol, and attached macros which begin with @.
Holly Borla, Apple engineering manager for the Swift language, described Swift macros as “an annotation that uses the structure of your code to generate new code that’s built with your project.” The documentation for macros insist that syntax checking and the fact that the compiler will catch errors when expanding macros make it “easier to reason about code that uses macros, and they make it easier to identify issues like using a macro incorrectly or a macro implementation that has a bug.”
SwiftData is a data modelling and management framework built on Apple’s Core Data, but using Swift’s macro system. “With SwiftData, you just annotate your class with the @Model macro,” said Josh Shaffer, Apple senior director for Swift frameworks, at WWDC, “automatically enabling persistence, iCloud synchronization, undo and redo, and more.” Additional attributes control aspects such as unique values.
The documentation describes the framework in detail. “Attach the Model macro to any model class to make it persistable,” it states, adding that “as your app’s model layer evolves, SwiftData performs automatic migrations of the underlying model data so it remains in a consistent state.”
Object-relational mapping simplifies the initial work of creating a database application, but can complicate matters when the database schema is advanced. Relations between tables can be fiddly, with the SwiftData approach documented here.
Another key new feature is Swift-C++ interoperability. Borla said at WWDC that “Swift-C++ interoperability allows you to use both languages in the same project without an intermediate bridging layer. You can share your classes, functions, and even template specializations like vector across both languages by setting a compiler flag.”
There is no doubting the company’s commitment to Swift which is presented to developers as the best way to write apps for Apple’s various hardware platforms. Developer views on the language vary, as this recent thread on Hacker News demonstrates.
“As someone who worked for years in Objective C and years in Swift, I honestly never felt that Swift significantly improved much except allocation rituals and properties … frankly, apps run over 10 times faster in obj C,” said one developer. Another found that “Swift is a true breath of fresh air. It checks all the boxes for me, very concise and elegant to write, yet very strictly statically typed … runs reasonably fast, on par with Go or Java.”