MIN()和MAX()在Swift中转换为CGFloat

时间:2022-09-28 17:08:56

I'm getting some errors with the following methods:

我用以下的方法犯了一些错误:

1) How do I return screenHeight / cellCount as a CGFLoat for the first method?

1)如何返回屏幕高度/ cellCount作为第一个方法的CGFLoat ?

2) How do I use the equivalent of ObjC's MIN() and MAX() in the second method?

2)如何在第二个方法中使用ObjC的MIN()和MAX()的等效项?

func tableView(tableView: UITableView!, heightForRowAtIndexPath indexPath: NSIndexPath!) -> CGFloat {
    var cellCount = Int(self.tableView.numberOfRowsInSection(indexPath.section))

    return screenHeight / cellCount as CGFloat
}

// #pragma mark - UIScrollViewDelegate

func scrollViewDidScroll(scrollView: UIScrollView) {
    let height = CGFloat(scrollView.bounds.size.height)
    let position = CGFloat(MAX(scrollView.contentOffset.y, 0.0))

    let percent = CGFloat(MIN(position / height, 1.0))
    blurredImageView.alpha = percent
}

4 个解决方案

#1


66  

1: You can't downcast from Int to CGFloat. You have to initialize a CGFloat with the Int as input.

1:你不能从Int调到CGFloat。您必须用Int作为输入来初始化一个CGFloat。

return CGFloat(screenHeight) / CGFloat(cellCount)

2: Use the min and max functions defined by the standard library. They're defined as follows:

2:使用标准库定义的最小和最大函数。它们的定义如下:

func min<T : Comparable>(x: T, y: T, rest: T...) -> T
func max<T : Comparable>(x: T, y: T, rest: T...) -> T

Usage is as follows.

使用如下。

let lower = min(17, 42) // 17
let upper = max(17, 42) // 42

#2


7  

If you're using Swift 3, max() and min() are now called on the sequence (i.e., collection) instead of passing in arguments:

如果您正在使用Swift 3,那么现在对序列调用max()和min()。,而不是传递参数:

let heights = [5, 6] let max = heights.max() // -> 6 let min = heights.min() // -> 5

设高度=[5,6]设max = height.max() // -> 6设min = height.min () / -> 5

#3


3  

You need to explicitly convert cellCount to CGFloat, since Swift doesn't do automatic type conversion between integers and floats:

您需要显式地将cellCount转换为CGFloat,因为Swift并不在整数和float之间进行自动类型转换:

return screenHeight / CGFloat(cellCount)

min and max functions are defined by the standard library.

最小和最大函数由标准库定义。

#4


2  

You can just use min() and max() - they're built-in.

您可以只使用min()和max()——它们是内置的。

If you wanted to roll your own (why? - maybe to extend it) you would use something like

如果你想自己卷(为什么?-可以扩展它)你可以用类似的东西

func myMin <T : Comparable> (a: T, b: T) -> T {
    if a > b {
        return b
    }
    return a
}

#1


66  

1: You can't downcast from Int to CGFloat. You have to initialize a CGFloat with the Int as input.

1:你不能从Int调到CGFloat。您必须用Int作为输入来初始化一个CGFloat。

return CGFloat(screenHeight) / CGFloat(cellCount)

2: Use the min and max functions defined by the standard library. They're defined as follows:

2:使用标准库定义的最小和最大函数。它们的定义如下:

func min<T : Comparable>(x: T, y: T, rest: T...) -> T
func max<T : Comparable>(x: T, y: T, rest: T...) -> T

Usage is as follows.

使用如下。

let lower = min(17, 42) // 17
let upper = max(17, 42) // 42

#2


7  

If you're using Swift 3, max() and min() are now called on the sequence (i.e., collection) instead of passing in arguments:

如果您正在使用Swift 3,那么现在对序列调用max()和min()。,而不是传递参数:

let heights = [5, 6] let max = heights.max() // -> 6 let min = heights.min() // -> 5

设高度=[5,6]设max = height.max() // -> 6设min = height.min () / -> 5

#3


3  

You need to explicitly convert cellCount to CGFloat, since Swift doesn't do automatic type conversion between integers and floats:

您需要显式地将cellCount转换为CGFloat,因为Swift并不在整数和float之间进行自动类型转换:

return screenHeight / CGFloat(cellCount)

min and max functions are defined by the standard library.

最小和最大函数由标准库定义。

#4


2  

You can just use min() and max() - they're built-in.

您可以只使用min()和max()——它们是内置的。

If you wanted to roll your own (why? - maybe to extend it) you would use something like

如果你想自己卷(为什么?-可以扩展它)你可以用类似的东西

func myMin <T : Comparable> (a: T, b: T) -> T {
    if a > b {
        return b
    }
    return a
}