Coding Explorer Blog

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

  • Home
  • Apps
  • About
  • Contact

Displaying a human readable NSDate

Last updated on July 23, 2014

Much like Casey Liss of the Accidental Tech Podcast, talked about recently on the Debug podcast, I came from C# before I started working on Objective-C.  One frustration he had originally with Objective-C was with getting a human readable date out of an NSDate object, when it was so easy to just type someDateTime.toString() in C#.  That was also one of my earliest frustrations, and I thought it would be nice to share with my readers.

In C#, you store dates in objects of the type DateTime.  The Objective-C equivalent of this is NSDate.  In C#, you can simply create a specific date date with:

DateTime someDate = new DateTime(2013, 12, 31);

And that would make a DateTime object for December 31, 2013.  There are a few ways to do this in Objective-C, and while they are not as easy, they do make it much more friendly to using non-Gregorian calendar systems.  NSDateFormatter can both format an NSDate into a string (more on this later), but it can also attempt to parse a string with date data.  The more exact way though, would probably be to use NSDateComponents.

[Read more…]

Filed Under: Syntax

Property Attributes in Objective-C

Last updated on July 23, 2014

In my previous articles, you’ve probably seen properties used many times. In this article I wanted to write down (in one place) and explain the attributes that can be used on them. At least in this context, “attributes” refers to the modifiers to the property (usually strong and nonatomic in my previous articles). I have covered those two a little bit in a previous article, but I wanted to list them all here.

 Valid Property Attributes

  • atomic
  • nonatomic
  • strong
  • weak
  • readwrite
  • readonly
  • getter=
  • setter=
  • copy
  • assign
  • retain
  • unsafe_unretained

[Read more…]

Filed Under: Syntax

Objective-C Categories

Last updated on July 23, 2014

Have you ever wished you could add a method to an existing class that you don’t have the code for?

Well, now you can, with categories.

Categories let you extend an existing class with whatever method you deem fit.  It is pretty useful to add helper methods to other classes that help parse the date in a way the class make perhaps did not intend.  You can also use it to split up your own classes so that if you have some big omni-class that does several things, you can opt in to just the parts you want by only including those categories.

[Read more…]

Filed Under: Syntax

Storing data with NSUserDefaults

Last updated on June 25, 2014

How can you store data between application launches?

There are plenty of ways, but here I will show you the simplest, NSUserDefaults.

NSUserDefaults is a simple property list (aka plist) where an app can store simple data.  While there is no limit to its size (besides the device’s own limits), you should not store a large amount of data here.  The file is written and read atomically (as a whole), so the more data that is in there, the longer that will take.  Nonetheless, it is a great place to store settings, high scores, and the like.

[Read more…]

Filed Under: Syntax

Updating an app when returning from background in iOS

Last updated on June 25, 2014

I am working on an app that needs to update the UI based on the current date when it is loaded.  I told it to do the update in my viewWillAppear, but about half the time it would not update when I loaded the program.  Shouldn’t viewWillAppear always be called when your view is about to appear?  Apparently not, but I’ll tell you how I got the functionality I was looking for.

Updating from the background with Notifications

So, viewWillAppear will run every time your view is loaded from disk, or being called to be onscreen by a segue from another view.  However, when I leave the app, use some other apps and come back, as far as my app was concerned, this view never left.  It left as far as the system was concerned, but in my app’s little world, it just had the same view up as it did when I left the app.  When I left the app, I sent it to the background.  That is the difference.

So, how do I update my screen when it returns from the background?  There are two notifications that would be of help here.  They are:

  • UIApplicationWillEnterForegroundNotification
  • UIApplicationDidBecomeActiveNotification

As your program is brought back from the background, these two notifications are raised, in that order.  UIApplicationWillEnterForegroundNotification will run right before the view should appear onscreen, and UIApplicationDidBecomeActiveNotification should run just after.  In my previous post about Supporting Dynamic Type, I mentioned how to subscribe to notifications, but for simplicity’s sake, I will show you here.

[Read more…]

Filed Under: Syntax

  • « Previous Page
  • 1
  • …
  • 12
  • 13
  • 14
  • 15
  • 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