Knowing when we’re being “popped” to in viewWillAppear

If we want to know whether viewWillAppear is currently being called not for the first time, we could obviously store “alreadyAppeared” in a boolean or something, but there is an easier solution:


is only true the first time the ViewController is added to its parent, so when being “popped” to in a NavigationController it will be false.


Core Data warning: “Relationship does not have an inverse”

I have a Priority class with id, priority name, and URL to an icon.

I have a Ticket class which has a reference to a single Priority.

Core Data is warning me that I have no inverse relationship, which would make no sense to me and would seem to even be negative for performance. But the follow StackOverflow answer was a huge help in realizing that it actually makes sense to have an inverse relationship since Core Data uses it for data integrity!

Apple generally recommends that you always create and specify the inverse, even if you don’t use the inverse relationship in your app. For this reason, it warns you when you fail to provide an inverse.

Relationships are not required to have an inverse, because there are a few scenarios in which the inverse relationship could hurt performance. For example, suppose the inverse relationship contains an extremely large number of objects. Removing the inverse requires iterating over the set that represents the inverse, weakening performance.

But unless you have a specific reason not to, model the inverse. It helps Core Data ensure data integrity. If you run into performance issues, it’s relatively easy to remove the inverse relationship later.

‘try!’ expression unexpectedly raised an error: Foundation._GenericObjCError.nilError

When calling try! context.fetch(fetchRequest) on an NSManagedObjectContext, I was getting

fatal error: 'try!' expression unexpectedly raised an error: Foundation._GenericObjCError.nilError: file /Library/Caches/, line 178

The issue was that my NSManagedObjectContext itself was nil, it wasn’t anything internal in NSManagedObjectContext fetch, like it looked at first…

So then the question became why it was nil. Turns out I had included the xcdatamodeld file in one of my sub-modules instead of my main target. Because the model is being accessed at run-time, it wasn’t accessible from the main module.

Centering text in UITableViewCells with accessory type

Was trying to have a centered label in custom table view cells but have an accessory type for some. That caused the labels to be indented to the left because the accessory type makes the content view of the cell smaller. Here is the solution I ended up with (and posting to a Stackoverflow question where everyone else’s answer was for default table view cells only).

These solutions work for default cells. If you have your own custom cell, the easiest way to do it is make the label have constraints both to the left margin and the right margin, then create an outlet for your left margin constraint, and set its constant in the cellForRowAtIndexPath method:

cell.accessoryType = isChosen ? .checkmark : .none
cell.leftMarginConstraint.constant = isChosen ? 40 : 0
This way, the right margin which is added for your accessoryType is then added to the left as well.
– via

Shorten page title when navigating further in

When using UINavigationController, the Back button takes the title of the page you’re navigating from. If it’s too long it can create layout issues.

Solution is simple – in viewWillAppear of the originating page, set its title, and it its viewWillDisappear set a shortened version.