Coding Explorer Blog

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

  • Home
  • Apps
  • About
  • Contact

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

DateComponents — Class Reference

Xcode 11.6 Swift 5.2.4

Last updated on August 12, 2020

As mentioned last time in my post Date — Class Reference, now we go a bit more in depth with DateComponents.  In my opinion, this is the real powerhouse for dealing with time in Swift or Objective-C.  When I was first learning iOS, I looked, understandably, at Date to work with dates.  As such, a test project used Date and it’s initializers, including NSTimeInterval.  When I had mentioned that at my local NSCoderNight, I was told to look into DateComponents, and boy was that good advice!  Not only is it a pain to calculate the seconds for the intervals you want, what about dealing with Daylight Savings Time, leap years, or just plain different calendars!  Now with DateComponents, I can set a date in a way that I understand, and in concert with Date and Calendar, it will even deal appropriately with the intricacies of calendars without me having to do so myself.

DateComponents can be used to either specify a specific date, or to specify a timespan in your Swift iOS apps.  There is no difference between the uses as far as DateComponents is concerned you just set the appropriate components (year, day, hour, second, etc).
[Read more…]

Filed Under: Class Reference Tagged With: Swift

Date — Class Reference

Xcode 11.6 Swift 5.2.4

Last updated on August 12, 2020

An NSDate object specifies a particular point in time.  Despite “date” being in its name, it really stores time, of which the date is a component.  Under the hood, NSDates (as far as the public superclass we can see is concerned) are specified as a number of seconds past a reference date.  NSDate’s reference date is the first moment of January 1, 2001 in the GMT timezone.  These seconds are stored as a Double, which allows NSDate to range from milliseconds to years.  What we see as an NSDate is actually an abstract public superclass for a cluster of classes related to dates.  The internal classes are private, so talking about them here wouldn’t be particularly helpful, and it is probably best not to mess with them anyway.

Since Swift 3 though, most “NS” prefixes were removed from the Swift Standard Library, so now it is simply called “Date”.  If you need to save a timestamp for something in your iOS app, or maybe have a countdown timer for an app or game, you will be dealing with the Date class, so let’s take a look at it.
[Read more…]

Filed Under: Uncategorized

Add sharing to your Swift app via UIActivityViewController

Xcode 11.6 Swift 5.2.4

Last updated on August 12, 2020

In several iOS apps, such as Safari or the Camera app, you can click a button that brings up an interface that makes it easy to send or share what you are looking at via messages, Twitter, Facebook, etc.  That interface is known as the UIActivityViewController.  When I first learned about it, I didn’t even know what to search for.  I think I originally started with “share sheet” and went on from there, so it may seem silly to point out something so obvious, but when I first tried, I knew what I wanted, I just had no idea what it was called.

If you’ve paid any attention to WWDC a few years, you have probably saw that this is the new home for Share and Action extensions.  Today though, we are just going to cover the built in aspects of using UIActivityViewController in your Swift app.  We will cover those sometime later.
[Read more…]

Filed Under: Tutorial Tagged With: Swift

  • « Previous Page
  • 1
  • …
  • 3
  • 4
  • 5
  • 6
  • 7
  • …
  • 16
  • 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