Using dropLast and removeLast in Swift
Arrays are used all the time when building apps, which means having easy ways to manipulate them is huge. Two super useful Array functions that you get out-of-the-box with Swift are dropLast
and removeLast
.
DropLast
var allNumbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
let allbutLast = allNumbers.dropLast()
Let’s say you have an array of the numbers 1-10. If you call dropLast()
on that array it will return a new array that is the same as the original except without the last item. It’s also a really safe function to call because it isn’t mutating, so you won’t ever be changing the original array.
RemoveLast
var allNumbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
let last = allNumbers.removeLast()
Unlike dropLast
, removeLast
is a mutating function. When you call removeLast
it will remove the last item from the array, and rather than returning back a new array it returns the item that it removed. So in the example above when removeLast()
is called on allNumbers
the number 10 is returned, and allNumbers
is updated to only contain the numbers 1-9. This can be really useful if you’re doing things like reordering arrays.
Check out some other recent tutorials
Animations in your UI are a must these days, but everyone is doing the same old fade or move animations. If you really want to take your animations to the next level you need to incorporate some rotation.
Views in SwiftUI can be added to and removed from other views easily by checking a state property. When views are added or removed using just a normal bool check they are automatically given a fade in/out transition by default. That’s great for a lot of cases, but what if you want to do something different, like show a message coming in from the top?
Showing an alert is a great way to notify a user of important information related to your app such as a payment going through or an error loading something from the server. In SwiftUI Apple has added a modifier that makes it super easy to show an alert based off your view’s state.
Lists are great for displaying vertically scrolling collections of data. But what if you want something that scrolls horizontally? There isn’t a fully native SwiftUI version of UICollectionView yet, so for the time being you have to get a little creative using a combination of ScrollView and HStack.
Today we’re going to look at how to create a quick and easy animation for a button that scales and fades out when you tap on it.
Learn how to use protocols and extensions to make dequeueing cells super easy and optional free.
A best practice in SwiftUI architecture is to break views down into smaller components that you combine together to build more complex views. Learn how to do just that by building custom List rows in this tutorial!
It took me longer than I’d care to admit to figure out how to set the background color for a full screen view in SwiftUI. Read on to avoid my mistakes and learn how to quickly and easily set the background color for your views!
Being able to enter text into an app is essential. In this short tutorial you’ll learn how to create a TextField
, bind it to a value, and update its style so you can give your users a way to enter text in your apps.