Foundation
provides you with a lot of the bread and butter needed for your daily iOS development, ranging from structures like Data
all the way to complete APIs like URLSession
. But as it turns out, we only use a fraction of what Foundation
offers.
There are a bunch of Foundation types that are so situational that people doubt they even exist in the first place! In fact, they are so rarely mentioned as solutions to daily problems that developers may end up coding things that already exist in these SDKs. But they do exist, and although some of them are really old, most of these types are still very useful. Let’s take a look at some of them!
NSScanner
can progressively extract numbers and strings from a base string, similarly to how scanf
works in C:
The scanner API has many variations, like scanString
, scanDouble
, and scanHexInt
, and can be configured to scan at specific locations in a string or to take case sensitivity into consideration. This is a more direct and performant solution for searching occurrences of a string when you’re looking for specific patterns such as something that resembles a number instead of a concrete match.
You might notice that this API requires pointers, which is an unfortunate side effect of this being an old Obj-C API, but you can always abstract these types under extensions to make them look better for you and your colleagues.
There are many problems in programming that require you to keep track of the quantity of a certain element, like the classic anagram interview problem:
This is easy to solve with dictionaries with Int
values, but we don’t need to because that’s exactly what NSCountedSet
does:
#xcode #mobile #swift #ios #programming