Add Existing Source Files to Your Project
Xcode maintains a project file as an index of files that belong to your project, along with details about how to build and package your project. To add existing files to your project, choose File > Add Files to [Your Project Name], or Control-click a file or group in the Project navigator, choose Add Files to [Your Project Name] and follow these steps:
Select the files to add to your project. Files already in your project aren’t available for selection.
Select the “Copy items if needed” option if you’re adding files that aren’t in your project folder and don’t want to change the original files.
Specify whether you want to create groups or folder references for any folders in the files you add. Select Create Groups if your folders contain source code or resources that you want Xcode to reference. Select Create Folder References if your folders have frequently changing resources that you want to include in your app bundle, but don’t need Xcode to reference directly.
Select the targets to add to your new files.
Click Add to add the files to your project and update it accordingly.
To add your own code snippet:
For common code structures, Xcode provides snippets you can add to your code. Click the Library button (+) in the upper-right corner of the project window, select the “Show the Snippets library” option, then select a snippet and drag it to the desired location in the editor. Navigate between the placeholders to customize the snippet for your needs.
To add your own code snippet:
Select some code for the snippet, then Control-click and choose Create Code Snippet. Alternatively, choose Editor > Create Code Snippet.
Specify a name and summary for your snippet.
If you don’t select code when creating the snippet, enter code into the snippet editor. Mark any placeholders in your snippet with <#placeholder name#>.
Select the language and platform, enter any text to use for code completion, select the scope, and click Done.
Annotate Your Code for Visibility###
The jump bar and the minimap each provide a quick visual way to navigate your code in the Xcode source editor. Annotate your code with MARK, FIXME, and TODO comments to enhance the power of these tools when organizing your code.
Add a MARK comment to add a heading to a section of code. Include a dash in the comment to instruct Xcode to show a divider line before the section in the jump bar and minimap.
// MARK: - Properties
/// A string describing the fruit list to be displayed as a title.
private var title: String = "Fruit"
Add a TODO comment to indicate where you want to do future work. The jump bar highlights the TODO comment with an icon for easy identification.
// TODO: Make the initializer required
init(title: String, fruitList: [FruitDisplayProtocol]) {
self.title = title
self.fruitList = fruitList
}
Add a FIXME comment to note where you need to make a fix in your code. The jump bar highlights the FIXME comment with a different icon.
var titleForList: String {
get {
// FIXME: Add customization for title
return title
}
}
In addition to the organizational benefits, consistent use of MARK, TODO, and FIXME comments improves your ability to search for common sections, future updates, and needed fixes.
Add Quick Help Comments to Your Code
View Quick Help comments for a symbol by selecting it and choosing View > Inspectors > Quick Help. Alternatively, Control-click a symbol and select Show Quick Help. Add Quick Help comments to your code to clarify usage for other developers.
To add Quick Help to a symbol in your code, hover over the symbol declaration, Command-click it and choose Add Documentation. Xcode adds lines of comments, preceded with ///, that have placeholders for a description, parameters, throws, and a return value, depending on the symbol declaration. Update the placeholders using Markup syntax to complete the comments and enable Quick Help for the symbol.
/// Returns an item that implements `FruitDisplayProtocol`, or throws an error if the `row` of the `indexPath` parameter is out of bounds of `fruitList.`
/// - Parameter indexPath: An instance of `IndexPath` whose `row` indicates which fruit to return.
/// - Throws: If the `indexPath.row` is outside the bounds of the `fruitList` array, throws a `FruitListRangeError.indexOutOfRange` error with a message indicating the indexPath that is out of range.
/// - Returns: An object that implements `FruitDisplayProtocol`.
func fruit(at indexPath: IndexPath) throws -> FruitDisplayProtocol {
guard indexPath.row >= fruitList.startIndex && indexPath.row < fruitList.endIndex else {
throw FruitListRangeError.indexOutOfRange("Fruit not found for index path: \(indexPath)")
}
return fruitList[indexPath.row]
}
Xcode formats and displays the information as Quick Help when you view it.
Xcode also formats Quick Help from multiline comments that begin with /**
and end with */
. For more information about the Markup syntax for Quick Help, see Markup Formatting Reference.