Reactive Programming in iOS with Combine

Feb 4 2021 · Swift 5.3, macOS 11.0, Xcode 12.2

Part 3: Combining Operators

17. Challenge: Append and Prepend

Episode complete

Play next episode

Next
About this episode

Leave a rating/review

See forum comments
Cinema mode Mark complete Download course materials
Previous episode: 16. Append Next episode: 18. More Combining Operators

Get immediate access to this and 4,000+ other videos and books.

Take your career further with a Kodeco Personal Plan. With unlimited access to over 40+ books and 4,000+ professional videos in a single subscription, it's simply the best investment you can make in your development career.

Learn more Already a subscriber? Sign in.

Heads up... You've reached locked video content where the transcript will be shown as obfuscated text.

You learned all about prepending and appending operators, so now it's time for a challenge. Back in the day, phone numbers used to be a lot easier to remember. Seven digits in the United States at least was all you needed to remember to call your best friend. Now you need area codes, and if you're calling a business, you probably need to know a person's extension, and if the number was long distance, you need to pretend a 1 in front of it. So for this challenge, in the starter project, you'll find an array with one seven digit phone number represented as a string. You'll also find two values, an area code 401 that goes before the number, and an extension 901 that goes after the string EXT, which follows the phone number. To complete this challenge, take the seven digit number admitted by the publisher, and turn it into a phone number of the form 1-401- the phone number followed by the string EXT followed by the number 901 using what you've learned about prepending and upending operators. Pause the video, try to come up with a solution, and when you're ready, resume the video. Note that there may be multiple ways to accomplish this challenge. And here's a hint, to print out your output in the format above, use the joined method to combine the different elements of the string together. Good luck. (bouncy music) Were you able to avoid the dreaded, "This number cannot be completed as dialed," error message from the phone company. Let's take a look at the solution. First, on the phoneNumbersPublisher, append the 1-, areaCode, and - values to handle the area code part of the string. Then append the EXT string with the necessary space padding as well as the phoneExtension. And finally, collect everything into one array. And in the print statement in the sink, join the strings together with the joined method. Did you get this solution or something similar using the various types of prepend and append operators? If so, congrats. But what if you had a series of extensions and area codes to deal with? That involves some more advanced combining operators, which happens to be the topic of the next episode in the course. See you then.