Day 81
Day 81 ๊ด๋ จ
Project 24, part two
If thereโs one thing you need to know about language, itโs this: language is complicated. And thatโs the number one reason why Swiftโs strings are also complicated: they are designed to do the right thing by default, so you get support for all known languages, plus fantasy languages like Klingon and Elvish, and even emoji โ all without having to do anything special.
Although this might seem like a pointless technicality, youโd do well to remember this quote from Frank Chimero: โpeople ignore design that ignores people.โ Our job isnโt just to write great code and hope for the best, but instead to put our users needs front and centre all the time โ to make sure we meet their needs and solve their problems.
If your software falls on the first hurdle โ if itโs unable to handle their language โ then you might as well not bother.
Today you should work through the wrap up chapter for project 24, complete its review, then work through all three of its challenges.
Wrap up
Wrap up
Coming into this project you might thought strings were trivial, but I hope Iโve shown you that thereโs more to them than meets the eye.
Weโve looked at how strings are different from arrays, how we can write useful extensions for strings, how Swift lets us combine functions together beautifully, and how NSAttributedString
lets us add formatting to strings.
Of course, weโve only just scratched the surface of strings here, but the challenges below will encourage you to write some extensions of your own so you get a better feel for how it works.
Review what you learned
Anyone can sit through a tutorial, but it takes actual work to remember what was taught. Itโs my job to make sure you take as much from these tutorials as possible, so Iโve prepared a short review to help you check your learning.
Click here to review what you learned in project 24.
Challenge
One of the best ways to learn is to write your own code as often as possible, so here are three ways you should try your new knowledge to make sure you fully understand whatโs going on:
- Create a
String
extension that adds awithPrefix()
method. If the string already contains the prefix it should return itself; if it doesnโt contain the prefix, it should return itself with the prefix added. For example:"pet".withPrefix("car")
should return โcarpetโ. - Create a
String
extension that adds anisNumeric
property that returns true if the string holds any sort of number. Tip: creating aDouble
from aString
is a failable initializer. - Create a
String
extension that adds alines
property that returns an array of all the lines in a string. So, โthis\nis\na\ntestโ should return an array with four elements.
Wrap up - Additional
Thatโs another project finished, and one that has given you a glimpse inside Swiftโs inner working โ make sure you share the extensions you write with others!