swift2 将自定义CGRect函数从Objective-C转换为Swift时出现问题

xiozqbni  于 2022-11-06  发布在  Swift
关注(0)|答案(2)|浏览(185)

我一直在反复尝试将一个自定义的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测试版与Swift 2,真的很感谢您的帮助,谢谢。

qlfbtfca

qlfbtfca1#

您的代码中有一些错误,您必须使用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
}

我希望这对你有帮助。

41ik7eoe

41ik7eoe2#

更实用的方法:

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
}

相关问题