Chapter    13

Extensions

When you write object-oriented programs, you’ll often want to add some new behavior to an existing class. For example, you might have designed a new kind of tire, so you’d subclass Tire and add the new, cool stuff. When you want to add behavior to an existing class, you often create a subclass.

But sometimes subclassing isn’t convenient. For example, to add some new behavior to String, you can sublass the string as your own string type such as MyString, but what if you’re using a toolkit or library they will have no knowledge of this new new class. When the toolkit or library return a string it will be returned as the original string type, you will end up converting those String type to MyString every time if you wanted ...

Get Learn Swift on the Mac: For OS X and iOS now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.