Challenge C: Functional ratings

In this final challenge you will have a list of app names with associated ratings they’ve been given (note — these are all fictional apps!).

Create the data dictionary like so:

let appRatings = [

"Calendar Pro": [1, 5, 5, 4, 2, 1, 5, 4],

"The Messenger": [5, 4, 2, 5, 4, 1, 1, 2],

"Socialise": [2, 1, 2, 2, 1, 2, 4, 2]

]

First, create a dictionary called averageRatings which will contain a mapping of app names to average ratings. Then, use forEach to iterate through the appRatings dictionary, use reduce to calculate the average rating and store this rating in the averageRatings dictionary.

Finally, use filter and map chained together to get a list of the app names whose average rating is greater than 3.

Now that you know the basics of Swift, it’s time to put everything you’ve learned together to create your own types.

You can create your own type by combining variables and functions into a new type definition. For example, integers and doubles might not be enough for your purposes, so you might need to create a type to store complex numbers. Or maybe storing first, middle and last names in three independent variables is getting difficult to manage, so you decide to create a FullName type.

When you create a new type, you get to give it a name; thus, these custom types are known as named types. Structures are a powerful for modeling the real world concepts. You can encapsulate related concepts, properties and methods into a single, cohesive model.

Chapter 11, Structures

    • Chapter 12, Properties
    • Chapter 13, Methods

Swift, in fact, includes four kinds of named types: structures, classes, enumerations and protocols. Now that you understand structures work with methods and properties you can see how the other named types use these same concepts, how they differ, and where you want to use each.

Chapter 14, Classes

    • Chapter 15, Advanced Classes
    • Chapter 16, Enumerations
    • Chapter 17, Protocols

Finally, you expand your knowledge of the type system by learning about generics: types and methods that take as input other types instead of just methods. Swift’s key to safety, speed and expressiveness lies in the ability to utilize generic types.

Chapter 18, Generics

Swift Apprentice Section III: Building Your Own Types

Custom types make it possible to build large and complex things with the basic building blocks you’ve learned so far. It’s time to take your Swift apprenticeship to the next level!

You’ve covered many of the fundamental building blocks of programming in Swift. With variables, conditionals, strings, functions and collections, you’re ready to conquer the world! Well, almost. ;]

Most programs that perform complex tasks would probably benefit from higher levels of abstraction. In other words, in addition to an Int, String or Array, they’ll need new types that are specific to the domain of the task at hand. Keeping track of photos or contacts, for example, demands more than just the simple types you’ve seen thus far.

This chapter introduces structures, which are the first named type you will learn about. As you will learn, structures are types that can store named properties and define their own behaviors. Like a String, Int or Array, you can define your own structures to create named types to later use in your code. By the end of this chapter, you’ll know how to define and use your own structures.

You begin your adventure into custom types with pizza.

results matching ""

    No results matching ""