[英]Cannot use “floor” in swift
var viewSize = Double(viewersView.frame.size.width)
var itemSize = Double(boxSize + viewerHorizontalPadding)
self.maxViewers = floor(viewSize / itemSize) //should be Int
I get an error that says:我收到一条错误消息:
No "floor" candidates produce expected contextual result type "Int"没有“地板”候选人产生预期的上下文结果类型“Int”
I imported Darwin.我进口了达尔文。
floor
takes a Double and returns another Double. floor
接受一个 Double 并返回另一个 Double。 If you want it to be an Int (to match self.maxViewers
, you must convert it explicitly: Int(floor(viewSize / itemSize))
.如果你希望它是一个 Int(为了匹配self.maxViewers
,你必须显式转换它: Int(floor(viewSize / itemSize))
。
Use round(_:)
method or rounded(_:)
使用round(_:)
方法或rounded(_:)
round(_:)
]: Rounds the value to an integral value using the specified rounding rule.round(_:)
]:使用指定的舍入规则将值舍入为整数值。rounded(_:)
: Returns this value rounded to an integral value using the specified rounding rulerounded(_:)
:使用指定的舍入规则将此值舍入为整数值
var w1 = 6.5
w1.round()
// w1 == 7.0
// Equivalent to the C 'round' function:
var w = 6.5
w.round(.toNearestOrAwayFromZero)
// w == 7.0
// Equivalent to the C 'trunc' function:
var x = 6.5
x.round(.towardZero)
// x == 6.0
// Equivalent to the C 'ceil' function:
var y = 6.5
y.round(.up)
// y == 7.0
// Equivalent to the C 'floor' function:
var z = 6.5
z.round(.down)
// z == 6.0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.