Xcode: What is a target and scheme in plain language?

Solution 1:

I've added in Workspace and Project too!

  • Workspace - Contains one or more projects. These projects usually relate to one another
  • Project - Contains code and resources, etc. (You'll be used to these!)
  • Target - Each project has one or more targets.
    • Each target defines a list of build settings for that project
    • Each target also defines a list of classes, resources, custom scripts etc to include/ use when building.
    • Targets are usually used for different distributions of the same project.
      • For example, my project has two targets, a "normal" build and an "office" build that has extra testing features and may contain several background music tracks and a button to change the track (as it currently does).
      • You'll be used to adding classes and resources to your default target as you add them.
      • You can pick and choose which classes / resources are added to which target.
        • In my example, I have a "DebugHandler" class that is added to my office build
      • If you add tests, this also adds a new target.
  • Scheme - A scheme defines what happens when you press "Build", "Test", "Profile", etc.
    • Usually, each target has at least one scheme
    • You can autocreate schemes for your targets by going to Scheme > Manage Schemes and pressing "Autocreate Schemes Now"

Solution 2:

A target is an end product created by running "build" in Xcode. It might be an app, or a framework, or static library, or a unit test bundle. Whatever it is, it generally corresponds to a single item in the "built products" folder.

A scheme represents a collection of targets that you work with together. It defines which targets are used when you choose various actions in Xcode (Run, Test, Profile, etc.) Often, you will have just one scheme, which uses the main app target for the Run, Archive, and Profile actions, and a unit test target for the Test action. If you were building two related apps, you might have two schemes that used the same unit test bundle but different app targets.

The main benefit of schemes (introduced in Xcode 4) is that they allow you to switch between running your app and your unit tests without needing to toggle the selected target.

Solution 3:

I am a visual person, hence to explain the concept I will be using a diagram.

When you have multiple targets they can be one-to-one matched with Xcode's Run,Test,Profile actions, this concept defines a scheme

enter image description here

A target is a version of your Project,i.e targets differ slightly in classes & resources to use during built time. A project can have multiple built time setting for separate distribution requirements.

Solution 4:

Xcode structure

Workspace  
  -> Project 
    -> Target 
     -> Dependency 
      -> Scheme 
        -> Action
        -> Build Configuration  
          -> Build Configuration File(.xcconfig) 

Workspace (.xcworkspace) - is a container of multiple projects. It was created as a next step of cross-project references[About]

  • Workspace contains all schemes from included projects
  • Workspace handles all implicit dependencies[About]

Observations:

  • It is safe to work with different projects inside the same workspace and do not catch
//if you try to open two projects on two Xcode instances
Couldn't load Project.xcodeproj because it is already opened from another project or workspace
  • Cocoapods[About] working with workspace where creates Pods project

Project (.xcodeproj) - It is a container for targets and scheme. It defines code files, resources...

Also Projects manages Build Configuration(changed by scheme) and Build Configuration File[About]

You can convert existing Project into Workspace

File -> Save As Workspace...

[Workspace vs Project]

Target - PBXNativeTarget section. Defines a specific set of build settings that generate:

  • Application target
  • Library and framework targets
  • Test
  • Aggregate[About]. E.g. it is used to create a Universal framework or Umbrella framework

Scheme

Contains action(run, test, profile, analyze, archive) + configuration(additional arguments, [Build Configuration], diagnostic)

Scheme can be shared which helps you in CI, Carthage[Example]... and located:

<project_path>/<project_name>.xcodeproj/xcshareddata/xcschemes

Dependency - Targets can have dependencies. Dependency is a source link against. These dependencies can be linked statically or dynamically[About] There are two types of them:

  • Explicit Dependency[About] - Source code of the dependency that is located in the same project or nested project
  • Implicit Dependency[About] - Source/closed code of the dependency that is located in the project that is a part of the same workspace.

[Vocabulary]