How does String substring work in Swift

All of the following examples use Swift 4 Strings got a pretty big overhaul in Swift 4. When you get some substring from a String now, you get a Substring type back rather than a String. Why is this? Strings are value types in Swift. That means if you use one String to make a new one, then … Read more

How do you create a Swift Date object?

Swift has its own Date type. No need to use NSDate. Creating a Date and Time in Swift In Swift, dates and times are stored in a 64-bit floating point number measuring the number of seconds since the reference date of January 1, 2001 at 00:00:00 UTC. This is expressed in the Date structure. The following would give you the current … Read more

What does “Fatal error: Unexpectedly found nil while unwrapping an Optional value” mean?

Background: What’s an Optional? In Swift, Optional<Wrapped> is an option type: it can contain any value from the original (“Wrapped”) type, or no value at all (the special value nil). An optional value must be unwrapped before it can be used. Optional is a generic type, which means that Optional<Int> and Optional<String> are distinct types — the type inside <> is called the Wrapped type. Under the hood, … Read more