Coding Explorer Blog

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

  • Home
  • Apps
  • About
  • Contact

Archives for September 2014

Swift Extensions Part 1 — Computed Properties and Initializers

Xcode 11.6 Swift 5.2.4

Last updated on August 12, 2020

So, there has been a reason for my most recent choice of topics to discuss.  This isn’t some grand culmination post, but the topic for today is related to several of the previous topics due to what it can do.  Back in Objective-C, you could extend classes for which you did not have the source code with something called categories.  If you are curious about Objective-C categories, I wrote about them back when this blog was still about Objective-C in my post Objective-C Categories.  This post was much earlier in my blogging career, so it may not be as polished as my current posts, so, you have been warned.

Anyway, Objective-C Categories gave you the ability to add additional methods to another class for which you didn’t have the original source code.  I used this capability in a later Objective-C post Introduction to UIColor, to create a category that made it easier to create a UIColor for my purposes.  I didn’t need to customize alpha (transparency) each time, and I wanted to use color values between 0 and 255 instead of 0.0 and 1.0, so I made a category that translated my 0 to 255 values to their 0.0 to 1.0 equivalents, and hard-coded the alpha to 1.0 (fully opaque).

These are not the same as iOS 8 extensions, such as the Today View Widgets, UIActivityViewController sharing extensions, etc.  We will cover those in a later post.

When you look at the capabilities of Swift Extensions, they are basically doing the same thing as Objective-C categories, adding methods to existing classes.  With one exception, they are all different flavors of methods.
[Read more…]

Filed Under: Swift

Instance Methods and Type Methods in Swift

Xcode 11.6 Swift 5.2.4

Last updated on August 12, 2020

We touched a bit on this in a previous post Classes In Swift — An Introduction.  To recap what was mentioned in that post, methods are functions that are associated with a type.  In other words, all methods are functions, but not all functions are methods.  Functions can be written outside of the context of a type, especially in playgrounds.  I felt it would be best to state that in a post dedicated to them, instead of having it just nestled in the Classes post.  I also have learned of some nuances about them, that there are a few differences between them, particularly in parameter naming.  While I’m at it, we’ll quickly cover the idea of type methods as well.
[Read more…]

Filed Under: Swift

Designated Initializers and Convenience Initializers in Swift

Xcode 11.6 Swift 5.2.4

Last updated on August 12, 2020

Today we will be learning about another aspect of Class Initializers.  The Swift language has two different types of initializers they are called designated initializers and convenience initializers.  These existed in Objective-C, but a few rules have changed in Swift, and a very helpful keyword was introduced.  We will discuss how designated and convenience initializers are used, and how they can work together to get your classes ready for use.

Designated Initializers

Like most constructs in Swift, designated initializers are aptly named and do exactly what they say they do.  They are the main initializers to be used for a class.  A class must have one designated initializer, but it is not limited to one.  It can have multiple if necessary, but most classes only have one.
[Read more…]

Filed Under: Swift Tagged With: properties

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

Class Initializers

Xcode 11.6 Swift 5.2.4

Last updated on August 12, 2020

Writing Class Initializers

So, I have mentioned a few times about initializers on this blog.  I want to talk today about writing and using initializers for classes, later we can talk a bit more about how they are different for value types like structs or enumerations.

Initializers do exactly what is sounds like they do, they initialize your instances and get them ready to use.  Specifically, before an object can be used, it must set all of its stored properties to valid values.  This can be handled in one of three ways:

  • Give them default values in their property definition
  • Set them during the initializer
  • Declare them as optionals

[Read more…]

Filed Under: Swift

  • 1
  • 2
  • Next Page »

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