An introduction to the Swift programming language. This will prepare you for more extensive iOS app development and build a foundation for advanced iOS development topics.
Upon completing this course, you will be able to:
1. Associate the relationship of Swift and Objective-C and their use in iOS and Mac (OS X) programming
2. Develop the ability to read and write Swift code
3. Distinguish how both programming languages can be used together in applications
4. Demonstrate how to write applications entirely in Swift with the help from several iOS programming samples
5. Determine how Swift can be used for development on new platforms such as Apple Watch and Apple TV
6. Create a working client-server iPhone application with location services that you can add to your portfolio.

审阅

SK

very good course. And trainers are expert in thier field. Thankyou. very nice experience

LD

May 02, 2016

Filled StarFilled StarFilled StarFilled StarFilled Star

would love to have this course in 2 sessions, instead of one,

从本节课中

Introduction to Swift

In this first week, you will get a first look at the programming environment and the Swift programming language. If you have taken the “Foundations of Objective-C App Development” Course in this Specialization, some of this content may be familiar. Students who have not taken that course - no need to worry - you will receive a full introduction to become familiar with programming for iOS or Mac OS X as if Swift is your first programming language.

教学方

Justin-Nicholas Toyama

Frank Ayars

Ralf Brockhaus

脚本

[MUSIC] Let's take a look at functions that return a value or perhaps multiple values. A function may return a value in order to tell the caller usually whether it was successful and performing its task or returning some value maybe this whole purpose of the function. For example, we see it's a lot with math functions, right? We want to add a plus b together and return c to whoever called the function. All right, so let's look at some functions that have a return type. That's another way of saying that a function returns a value. Look for syntax denoting that a function returns a value and the type of the value. And also look for syntax denoting that a function returns multiple values and how to access those multiple values. Also be sure to look for syntax denoting that a function might not produce a value, in other words it might return nill to us. This is different than a function that doesn't have a return type, right? So don't be confused. A function can still have a return type, but not return a value. In other words, it would return nill to us, so look out for that difference. And when you work on your own, try omitting the return, and I'll show you how to return something out of a function in the play ground, but try omitting the line of code that says return something. On a function that does expect to return type and see what happens. You'll get an error and that is the compiler helping you to complete the implementation of your function based on the function signature. Okay. Let's go to the playground. So let's look at these functions that have a return type. Now up here we have some come code that was calculating the number of moons. And we rewrote that code over and over and over again, right? So that looked like this. If Let numberOfMoons = planet.count. And if numberOfMoons = > 0, do this, otherwise do that, etc. We kept on writing that code over and over again. So in a real implementation, it's wasteful to keep on retyping that code. And it's in fact could be actually dangerous to constantly write that code over and over again because you may want to change the behavior, change the implementation of that calculation, how it's calculated. But if you have it typed up in multiple locations in your project or in your code file your single class file what have you. If you change it one place you have to make the decision if you need to change it in the other place. It would be safer to have one function encapsulate that calculation and then make different objecst call that one function. That way if you have to change how that calculation is made you just do it once and everybody gets the benefits of that. So let's now create a function that will give us back the number of moons of a planet. Look at the syntax here. This function will return a value to its caller. Functions with return types are declared with an arrow, which is a hyphen and then a greater than sign, right? So this makes the arrow. And after the arrow we have the expected type of the return value. So in this case, we're going to return an int. Now keep in mind that a planet might not have any moons in which case the ends may be zero. But in our dictionary the way that they're structured we just wouldn't have that key value pair, so we wouldn't be able to even find anything about the moons and will return nil. So that's why this is declared as an optional int, all right? So it says we may return an int, but we may also return nil. That's different then if we just didn't have a return type here, right? Okay so let's look at this inside if this function. Var number of moons of type an option int declaring a variable as an optional type will produce a nil. And so if we don't find the value at the key moons inside of our planet dictionary, the number of moons will remain nil by the time that we return it which is what we do down here. Return number of moons. So again, that's another reason why we've decided to make the return type be an optional int. Okay, this part of the code we should be familiar with, right? We look in the planet p, look in the dictionary for the key moons, and grab that value out, we use optional binding. To assign that to the moonsArray temporary variable. And now we can just say numberOfMoons = moonsArray.count, so now that we've set moonsArray if we didn't have some moons, we return that number on the last line of code in the function here. So we have the return keyword, and followed by the value we're trying to return. This works out because number of moons was declared to be optional int, and the expected return type is optional int. So the signature up here of the return type. Matters if the value you return is not of the same type, you'll get a compiler error and you won't be able to build. [MUSIC]