App developer

Xcode Storyboard and Same Named Files with Different Targets

Today, I came across a strange issue in Xcode where the Storyboard was unable to determine the difference between two ViewController.swift files that were part of the same project, but each had a different target (one was for iOS and one was for macOS). Whenever working with macOS Storyboard and control-dragging to the macOS ViewController.swift file, all outlets and actions were created in the iOS ViewController.swift file.

Even after renaming the macOS target’s file (and class) to ViewController-Mac, linking it up in Storyboard, cleaning, and then attempting to control-drag, Xcode threw an error stating that no details could be found for the renamed class. Other than having completely separate Xcode projects, I couldn’t find a workaround.

Here’s how to recreate the issue:

  1. Create a New Single View Application for iOS
  2. Add a New Cocoa Mac Application target
  3. Open the Main.storyboard file for the Mac Application target
  4. Drag a NSButton from the Object Library onto the View Controller
  5. Click the Show the Assistant Editor button (see Issue 1)
  6. Using the Assistant Editor’s Manual Mode, select the ViewController.swift file for the Mac Application target
  7. Drag from the NSButton to the ViewController.swift file and create an outlet or action for the button (see Issue 2)

Issue 1: The Assistant Editor shows the ViewController.swift file for the iOS target.

Issue 2: The outlet or action is added to the ViewController.swift file for the iOS target