Sign In Start Free Trial
Account

Add to playlist

Create a Playlist

Modal Close icon
You need to login to use this feature.
  • Book Overview & Buying Learning Swift
  • Table Of Contents Toc
  • Feedback & Rating feedback
Learning Swift

Learning Swift

By : Andrew J Wagner
4.8 (5)
close
close
Learning Swift

Learning Swift

4.8 (5)
By: Andrew J Wagner

Overview of this book

If you are looking to build iOS or OS X apps using the most modern technology, this book is ideal for you. You will find this book especially useful if you are new to programming or if you have yet to develop for iOS or OS X.
Table of Contents (13 chapters)
close
close
12
Index

Extending system types to reduce code


Another powerful feature that we briefly covered in Chapter 3, One Piece at a Time – Types, Scopes, and Projects is the ability to extend the existing types. We saw that we could add an extension to the String type that would allow us to repeat a string n multiple times. Let's look at a more practical use case for this and discuss its benefits in terms of improving our code.

Perhaps, we can create a grade-tracking program where we will print out a lot of percentages. A great way to represent percentages is using a Float with a value between 0 and 1. Floats are great for percentages because we can use all the built-in math functions and they can represent pretty granular numbers. The hurdle that we need to cross while using a float to represent a percentage is its printing. If we simply print out the value, it will most likely not be formatted in the way we would want. People prefer percentages to be out of 100 and prefer a percent symbol after it.

In a...

Unlock full access

Continue reading for free

A Packt free trial gives you instant online access to our library of over 7000 practical eBooks and videos, constantly updated with the latest in tech
bookmark search playlist font-size

Change the font size

margin-width

Change margin width

day-mode

Change background colour

Close icon Search
Country selected

Close icon Your notes and bookmarks

Delete Bookmark

Modal Close icon
Are you sure you want to delete it?
Cancel
Yes, Delete

Confirmation

Modal Close icon
claim successful

Buy this book with your credits?

Modal Close icon
Are you sure you want to buy this book with one of your credits?
Close
YES, BUY