Working with Lists in Multiplatform SwiftUI AppsOne of SwiftUI's best features is you can use it to make apps that run on both iOS and Mac. Almost every SwiftUI article you find online is about iOS development, but most of the material also applies to Mac as well. Lists are one area of SwiftUI where there are large differences between iOS and Mac. If you read an article about lists and try to use the code in a Mac app, you'll run into problems. This article provides guidance on writing list code that works in both iOS and Mac apps. Differences in List BehaviorThe main reason you can't use the same list code on iOS and Mac is because the way people interact with lists is different in iOS and Mac. iOS apps have an Edit button that people use to delete and move items. When someone wants to delete a list item, they tap the Edit button. Each item has a Delete button next to it. Tapping the button deletes the item. Because each list item has a Delete button, you don't have to keep track of the selected items. Mac apps don't have an Edit button for deleting and moving list items. In a Mac app, people delete list items by selecting them and either pressing the Delete key or clicking a Delete button. People move items by selecting them and dragging them to the desired destination. When developing a Mac app that uses lists, you must keep track of the selected item. Tip: Create Separate List Views for iOS and Mac App TargetsBecause list behavior is so different between iOS and Mac apps, you should create separate SwiftUI list views for the iOS and Mac versions of your app. If you try to support iOS and Mac in the same view, your code is going to be littered with You can share a list view if the list only displays data. If your app doesn't allow people to delete and move items, it doesn't require an Edit button on iOS. In that case you can share the view. Deleting List ItemsiOSDeleting a list item on iOS requires the following steps:
You can't apply the Call the function to delete the item in
The function to delete the item takes an
MacDeleting a list item on Mac requires the following steps:
The usual way to keep track of the selected list item is to add a
Call the function to delete the item in
Removing an item from an array in a Mac app is trickier. Call the array's
Moving List ItemsiOSMoving list items on iOS requires the following steps:
You can't apply the Call the
The
MacMoving list items on Mac requires the following steps:
You have to do a little more work on Mac with the
The
|
Subscribe and get exclusive articles on Swift development, a free guide on moving from tutorials to making your first app, notices of sales on books, and anything I decide to add in the future.
If you find your app becomes unresponsive at times, it can be frustrating to find the cause. Instruments includes a Hangs instrument that reports hangs to help you find and fix hangs. This article shows you how to use the Hangs and Time Profiler instruments to find hangs and find the code causing the hangs. Profiling Your App In Xcode press Cmd-I to build and profile your app with Instruments. When Instruments launches, it will ask you to choose a template for profiling. Select the Time...
Saving App Data in Property List Files Your app needs to save data. There's too much data to use User Defaults. Using SwiftData or Core Data would be overkill. One solution is to save the data in property list files. This article shows you how to save your app's data in property list files. Use the PropertyListEncoder Class if you can If the data you want to save conforms to the Codable protocol, use the PropertyListEncoder class to save the data. Encoding data with PropertyListEncoder...
Getting Started with Document-based SwiftUI Apps Document-based apps let people create documents they can share with others. Examples of document-based apps are text editors, spreadsheets and video editors. Learn the basics of making document-based SwiftUI apps in this article. Creating a Project in Xcode The iOS, Mac, and Multiplatform project categories have a Document App project template. Select that template and click the Next button to create a document app. Multiplatform document apps...