-3

I am new to programming with Swift (with a Java background) and I am trying to initialise a CGFloat using the init() method. I also need a reference to CGFloat after it is initialised. I keep trying this like I'd do in Java:

CGFloat _cgFloat = CGFloat.init(10);

Quite evidently as the IDE indicates, this is an error. I don't seem to understand how to work around this. What am I doing wrong ? Isn't there a way to make this work like in Java:

Object a = new Object();

A solution to this would be most appreciated ! Thanks in advance.

user1841702
  • 2,683
  • 6
  • 35
  • 53
  • 3
    Drop the init, simply use `CGFloat(10)` - and no need for the `;` in the end – luk2302 May 16 '16 at 10:31
  • 1
    `CGFloat _cgFloat =` causes the syntax error, not the `CGFloat.init(10);`. – Note also that `CGFloat` is a *value type* and not an "object". – Martin R May 16 '16 at 10:40

1 Answers1

4

init methods are written like this in Swift:

let newFloat = CGFloat(10)
Mundi
  • 79,884
  • 17
  • 117
  • 140
  • and just one other thing, so wherever I see an init(..) that's actually a constructor and I need to use the method without init () but retain the rest of the signature in parenthesis preceded by the class's name? Hope I got that right ... – user1841702 May 16 '16 at 10:35
  • 2
    You can write also .init in Swift, during code development in Xcode, if you type init Xcode give to you a code auto-completion, it's more useful.. – Alessandro Ornano May 16 '16 at 10:38
  • 1
    I would not recommend using the `init` keyword. It just makes for more clutter in your code. If you want Xcode autocompletion, start typing the class followed by an opening parenthesis (in this case `CGFloat(`) and you will see the possible completions. – Mundi May 16 '16 at 13:34