C. Keith Ray

C. Keith Ray writes about and develops software in multiple platforms and languages, including iOS® and Macintosh®.
Keith's Résumé (pdf)

Thursday, January 15, 2015

Swift - initializing CGFloat values

import UIKit

var red: Int = 12

var newRed1 = red/255
// newRed1 is Int with value 0

// var newRed2 = red/255.0
// Error: Cannot invoke '/' with an argument list of type '(@lvalue Int,FloatLiteralConvertible)'

var newRed3 = CGFloat(red)/255.0
// newRed3 is CGFloat with value 0.0470588235294118

var newRed4 = CGFloat(red)/255
// newRed4 is CGFloat with value 0.0470588235294118

var newRed5 = CGFloat(red/255)
// newRed5 is CGFloat with value 0.0
// This one is surprising:
// var newRed6 = CGFloat(red/255.0)
// Error: "Could not find an overload for 'init' that accepts the supplied arguments"


No comments:

Post a Comment