Coding Explorer Blog

Exploring how to code for iOS in Swift and Objective-C

  • Home
  • Apps
  • About
  • Contact

Swift Property Observers

Xcode 11.6 Swift 5.2.4

Last updated on August 12, 2020

In my previous article Class Initializers, I had mentioned property observers, and glossed over them a bit.  Now we’ll discuss them in a bit more depth.

Why use a Property Observer?

Back in Objective-C, if you wanted to do any special handling for setting a property, you would have to override the setter, reimplement the actual value setting (that was originally done for you), and then add whatever you wanted to do besides that, like posting a notification of a change.  Swift’s property observers save you from having to reimplement the setter in those cases.

Property Observers are somewhat similar to computed properties.  You can read more about those in my previous article Computed Properties in Swift.  For computed properties, you write custom code for the getter and setter.  For property observers, you write custom code only for setting, for right before (willSet) and right after (didSet).  The main purpose of Swift’s property observers is to watch for when a property is set.  As such, property observers are only useful for variables (var properties), and cannot be written for constants (let properties).
[Read more…]

Filed Under: Swift Tagged With: properties, Swift

Type Casting in Swift

Xcode 11.6 Swift 5.2.4

Last updated on August 10, 2020

In my previous post, Generic Functions in Swift, I used a type casting operator to check for inheritance.  It was simple enough to mention there, but I felt I should cover type casting in Swift a bit more in-depth in its own post.  Type casting is a way to convert an object of one type to another.

There is one term that is used a lot when talking about type casting, so it should probably be defined upfront, that term is downcast.  According to Wikipedia, downcasting is the act of casting a reference of a base class to one of its derived classes.  There is an opposite term to this one, the obviously named upcast.  While this term is not used in Apple’s iBook, it is used in the WWDC video “Swift Interoperability In Depth.”  It of course means to go the other way, casting a derived class back up to one of its base classes.
[Read more…]

Filed Under: Swift Tagged With: optionals, Swift

WWDC 2014 Reactions and the Swift direction of this Blog

Last updated on August 6, 2014

So, to be perfectly honest, I was not expecting anything groundbreaking at this years WWDC.  I thought there would be iOS 8, which would have a few new features, but not TOO much.  I mean, after iOS 7, there should definitely be some cool-down time before anymore big changes to the OS.  I also thought they would probably talk about OSX 10.10, and there would be a few new features too.  Not that I was expecting Apple to be lazy this year, but you can’t make something groundbreaking and amazing every single year.

[Read more…]

Filed Under: General Tagged With: Swift

  • « Previous Page
  • 1
  • …
  • 3
  • 4
  • 5

Subscribe to the Coding Explorer Newsletter

* indicates required

Follow Us

Facebooktwitterrss

Recent Posts

  • Error Handling in Swift
  • Creating and Modifying a URL in your Swift App
  • Watch Connectivity in Swift — Application Context
  • watchOS Hello World App in Swift
  • API Availability Checking in Swift

Categories

  • Class Reference
  • General
  • Interface Builder
  • My Apps
  • Objective-C
  • Swift
  • Syntax
  • Tutorial
  • Uncategorized

Archives

  • May 2016
  • March 2016
  • February 2016
  • December 2015
  • July 2015
  • June 2015
  • April 2015
  • February 2015
  • January 2015
  • December 2014
  • November 2014
  • October 2014
  • September 2014
  • August 2014
  • July 2014
  • June 2014
  • May 2014
  • April 2014
  • March 2014
  • January 2014
  • November 2013
  • September 2013
  • August 2013
  • July 2013
  • Terms Of Use
  • Privacy Policy
  • Affiliate Disclaimer

Subscribe to the Coding Explorer Newsletter

* indicates required

Copyright © 2025 Wayne Media LLC · Powered by Genesis Framework · WordPress