Coding Explorer Blog

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

  • Home
  • Apps
  • About
  • Contact

Archives for December 2014

Swiftly getting a human-readable date with NSDateFormatter

Xcode 11.6 Swift 5.2.4

Last updated on May 23, 2023

Now that we know a bit about Date and DateComponents, let’s actually get a readable date in our Swift app, shall we?  This is the job of DateFormatter.  DateFormatter is a class that can take a Date, and output a String describing that time/date as its format instructions dictate.  In other words, you tell it what you want to know, and it will give you a String that says that.  It has a few pretty useful built-in formats, and the capability to accept a custom date format string.

There’s not too much to the actual creation of an DateFormatter object.  It pretty much just has an empty initializer (and an NSCoding one, but we aren’t covering NSCoding today).  After initializing your DateFormatter, you set the appropriate style for the date and time, and that’s all you need.
[Read more…]

Filed Under: Class Reference Tagged With: Swift

NSUserDefaults — A Swift Introduction

Xcode 11.6 Swift 5.2.4

Last updated on August 12, 2020

So, let’s say we have an app that needs to remember a few simple things that the user puts in when they first load the app.  It needs to remember the user’s name and birthday, to show on some view controller, or maybe even for a countdown on their Apple Watch.

There are plenty of ways to save data for your app.  Some are easy to use, but rather limited, while others are much harder to use, but give you a lot more capabilities.  Today, we are going to cover something on the easy, but limited end of the spectrum.  For the app mentioned above, the information we’re storing will be used to set this app up with default values, for this user anyway.

That is why this method is called NSUserDefaults.  It has its limitations, but it is very easy to use, and is ideal for simple storage of things like Strings and numbers.
[Read more…]

Filed Under: Tutorial Tagged With: optionals, Swift

Custom Operators in Swift

Xcode 11.6 Swift 5.2.4

Last updated on August 12, 2020

You have been spared the terrible pun for this title, I knew the last one was trouble when it was typed in.

So, in the earlier article Operator Overloading — Tailor Swift To Your Needs, you saw how you could add functionality to already existing operators to extend their functionality to work with new classes, or in that post’s case, existing ones that it might make sense with.  Now, what if you want a completely new operator?  What if you have functionality so unique, that no current operator makes sense?  That’s where custom operators comes in.  You can declare your own operator and have it run whatever code you want.  Now, like last time, this is a very powerful, but very dangerous capability.  You COULD have it play sounds, make network calls, draw something to screen, or any number of annoying things, but you really should not have those in your operator overloading or custom operator methods.  They are meant to make things more readable, not less.  Unless you use some emoji for a loudspeaker in your operators, you really shouldn’t have it play sounds, and even if you do, why not just use a method?

Also of course, since these are meant to increase readability, you have to be careful about using these.  For instance, can you tell me what the ” #$^&* ” operator does?  Obviously somebody new to a codebase needs some time to get up to speed with it, and even normal functions will take some time to learn the intricacies of.  However, if you compress a gigantic algorithm into a custom operator, that may take longer to figure out than a well named method doing the same.

Nonetheless though, if you use custom operators sparingly, well document them, and don’t give them side effects (like making network calls, playing sounds, etc), they can very useful for increasing legibility and decreasing the character count in your code.
[Read more…]

Filed Under: Swift

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