Coding Explorer Blog

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

  • Home
  • Apps
  • About
  • Contact

How to Create a UIColor in Swift

Xcode 11.6 Swift 5.2.4

Last updated on August 12, 2020

There comes a time in many iOS developers’s careers that they will need to save a reference to a color itself.  Maybe it is just to change the background color of a view, maybe it is for custom drawing, or any number of other reasons.

This is where UIColor comes in.  UIColor is particularly optimized to work on iOS devices, so while there are older similar classes like CGColor or CIColor, you should probably use UIColor, unless you are working on some app that needs to translate between different colorspaces.
[Read more…]

Filed Under: Class Reference Tagged With: Swift

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

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

Getting started with NSNotificationCenter

Last updated on August 6, 2014

Have you ever wanted to alert other classes of your app of some event from another one?

Sure, you could have your class poll the other one and keep asking if something has changed, but that seems to be wasteful, since in many cases, there usually won’t be.  You could even not bother with polling, and just read it back in all the time, but if there has been no change, that again is wasteful of processing time, power, etc.  To avoid these issues, I have found that  NSNotification Center is a great answer to this problem.  You can just have your class subscribe to the Notification, and then when it alerts your class that there has actually been an event you’ve been waiting for, you THEN can call that class and read in what is necessary, or react appropriately.

[Read more…]

Filed Under: Class Reference, Objective-C

Getting Started With NSNumberFormatter

Last updated on August 6, 2014

So, you want to print out a floating point number.  Do you want it to as high of accuracy as your computer can muster?  In general, you probably don’t.  If I was splitting a $43.89 check 4 ways, would you really want to know that the answer is $10.9725?  Of course you could round the number yourself, but why, when you can simply use NSNumberFormatter!

[Read more…]

Filed Under: Class Reference

  • 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