ios - Divided operation in Swift -


why error on that?

 var rotation:float= double(arc4random_uniform(50))/ double(100-0.2) 

actually try 1 too:

 var rotation:double= double(arc4random_uniform(50))/ double(100-0.2) 

thank you

swift has strict rules whitespace around operators. divide '/' binary operator.

the important rules are:

  • if operator has whitespace around both sides or around neither side, treated binary operator. example, + operator in a+b , + b treated binary operator.
  • if operator has whitespace on left side only, treated prefix unary operator. example, ++ operator in ++b treated prefix unary operator.
  • if operator has whitespace on right side only, treated postfix unary operator. example, ++ operator in a++ b treated postfix unary operator.

that means need add space before / or remove space after indicate binary operator:

var rotation = double(arc4random_uniform(50)) / (100.0 - 0.2) 

if want rotation float, should use instead of double:

var rotation = float(arc4random_uniform(50)) / (100.0 - 0.2) 

there no need specify type explicitly since inferred value assigning to. also, not need explicitly construct literals specific type conform type using them with.


Comments

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

java - Digest auth with Spring Security using javaconfig -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -