Open menu with table of contents iOS Assignment
Logo of Stuttgart Media University for light theme Logo of Stuttgart Media University for dark theme
Mobile Application Development 2

iOS Assignment

Swift Basics

Please conduct the following tasks alone. For implementation details you can refer to the lecture slides or the Apple developer website. Please do not hesitate to ask me or the tutor if you have any questions. Under "Tip" you can find some hints for the task. The "Checklist" specifies what should happen when your implementation is correct.

Stuttgart Media University

1 Tasks

  1. Shopping List
  2. Swift Console Game - NumberGuess

2 1. Shopping List

Open a Playground and write a program that creates a shoppingList with 5 shopping items. Next, iterate over the shoppingList and output the items to the console. Then remove the item number 4 in the list and print the new number of items in the shoppingList to the console. The output should look like this:

center 50%

2.1 Tip

Use an Array of Strings to store the items in the shoppingList. Iterate over the list using a for-loop.

3 2. Swift Console Game - NumberGuess

Create a new Xcode Project and choose OS X - Application -> Command Line Tool.

If you have problems compiling your application. Check if Swift runtime tools are missing. You can download them here.

center 50%

Name your project „NumberGuess“ and select „Swift“ as the Language.

Now write a game that asks the user:


„Please think about a number between 0 and 1000“
„Is the number 500 (g)reater or (s)maller than your number or is it (c)orrect?“

To read input from the command line use the following code:


textInput = readLine()!

At the beginning of the game, the number 0 is stored in a variable „min“. The number 1000 is stored in a variable „max“. The number 500 in this case is stored in a variable called „currentValue“ and is calculated as follows:


currentValue = min + (max - min) / 2

When the „readLine()“ function is called, the user can enter text in the console. The user should now enter „g“, „s“ or „c“ in the console correspondingly, until the number is correct. If the user enters another letter than „g“,“s“ or „c“, the program should output „Please only type g,s or c“

If the user enters „g“, the value of „max“ is set to „currentValue“, i.e.:


max = currentValue

In the case of „s“ the value of „min“ is set to „currentValue“, i.e.:

min = currentValue

When the user enters „c“ the program prints to the console how many tries were needed to guess the number.

The following shows an example game output:

center 50%

3.1 Tip

Use a variable „tries“ to count the tries. Use a while-loop as „game-loop“ and a boolean „finished“ to check if the user pressed „c“. Use a switch statement to check what the user has entered. Use println() to output text and input() to read text from the console