Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds
Arrow up icon
GO TO TOP
Mastering macOS Programming

You're reading from   Mastering macOS Programming Hands-on guide to macOS Sierra Application Development

Arrow left icon
Product type Paperback
Published in May 2017
Publisher Packt
ISBN-13 9781786461698
Length 626 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Authors (2):
Arrow left icon
Stuart Grimshaw Stuart Grimshaw
Author Profile Icon Stuart Grimshaw
Stuart Grimshaw
Gregory Casamento Gregory Casamento
Author Profile Icon Gregory Casamento
Gregory Casamento
Arrow right icon
View More author details
Toc

Table of Contents (28) Chapters Close

Title Page
Credits
About the Author
About the Reviewer
www.PacktPub.com
Customer Feedback
Dedication
Preface
1. Hello macOS FREE CHAPTER 2. Basic Swift 3. Checking Out the Power of Xcode 4. MVC and Other Design Patterns 5. Advanced Swift 6. Cocoa Frameworks - The Backbone of Your Apps 7. Creating Views Programmatically 8. Strings and Text 9. Getting More from Interface Builder 10. Drawing on the Strength of Core Graphics 11. Core Animation 12. Handling Errors Gracefully 13. Persistent Storage 14. The Benefits of Core Data 15. Connect to the World - Networking 16. Concurrency and Asynchronous Programming 17. Understanding Xcodes Debugging Tools 18. LLDB and the Command Line 19. Deploying Third - Party Code 20. Wrapping It Up

Customizing operators


In addition to the operators provided by the Swift language, we can both override existing operators and, where necessary, create new ones.

Adding operator implementations to types

This sounds so complicated, but it is, in fact, really easy. All we need to do is define an implementation of any given operator for any given type.

Note

For the time being, we'll use a custom type, although what we are doing can be used equally effectively for Swift and Cocoa types.

We'll start by defining a custom type:

struct GridMovement 
{ 
  let rows: Int 
  let cols: Int 
} 

Now that we have it, we notice that it would make perfect sense to be able to apply some basic arithmetical operators to our type. We could add these to the main type declaration, but convention (young though it is) suggests we put it in an extension:

extension GridMovement 
{ 
  static func + (lhs: GridMovement, 
                 rhs: GridMovement) -> GridMovement 
  { 
    let rows = lhs.rows + rhs.rows 
    let cols...
lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €14.99/month. Cancel anytime
Banner background image