# Setting Up a New Xcode Project

### Creating a new Xcode project

Let's begin by creating our first, new Xcode project! To do so, launch the Xcode application and press the **Create a new Xcode project** option

![](/files/-M0012ggaXpvOa1HgEHn)

Now you will now be prompted to choose the type of application you want to create. Apple provides a number of application templates for you to use. For the duration of this class, however, we will **always** be creating a **Single View App**, which will create a single non-complex view app for you to add to. If you have a newer Xcode version, you should click the option that says **App** and make sure you select **iOS** for your category–not multiplatform.

![](/files/-M00OM1i9ye0dosm8CnW)

Next, give a **name** for your new application. Please also make sure that you've selected **Language:** **Swift** and **User Interface**: **Storyboard**. After doing so, you may click **Next**, and then save your application in the directory that you want to save it in. You can keep the default settings for the other fields.

![](/files/-M00PHAg6aRUHQeD7rI5)

### Navigating Through Xcode

Now that you’ve created a new Xcode project, you can start working! Before you start, however, we are going to go over some small tips and tricks for using and navigating to change the type of device to build your application on, click on the button next to the Play Button that says code. Firstly, on the left side of the project, you should see the **file hierarchy** for the project. This allows you to see how the files that your project contains as well as navigate or change any of them by simply clicking on the file that you want to change.

![](/files/-MP5LT7zqd9GdTuLFU1I)

#### Running Simulator

Now, if you look to the top-left of the project, you should see a **play button** next to another button that should say something like **SampleApplication > iPhone 12 Pro Max**. The play button is the button that you will want to click when you have written some code and want to see how your application looks. Clicking on the play button **(or clicking CMD + R)** will run your application on a simulator device.&#x20;

**iPhone 12 Pro Max** (it may something different on your application, i.e. iPhone X). This should then bring down a dropdown menu of all the possible devices that you can run your application on. Simply click on the device that you want and then you are done!

#### Creating New File

Now, let's say that you want to **create a new Swift file.** To do so, navigate to **File -> New -> File...** or simply press **CMD + N.** You should now see a window asking you what type of file you want to create. You will often be choosing between either an empty **Swift File** or a **Cocoa Touch Class**, depending on whether you want some of the boilerplate code for built-in swift classes (you will learn more about this as you go through the course).

![](/files/-M00j1_lS9nP7tWfQ2U5)

For now, we will continue with a **Cocoa Touch Class**. After clicking next, you should see a window like the one below. Next to the **Class:** textfield, put in the name of your new file. Then, in the **Subclass of:** dropdown, find and select the type of file that you want to create. In the screenshot below, I want to create a new UIViewController file. Lastly, click Next and then you are done!

![](/files/-M00jFFffb0NbzTMEU2m)

### Setup for Programmatic AutoLayout

There are multiple ways to layout views to a screen in iOS development – some examples include frame-based, storyboards, and programmatic AutoLayout. In this course, we'll be learning [**programmatic AutoLayout**](/resources/archived-past-semesters/sp23/chapters/2.-uikit-and-autolayout/lecture-handout.md#what-is-autolayout).&#x20;

To use Swift programmatically, there are a few things you need to do:

1. Delete `Main.storyboard` . Newer Xcode versions might be just `Main`. Right click on the file, select **Delete --> Move to Trash.** &#x20;

![](/files/-M1C5Bpqk7pbkXxU64OS)

2\. Navigate to `Info.plist` and select the minus symbol next to `Main storyboard file base name` to delete it.

![](/files/-M1C5uYL2Tt-inVp3N4z)

If you have a newer version of Xcode, you might not see this entry in `Info.plist`. Instead, you can either:&#x20;

1. Search for `main` like so and click on the first result

![](/files/-Ml3216zKi32B0-D9B_A)

&#x20;    Then delete `Main` for `UIKit Main Storyboard File Base Name`

![](/files/-Ml328uk0i9THVp3oa39)

2\. Click on the app name in the left pane as shown in the image, select the app name under `Targets` and under the `Deployment Info` section within the `General` tab, change `Main Interface` to `LaunchScreen`

![](/files/-Ml84eeLiszmaRXpjOtU)

*The following steps may be different for you depending on which version of Xcode you are running. If you are running the newer version of Xcode, you will see `SceneDelegate.swift` in your sidebar. If not, you will only see `AppDelegate.swift`.*&#x20;

3\. **If you have `SceneDelegate.swift` in the sidebar,** expand the `Application Scene Manifest` option all the way. Select the minus symbol next to `Storyboard Name` to delete it.&#x20;

![](/files/-M1CBEaWITOf8idpu0D9)

4\. SceneDelegate OR AppDelegate

#### **If you have `SceneDelegate.swift` :**&#x20;

In `SceneDelegate.swift`, go to the first function, `scene(_:willConnectTo:options:)`. This function is called whenever a scene is added to your app. In our case of setting up the project, we're dealing with the first scene of the app -- i.e. the first screen of you app. Write the following lines of code:&#x20;

![](/files/-M1CEFj7o6SncJsHrSW1)

Line-by-line, here's what this code is doing:\
**21**. Create a `UIWindow` instance that will act as our key window, i.e. the main "container" for our app . Apple documentation refers to this as a "backdrop for your app's user interface". \
**22**. Create an instance of `ViewController`, which is the view that we want to be the base of our application, i.e. the view that shows when a user first opens the app. \
**23**. Set the window's `rootViewController` to be a `UINavigationController` with our rootVC as its root view. \
&#x20;     *- If you don't want to embed your app in a navigation controller (discussed in* [*Lecture 3*](/resources/archived-past-semesters/sp23/chapters/3.-navigation-mvc-and-delegation/lecture-handout.md#navigation)*),* \
&#x20;        *you would just do* `window.rootViewController = rootVC` *on this line.* \
**24**. Set our newly created `window` to the `SceneDelegate`'s `window` property.\
**25**. Make our window visible! (Instead of hidden)&#x20;

#### **If you DON'T have `SceneDelegate.swift`:**&#x20;

In `AppDelegate.swift`, add this instance variable to the top of the class:

![](/files/-M1TBwFAEGJUZzHev-mn)

Now go into the first function, `application(_:didFinishLaunchingWithOptions:)`. This function is called whenever your app launches. This is where we want to set up our app. Write the following lines of code:&#x20;

![](/files/-M1TCh1kvfjUuRz9Thp2)

Line-by-line, here's what this code is doing:\
**14**. Declare a `UIWindow` object at the top level, to be instantiated elsewhere in the file. \
**18.** Create an instance of the `window` object with the size of the phone's screen, which will serve as the main container for our app.\
**19.**  Create an instance of `ViewController`, which is the view that we want to be the base of our application, i.e. the view that shows when a user first opens the app. \
**20**. Set the window's `rootViewController` to be our viewController from line 19.\
**21.** Set our newly created `window` to the `AppDelegate`'s `window` property.\
**22.** Make our window visible.<br>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://ios-course.cornellappdev.com/resources/archived-past-semesters/sp23/cheat-sheets/setting-up-a-new-xcode-project.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
