to begin: setup - amazon s3 › devmountain › www › files › ios › ios...to begin: setup in...

12
To Begin: Setup In this first section we’re going to create our Hello World project. This is going to be a very simple project. Learning Goals: Get a working version of Git on your Mac configured with your personal settings Have a basic understanding of Git commands: init, clone, branch, add, commit, pull and push Understand how to use GitHub to work alone and with others Download Xcode and install Download Xcode from the Mac App Store. You may have already done this in your attempt to learn iOS programming in the past. You can visit the store here (https://itunes.apple.com/us/app/xcode/id497799835?mt=12 ) to pick up the app, or just type “xcode” into the search field at the top of the Mac App Store app. It takes a while to download, so you may end up going on to the next couple steps while you wait. If you don’t have the latest version, go get it—Apple consistently fixes bugs and crashes. Once Xcode is downloaded, you’ll need to use your password to install the command line tools. This will give you Git as well as other important tools. Git Git: the ultimate tool to protect yourself from yourself. I once heard a developer say, “the only tool that never loses data is Git.” Git is the software that manages changes to your files. By tracking these changes, you will be able to recall specific versions of those files later. Using a version control system means that if you break your app, you can hopefully recover easily by reverting to previous versions of the code. Git is a free and open source distributed version control system. “Distributed” means that the change tracking is decentralized so you can continue working and editing a project even without maintaining connection to a common network. There are some upsides and downsides to a distributed tracking system, but you’ll find with iOS development that Git works very well. Git can handle small projects with one developer and large projects with hundreds of developers. You’ll likely want to use git for every project. I can assure you that any employer will expect it. Go ahead and download and install git now. Follow this link (http://sourceforge.net/projects/gitosxinstaller/ ) to download the git installer.

Upload: others

Post on 27-Jun-2020

5 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: To Begin: Setup - Amazon S3 › devmountain › www › files › iOS › ios...To Begin: Setup In this first section we’re going to create our Hello World project. This is going

To Begin: Setup In this first section we’re going to create our Hello World project. This is going to be a very simple project. Learning Goals:

Get a working version of Git on your Mac configured with your personal settings Have a basic understanding of Git commands: init, clone, branch, add, commit, pull and

push Understand how to use GitHub to work alone and with others

Download Xcode and install Download Xcode from the Mac App Store. You may have already done this in your attempt to learn iOS programming in the past. You can visit the store here (https://itunes.apple.com/us/app/xcode/id497799835?mt=12) to pick up the app, or just type “xcode” into the search field at the top of the Mac App Store app. It takes a while to download, so you may end up going on to the next couple steps while you wait. If you don’t have the latest version, go get it—Apple consistently fixes bugs and crashes. Once Xcode is downloaded, you’ll need to use your password to install the command line tools. This will give you Git as well as other important tools.

Git Git: the ultimate tool to protect yourself from yourself. I once heard a developer say, “the only tool that never loses data is Git.” Git is the software that manages changes to your files. By tracking these changes, you will be able to recall specific versions of those files later. Using a version control system means that if you break your app, you can hopefully recover easily by reverting to previous versions of the code. Git is a free and open source distributed version control system. “Distributed” means that the change tracking is decentralized so you can continue working and editing a project even without maintaining connection to a common network. There are some upsides and downsides to a distributed tracking system, but you’ll find with iOS development that Git works very well. Git can handle small projects with one developer and large projects with hundreds of developers. You’ll likely want to use git for every project. I can assure you that any employer will expect it. Go ahead and download and install git now. Follow this link (http://sourceforge.net/projects/git­osx­installer/) to download the git installer.

Page 2: To Begin: Setup - Amazon S3 › devmountain › www › files › iOS › ios...To Begin: Setup In this first section we’re going to create our Hello World project. This is going

Get started with Terminal When we use the term “command line,” we’re talking about giving instructions to your computer with keyboard­typed commands. Think about common tasks you do on your computer with the filesystem: move folders, copy and paste files, create directories, and so on. You can actually do any of these actions using the command line; that is, you can tell your computer with text commands to do things like copy files or create directories. Each Mac computer comes pre­installed with a powerful command line program, Terminal. You can find and open terminal by going to Applications>Utilities. Upon first open, you’ll likely see a white window open with some text in it, something like this:

The line in the above image that has the dollar sign ($) in it (TiBook:~/Desktop taylor$) is the prompt line. As “prompt” sort of implies, the system is waiting for your input. It’s waiting to be told what to do. It’s also important to note that the command line (Terminal) operates in a specific place on your file system. For example, if you wanted to copy a file, you’d want to make sure that you tell the system the location of the file you want to copy. Each time you start Terminal, you are taken to a default location in the file system. It’s very important to remember where you are at (or check) in the file system to ensure that you’re actually doing what you think you’re doing. Practice typing in a couple of basic commands:

ls (this will list the contents of the current directory you are in) pwd (this will tell you where you are in the file system)

Page 3: To Begin: Setup - Amazon S3 › devmountain › www › files › iOS › ios...To Begin: Setup In this first section we’re going to create our Hello World project. This is going

Sometimes commands require that you send more information. For example, cd is the command to change directories, or move your command line session to a different location. Simply typing in “cd” won’t do much for you–it’s required that you tell the system where you’re wanting to go.

cd /Users/myname/Documents (this will take us to your Documents directory (assuming your username is “myname”) cd ../ (this will move up a directory, ../ is the symbol for “up one directory”) cd ~ (this will take you to your “home” directory. This usually looks like /Users/myname)

Before going on, try using some of these commands: mkdir (makes a new directory) rm (deletes a file, be careful!)

You can also search for more help on basic command line usage on the web. This video (https://www.youtube.com/watch?v=Ms5sNYyejEw) is a pretty good resource.

Learning the git commands Now, use the command “cd” to go to a place where you’ll create your first git repository. This could be in your home directory, or in a new directory you’ve created called “DevMountain,” or anywhere you’d like. It should probably not be in a directory where there are already lots of other files and other directories, let’s go to an empty directory (or create one using mkdir). Now that we’re in the directory we want to create a Git repository in, let’s get going:

git init

This initializes a Git repository in the folder to which you’ve navigated.

git add .

This stages all of the files. "Staging" the process of preparing a file for a commit. You can actually specify the files you’d like to stage for a commit on an individual basis; just change the ‘.’ into the name of the file you want to stage. The '.' means you're staging all changed files for a commit. Now let's perform an actual commit:

Page 4: To Begin: Setup - Amazon S3 › devmountain › www › files › iOS › ios...To Begin: Setup In this first section we’re going to create our Hello World project. This is going

git commit ­m "message"

The commit message is important. The time you spend detailing what changed in each commit will be invaluable to you at some point very soon. When you write the message for your commit you could use the imperative, present tense: "change", not "changed" or "changes". You could instead use past tense. It’s a matter of preference, but git itself does tell you to use imperative tense. Let's talk about how to link your repository to a remote source.

git remote add origin url.git

The above code will add a new remote source, in this case "origin" to your project. You also specify a url (in the above example it's "url.git") that the remote points to. Setting up remotes will allow you to synchronize your project among multiple computers or developers. It also ensures that if your computer crashes, the code and the repository are still safe somewhere else. (Remember "distributed?") When you create a new repository on GitHub or any other server­side hosting solution, they’ll give you a link to the repo like this one: https://github.com/jkhowland/shiny­ninja.git. You’ll want to find that link and add it to your local repo as the remote origin. git pull origin master

This command pulls or retrieves information from the remote source, in this case the "origin" source and the "master" branch. You would do this anytime you want to update your local repository with changes or commits that have been "pushed" to the remote source. These are only the most basic git commands. You could actually get away with those few commands and have the backup and version control you need. But as you go on working and using git you’ll discover other very important commands like:

git branch tableView git checkout tableview

This creates a separate branch for you to work on (called tableview). You can checkout master again and you can merge the two together.

git checkout master git merge tableview

If you’ve made changes in tableview it will add them in to the local master codebase. Branching using git allows you to move back and forth between different features and bug fixes maintaining

Page 5: To Begin: Setup - Amazon S3 › devmountain › www › files › iOS › ios...To Begin: Setup In this first section we’re going to create our Hello World project. This is going

the code for each of them separately. You can merge them into the master codebase when they’re ready to rock.

git push origin master

This command will push your local commits to the specified remote. Remember, "master" is a branch, and it's generally the branch from which all of the release­able code is deployed. So it's an important place to push updates to or pull updates from. If you've created more branches either remotely or locally, you can pull from or push to those branches by substituting "master" with the branch name you're referring to. If you’re ready to try a few more things, you can try out Code School’s tryGit tutorial (it’s pretty snazzy). DevMountain students will use Git on all of their class projects, and using Git should become second nature. Also, for further reading on a very handy Github feature called Pull Requests, here is (yet another) simple guide.

Create a GitHub account You install git on your computer and it handles versioning for you locally. GitHub is a website that hosts code; think of GitHub as a tool for backup and distribution. It's a super fancy network built on top of the git system. It's important to recognize the difference between git and GitHub. You should not get confused and call git GitHub or vice versa. This article by Andrew McWilliams is pretty spot on:

“At it's core, it's just a place to store your identical working directories ­ aka repositories, or repo's for short. That's the service that Github provides ­ it's literally a hub for Git repositories.” [sic]

Visit GitHub and create a free account. While you’re there, you could also download the GitHub Mac app. The Mac app is very useful for a lot of little features of GitHub (like opening up a pull­request in code on your Mac). Other more powerful programs like Tower are available, but also cost money. The free GitHub account will allow you to create public repositories, fork public repositories, and fork private repositories to which you are granted access. Think of forking as making a copy of someone else's repository so you can make changes and improvements without messing them up. You can even submit pull requests if you find and fix bugs on other developer’s projects. In order to create private repositories on GitHub, you’ll need to pay for a subscription.

Page 6: To Begin: Setup - Amazon S3 › devmountain › www › files › iOS › ios...To Begin: Setup In this first section we’re going to create our Hello World project. This is going

Create a public repository on GitHub Once you’re logged in to GitHub, you can create your first public repository. Click on the plus button at the top of the GitHub homepage and select “New repository”.

You’ll want to give your repo (short for repository) a name. You can edit the description later, and the name doesn’t have to be the name of your actual product. For example, at quite a few of the companies with which I’ve worked we’ve named the products based on Marvel characters, Street Fighters, or (so embarrassing) Lord of the Rings characters. The actual products had branding names, but the repositories were named something fun. Once you’ve created the project, GitHub will give you the command­line snippets you’ll need to get a new repo set up locally. You’ll even notice a green button that lets you use GitHub for Mac to set up the repo with the GitHub Mac app I mentioned earlier. We’re going to create the local repo via Xcode, so we’ll use the second example a bit later.

Page 7: To Begin: Setup - Amazon S3 › devmountain › www › files › iOS › ios...To Begin: Setup In this first section we’re going to create our Hello World project. This is going

Create an Xcode Project Hopefully by now you have Xcode installed. It can take a while if you don’t have a speedy internet connection. Open it up and click on the “Create a new Xcode project” on the left:

Eventually, we’ll create most of the project templates they give you here. For now, we’ll start with a simple “Single View Application”. I’m going to explain a couple of things in this “new project” window:

Page 8: To Begin: Setup - Amazon S3 › devmountain › www › files › iOS › ios...To Begin: Setup In this first section we’re going to create our Hello World project. This is going

Give the project a product name like “pre­course­git.” I try to keep the Product Name something command­line friendly. That means no spaces, and all lowercase. This isn’t the display name for your app anywhere. It’s just how your project file will be referenced. Spaces and capitalization can get in your way quickly. Feel free to leave the Organization blank if you’re not building the project for an organization. The Organization Name will be used every time you create a file. It will show: // Copyright (c) 2014 <Organization Name>. All rights reserved. The Bundle Identifier is a mystery to most new developers. Apple suggests you use reverse­DNS format. For example: if you company’s domain is LearnStack.com and you create an app named Word Search, you could assign the string com.learnstack.word­search as your app’s Bundle ID. Note: Bundle IDs are case sensitive. They are used in Xcode, iTunesConnect (app submission), iCloud (sandbox names) and as the app ID in your member center for provisioning profiles (so you can install the app on test devices). The Class Prefix can be very useful if you use it wisely. You’ll notice that Apple uses class prefixes to show which library the code comes from: UI (UIKit), NS (Foundation), GK (GameKit),

Page 9: To Begin: Setup - Amazon S3 › devmountain › www › files › iOS › ios...To Begin: Setup In this first section we’re going to create our Hello World project. This is going

SK (StoreKit). You can make it obvious to anyone using your project what files are third party, or Apple libraries and which files are the code for your app specifically. We’ll go into this in more depth in the first week of the course. Hit next and you’ll select the location of the project:

I will explain how I organize my projects, but you can honestly do whatever you want—everyone has their own organizational style. All code on my Mac resides in one folder called "Code." Within that folder, there are a few types of repositories: project code, banked code, and archived code. Any projects that I’m actively working on are in the project folder. Any code I’ve written, or a third party library I want to save for other projects is put in the Code Bank folder. Projects I’m not actively working on are in the archive folder. One of the options is to create the repo on My Mac or to create it on a server. Just select My Mac for now and hit “Create.”

Page 10: To Begin: Setup - Amazon S3 › devmountain › www › files › iOS › ios...To Begin: Setup In this first section we’re going to create our Hello World project. This is going

Add the remote to Xcode’s project Adding the GitHub remote to your existing project is relatively simple. You need to navigate to the project in the command­line:

cd code/pre­course­git (or wherever you stored it) Then use the commands from the repo’s homepage on GitHub:

git remote add origin https://github.com/jkhowland/shiny­ninja.git git push ­u origin master

Push to GitHub Now it’s all easy. From the command­line you can push:

git push origin master Now your initial commit that was generated by Xcode is up on GitHub

Edit the project’s storyboard Let’s add a UILabel to the app’s single view. In Xcode, select the Main.storyboard file on the left and make sure that View is selected. On the top toolbar make sure the right­most button is selected (blue) which will open the inspector on the right. In the bottom section of the inspector you’ll want to have the cube selected. Then in the search field at the very bottom type in “UILabel”. You can click and drag the UILabel from the list onto the UIView, and edit all of the attributes of the label including the text and size.

Page 11: To Begin: Setup - Amazon S3 › devmountain › www › files › iOS › ios...To Begin: Setup In this first section we’re going to create our Hello World project. This is going

Commit your changes Before you commit you’ll always want to build and run to make sure everything is working as you expected when you wrote the code (or changed the interface file). If you have tests you’ll want to run your tests as well. Go back to the Terminal app, and stage all of the changes you just made:

git add .

Then you’ll want to commit the changes so that you can push them up to GitHub.

git commit ­m "Adds a label to the main view" You’ll always want to keep your commits small and modular. When you run into bugs you’ll want to roll back to older commits. If each commit is small then it will be easier to peel back the layers until you can find the cause of the problem. If your commits are small and modular (meaning they apply only to a specific unit of the app) you can read what a commit does: ‘Oh! This one adds a label to the main view. So it couldn’t possibly be causing any network problems.’

Page 12: To Begin: Setup - Amazon S3 › devmountain › www › files › iOS › ios...To Begin: Setup In this first section we’re going to create our Hello World project. This is going

Push to GitHub Now get your code up on the remote server.

git push This is the most simple git process: code, stage, commit, push. You’re really going to want to get deeper into Git. Learn how to create and use branches. Learn how to review the change history and checkout a specific commit. Learn how to rebase your code with the latest on a remote repository. Learn how to submit a pull request on GitHub. And then learn even more. If you’re going to be a professional developer take some time to get to know Git really well. In 1­4 hours you could really nail down a foundation that will help you for many years. Here are a few resources that are awesome: Git Immersion: http://gitimmersion.com/ The Git Book: http://git­scm.com/book Git Real: http://www.codeschool.com/courses/git­real