-2

so i am just starting to teach myself a bit ios and am an absolute amateur (please dont be too harsh and forgive questions, of which to you the answers might be obvious). so i wanted to get this following code to work:

var int bildBreite;
var float bildHoehe, bildFlaeche;
var bildBreite = 8;
var bildHoehe = 4.5;
var bildFlaeche = bildBreite * bildHoehe;

but get the mistakes as seen on this screenshot here (link: http://prntscr.com/6cdkvv )

concerning the screenshot: do not be confused with the "6" on the very right of line 10 , this was because before the "4.5" it was saying "6".

also, this is a xcode playground and the code i am trying out is from an objective c beginners guide, if that helps. (this whole swift and objective c mix is too confusing)

i would really appreciate an answer and if you have more questions to figure it out properly, i am happy to answer as quick as possible.

all the best

leo

Gordon
  • 147
  • 2
  • 14
  • 1
    Did you download the Swift book? You are missing some very fundamental fundamentals of the language. And since you use German variable names, you might as well do it properly and write boldHöhe and bildFläche. – gnasher729 Mar 03 '15 at 22:45
  • No i downloaded a German objective guide called "Become an Xcoder - Programmieren mit dem Mac in Objective-C". I thought learning objective-c first and then switching to Swift would be the best way to do as Swift is still relatively new. Would you suggest me learning Swift directly? – Gordon Mar 04 '15 at 07:45
  • At least you should get a book on Swift to use when in the Swift playground, because it doesn't accept objective-c... – holroy Mar 07 '15 at 09:12

3 Answers3

2

Let's go over the code:

var int bildBreite;
var float bildHoehe, bildFlaeche;

You are trying to set the types of some variables in a non-Swift way. In order to declare variables of a specific type, you must add the type after the variable name: var bildBreite: Int Also note that class and struct names in Swift start with a capital letter, as @blacksquare noted.

var bildBreite = 8;
var bildHoehe = 4.5;

Here, it looks like you are trying to re-declare the variables. This isn't allowed.

var bildFlaeche = bildBreite * bildHoehe;

You are trying to multiply data between two different number types. This is not allowed in Swift. In order to actually do any math work, you would need to convert one to the same type as the other.

This version should work:

var bildBreite: Int;
var bildHoehe: Float;
var bildFlaeche: Float;
bildBreite = 8;
bildHoehe = 4.5;
bildFlaeche = Float(bildBreite) * bildHoehe;

The following code is a more concise version of your code:

// Inferred to be an Int
var bildBreite = 8
// This forces it to a Float, otherwise it would be a Double
var bildHoehe: Float = 4.5
// First, we convert bildBreite to a Float, then times it with Float bildHoehe.
// The answer is inferred to be a Float
var bildFlaeche = Float(bildBreite) * bildHoehe
MaddTheSane
  • 2,981
  • 24
  • 27
1

int should be Int and float should be Float. Class and primitive names in Swift always begin with a capital letter.

To declare a variable in Swift, you do this:

var variableName: Int = intValue

Or you can let the compiler decide the type for you:

var variableNameA = 1
var variableNameB = 1.5

If you want to declare a variable and then assign it later you use an optional or an implicitly unwrapped optional:

var variableNameA: Int?
var variableNameB: Int!
variableNameA = 1
variableNameB = 2

var variableNameC = variableNameA! + variableNameB

As you can see from the last example, variableNameA is optional (meaning it can have a null value, so it must be unwrapped before usage using the ! operator; an implicitly unwrapped optional like variableNameB doesn't need to be unwrapped, but it's less safe and should be used with caution.

Note also that you only use var when you first declare a variable.

Good luck!

kellanburket
  • 12,250
  • 3
  • 46
  • 73
  • 1
    And you should use Double unless you have a very good reason not to. – gnasher729 Mar 03 '15 at 22:43
  • You can declare a variable name and a type and not assign to it. You just have to assign a value before you use it. – MaddTheSane Mar 04 '15 at 04:30
  • @gnasher729 where exactly would I write double and just "double" or in brackets? If this sounds stupid, feel free to say that I should just read through a specific book before asking, that's fine, too. – Gordon Mar 04 '15 at 07:58
0
var bildBreite:Int = 8
var bildHoehe:Float = 4.5
var bildFlaeche:Float = bildBreite * bildHoehe

You dont need semi colons. You need to not try to apply java or any other language to this learn the syntax and you will be good

rush2sk8
  • 362
  • 6
  • 16