Giter VIP home page Giter VIP logo

swift-xcode-warnings-errors-swift-intro-000's Introduction

Xcode: Tests, Warnings, Errors

Overview

In this lesson we'll cover how to work with tests and how to distinguish between warnings and errors in Xcode.

Objectives

  1. Learn how to work with Tests
  2. Learn the distinction between warning and errors and how to navigate to them with the issue navigator.

Tests

NOTE - The test file might look different from the screenshots below (for now). The reason for that is that we updated our curriculum to work with Swift 3.0 and Xcode 8 which is the latest version as of 9/13/16. When the Quick & Nimble framework have been updated to work with Swift 3.0 (which should be soon!), the screenshots should reflect with what is in the Xcode Project and this note will be removed.

Tests

Throughout this course, you will find that some labs (to be completed by you!) include tests. What are they and why are they useful? Tests are made to ensure that you are implementing the functions correctly.

Instructions might look like this:

Create and implement each of the following functions in the ViewController.swift file.

  • Create a function called unicorn() that takes in no arguments but returns a String. The String that should be returned is a bunch of unicorns, to be exact.. it should be four Unicorn emoji's separated by a space.
  • Create a function called loveAgain(_:) that takes in a String as an argument which represents an individuals name. This function will also return a String. It should take in the individual's name and add a heart emoji to the end of it and return that String.

Your workflow for solving labs will require that you read through instructions just like the ones above and create those functions.

Instead of pressing (command + r) to run your app, you should press (command + u). This will instead run the tests.

If you implemented these functions correctly, this is what you should see.

correct

Notice the green check mark to the left of line 13, it means we implemented both our functions correctly. Let's now jump to the ViewController.swift file so you can see how we implemented these functions.

answer

We know our test file (ViewControllerTests.swift) is looking for us to implement these functions correctly. We know we run these tests by pressing (command + u), but what happens if we didn't implement these functions correctly. Lets instead of returning back a unicorn emoji, return back the string literal "I hate unicorns". So now our implementation looks like this:

func unicorn() -> String {       
   return "I hate unicorns"
}

Our Xcode project now looks like this:

finish

If we were to now run the tests (by pressing command + u), this is what we would see:

error

It's letting us know that the tests haven't passed, indicated by that red x in the upper left there. But that doesn't help us much in figuring out how our function is not correct? How can we tell?

You can either click the file to the left of that mini red-x which would take you to the ViewControllerTests.swift file or you can just get back to the ViewControllerTests.swift file by clicking the folder icon in the project navigator and locating it.

If we get back to the ViewControllerTests.swift file we should see this (which is very helpful):

helpMe

  • Here, we can see that it's expecting the unicorn() function to return back a String of four unicorn emoji's separated by a space, not the String "I hate unicorns".

  • It will be very helpful for you moving forward to reference the test files and see what it is they're expecting of you.

  • Included in this repository is the Xcode project if you would like to navigate through the files and see how it works.

unicorn

Warnings, Errors

When using Xcode, you're going to encounter warnings and errors. This is a normal part of the development process, so don't be alarmed when you see these symbols: they're there to help you!

Warnings and errors are generated by the pre-compiler (though often referred to as just "the compiler"), which is a tool built into Xcode that continuously scans through the code as it's edited in order to identify problems.

A warning is a minor issue that something unexpected might be caused by the code, but won't necessarily cause a crash at run time. Warnings will not impede the scheme being built.

An error is a major issue that will almost certainly cause a crash at run time, so the error impedes the scheme from being built.

Like all of the tools bundled together in Xcode, the pre-compiler is imperfect. It isn't able to identify every possible problem that might come about, and the messages that accompany these warnings and errors aren't always direct. This often leads Xcode to generate cryptic messages and to suggest incorrect fixes.

That being said, if your watchdog starts barking, investigate. In other words, don't simply ignore warnings and errors that Xcode presents to you. They're indicators that you may have done something undesirable, signs that your program might behave in a way that you don't want it to. So look into them. Keep your code clean from issues. (Eventually you'll begin working with other programmers in groups who shouldn't be burdened with having to sift through the warnings meant for you when working on their own features in the same project.)

Here's an example of the compiler disallowing us from changing the value of a constant. Because favoriteActor is a constant, as it's declared with the let keyword. We shouldn't be allowed to change the value to "Brad". The compiler enforces this rule as you can see below. It is even giving us a suggestion. It's telling us to declare favoriteActor using the var keyword which would make it a variable, thus allowing us to change its value to "Brad".

let favoriteActor = "Leo"
favoriteActor = "Brad"

error

leo

View Xcode: Warnings and Errors on Learn.co and start learning to code for free.

View Testing and Xcode Warnings on Learn.co and start learning to code for free.

swift-xcode-warnings-errors-swift-intro-000's People

Contributors

annjohn avatar bloomtiful avatar jimcampagno avatar markedwardmurray avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.