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.
Find the Code Causing Long SwiftUI Updates with Instruments Instruments comes with a SwiftUI instrument to find performance problems in your SwiftUI apps. This article shows how to use Instruments to find the code that causes long SwiftUI view updates. If you have never used Instruments, read the following article to learn how to profile your app with the SwiftUI instrument: Find the SwiftUI Views that Update the Most Using Instruments Finding Long Updates When you finish profiling your app,...
When you profile your app with the Allocations instrument, you may want to find the largest memory allocations your app makes. Take the following steps to find the largest memory allocations: Press Cmd-3 to open the allocations list. Click the Size column heading to sort the allocations by size. Choose All Heap Allocations from the Allocation Type menu in the bottom bar to hide virtual memory allocations. Your code doesn’t directly make virtual memory allocations. The Allocation Type menu is...
A common problem people run into when they start profiling their apps with Instruments is finding the code that is causing problems. Many instruments initially show general statistics instead of statistics about the code you wrote. For example the Allocations instrument initially shows the number of memory allocations and amount of allocated memory for hundreds of memory categories. If your app allocates a lot of memory, you want to find the code that allocates high amounts of memory. How do...