无法将自定义 CGRect 函数从 Objective-C 转换为 Swift

Trouble converting custom CGRect function from Objective-C to Swift

我一直在尝试将自定义 CGRect 函数从 Objective-C 转换为 Swift

我会取得一些小的进步,但最终总是会卡住。这是Objective-C中的工作函数:

CGRect CGRectSmallestWithCGPoints(NSMutableArray *pointsArray, int numberOfPoints) {

NSValue *firstValue = pointsArray[0];

CGFloat greatestXValue = [firstValue CGPointValue].x;
CGFloat greatestYValue = [firstValue CGPointValue].y;
CGFloat smallestXValue = [firstValue CGPointValue].x;
CGFloat smallestYValue = [firstValue CGPointValue].y;

for(int i = 1; i < numberOfPoints; i++) {
    NSValue *value = pointsArray[i];

    CGPoint point = [value CGPointValue];
    greatestXValue = MAX(greatestXValue, point.x);
    greatestYValue = MAX(greatestYValue, point.y);
    smallestXValue = MIN(smallestXValue, point.x);
    smallestYValue = MIN(smallestYValue, point.y);
}

CGRect rect;

rect.origin = CGPointMake(smallestXValue, smallestYValue);
rect.size.width = greatestXValue - smallestXValue;
rect.size.height = greatestYValue - smallestYValue;

  return rect;
}

这是我目前进行 Swift 转换的位置:

func CGRectSmallestWithCGPoints(pointsArray: NSArray, numberOfPoints: Int) -> CGRect {

    var greatestXValue = pointsArray[0].x
    var greatestYValue = pointsArray[0].y
    var smallestXValue = pointsArray[0].x
    var smallestYValue = pointsArray[0].y

    for(var i = 1; i < numberOfPoints; i++)
    {
        let point = pointsArray[i];

        greatestXValue = max(greatestXValue, point.x);
        greatestYValue = max(greatestYValue, point.y);
        smallestXValue = min(smallestXValue, point.x);
        smallestYValue = min(smallestYValue, point.y);
    }

    var rect = CGRect()
    rect.origin = CGPointMake(smallestXValue, smallestYValue);
    rect.size.width = greatestXValue - smallestXValue;
    rect.size.height = greatestYValue - smallestYValue;

    return rect;
}

错误始于 for 循环。当我尝试使用 max 和 min 时,出现以下错误:

Cannot assign a value of type 'CLHeadingComponentValue' (aka 'Double') to a value of type 'CLHeadingComponentValue!'

然后在 for 循环之后,当我修改 rect 值时,它给了我一个类似的错误:

Cannot assign a value of type 'CLHeadingComponentValue' (aka 'Double') to a value of type 'CGFloat'

我无法理解为什么这种转换看起来如此困难。在过去的几周里,我一直在断断续续地使用 Swift 并且我已经陷入某些事情,比如一些 optional 概念,但以前从来没有坚持这么久。

我正在使用 Xcode 7 beta 和 Swift 2,非常感谢您的帮助。

你的代码有一些错误,你必须使用 CGPointValue 属性 而不是 x 属性 直接看固定代码:

func CGRectSmallestWithCGPoints(pointsArray: NSArray, numberOfPoints: Int) -> CGRect {

    var greatestXValue: CGFloat = pointsArray[0].CGPointValue.x
    var greatestYValue: CGFloat = pointsArray[0].CGPointValue.y
    var smallestXValue: CGFloat = pointsArray[0].CGPointValue.x
    var smallestYValue: CGFloat = pointsArray[0].CGPointValue.y

    for(var i = 1; i < numberOfPoints; i++) {

       let point = pointsArray[i].CGPointValue

       greatestXValue = max(greatestXValue, point.x)
       greatestYValue = max(greatestYValue, point.y)
       smallestXValue = min(smallestXValue, point.x)
       smallestYValue = min(smallestYValue, point.y)
    }

    var rect = CGRect()
    rect.origin = CGPointMake(smallestXValue, smallestYValue);
    rect.size.width = greatestXValue - smallestXValue;
    rect.size.height = greatestYValue - smallestYValue;

    return rect
}

希望对你有所帮助。

更实用的方法:

func CGRectSmallestWithCGPoints(pointsArray: [CGPoint]) -> CGRect? {
    if pointsArray.isEmpty {
        return nil
    }

    let minX = pointsArray.reduce(CGFloat.max) { min, point in min(min, point.x) }
    let minY = pointsArray.reduce(CGFloat.max) { min, point in min(min, point.y) }
    let maxX = pointsArray.reduce(CGFloat.min) { max, point in max(max, point.x) }
    let maxY = pointsArray.reduce(CGFloat.min) { max, point in max(max, point.y) }

    let rect = CGRect(x: minX, y: minY, width: maxX - minX, height: maxY - minY)
    return rect
}