Join Todd Perkins for an in-depth discussion in this video Update for Xcode 7 and Swift 2, part of Cocoa with Swift Essential Training.
- [Voiceover] At the time of the releasing of this course, the current version of Xcode was version 6 and current version of Swift was Swift 1. Since that time, Xcode 7 and Swift 2 have both been released and some of the code in this course has subsequently changed. Now since the changes are minor, I haven't changed the movies in the course. I'm just going to show you the changes in this movie and you could implement them when you see any warnings on your screen.
The changes however have been implemented in the exercise files for this course. So here are some important differences that have changed since Xcode 7 and Swift 2. The most common change you're going to see in this course is that the print line function has been changed to print. So the L-N are removed. Now, you're going to see that whenever you type print line as shown in the course and just use print instead of print line and you'll be fine.
Swift 2 wants you to use let instead of var if the variable is not mutated. So if you define it and you don't change it to some other value later on, then use let instead of var. So if the course tells you to write var and the compiler won't compile it, just change that var to let. The as command for type casting has been changed to add an exclamation point.
The exclamation point denotes a forced cast which says it could potentially fail. For example, you can't cast every string as a number. It might fail, so that's why we add an exclamation point in Swift 2. Some method signatures like the delegate methods for app delegate and the table view have changed to remove the exclamation point and question mark characters. As a result, some code has been updated.
I'm going to show you an example of that. Here I have some code that runs a table view. On line 19 in the code, it says table column dot identifier and there is an exclamation point here. This is something that had to be added because the method signature for table view, object value for table column has changed. So table column is now optional as denoted with a question mark, so I had to add this exclamation point to unwrap it.
Now again, when you're typing out this code in the movie, you're going to see a warning message over here that'll tell you to insert the exclamation point. So just follow that and you'll be fine. The NSDate date method is now just the NSDate so it's removed that dot date call and it's just the NSDate constructor. That's called once of twice in the course. Another method that's been changed is NSURL's class method URL with string. It's now changed to an overloaded constructor so it's NSURL and then the string is passed in after the colon, after the word string within the parenthesis.
I'm going to show an example of that in some code that's not written in the course, but it's shown in the course. I'm going to show you a difference there. So here I have some highlighted code, and this is the NSURL constructor with a string overload where you pass in the string URL. Notice that I've also added an exclamation point to force unwrap it. Now here are some other changes that aren't quite crucial but I just wanted to let you know that they were changes that I made to the exercise files.
I've added the NSApplicationMain callout in AppDelegate Swift in every file. Now this gives a starting point for your application to run. It's the same thing that the main.swift file did in Swift 1. So I had to delete that file accordingly. In adding this callout in every AppDelegate file, I didn't change the line numbers as you're going to see in your code so the line numbers in your code should still match what you see in the course. I've also updated to the recommended build settings for Xcode 7 and Swift 2 and I've moved the user interface windows in the size inspector to the top left corner.
Now, you likely probably weren't even seeing that issue anyway but since I record on a lower resolution on my computer, I was getting a warning that the object was going to be rendered off screen when I ran the application. So I just moved them over. I'll show you what that looks like here. So in the MainMenu.zip file, I have my window selected and I'll head over to the utility area. And in here, you can see this window is at the top left. So I just moved it so that it will fit in my screen and if I move it somewhere else then I get a warning.
So I put them at the top left here. Now again, this is not something that you're probably even going to notice in going through this course. I just moved it so that the exercise files wouldn't have that warning. And finally, I left some harmless warnings in. Example, an unused variable should be deleted. I left those in so that the code in the exercise files was consistent with the code in the movies. So I tried to keep it as consistent with the code in the movies as much as possible and the only changes that I made were the changes that we've discussed in this movie.
So using these changes we just discussed, you should be able to get through the course using Xcode 7 and Swift 2.
- Creating your first Cocoa app
- Understanding the relationship between Cocoa and Swift
- Creating a playground
- Working with variables, functions, arrays, and loops
- Creating basic interactions and quick connections
- Building custom controller classes
- Using delegates
- Creating and arranging interface elements
- Using data controls
- Debugging and troubleshooting
- Distributing a Cocoa application
Skill Level Beginner
Q: This course was updated on 5/03/2016. What changed?
A: We added one new tutorial explaining how the Swift 2 and Xcode 7 updates affect the training in this course (which was recorded using Swift 1 and Xcode 6). The changes are minor, so Todd provides some short tips for using optionals, coding with NSDate() and NSURL(string:), and working with other smaller changes in the framework.