swift - 将自定义 CGRect 函数从 Objective-C 转换为 Swift 时遇到问题

标签 swift swift2 xcode7 cgrect

我一直在尝试将自定义 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
}

希望对你有帮助

关于swift - 将自定义 CGRect 函数从 Objective-C 转换为 Swift 时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32469622/

相关文章:

swift - 自定义 UICollectionViewCell 类识别但不工作

iOS 和 Swift 使用 Twitter 分享

ios - ReplayKit 导致 iOS 8 手机上的应用程序崩溃

xcode - 是否可以删除 macOS Xcode CoreSimulator devices 文件夹?

ios - 在Xcode7.0中手动安装模拟器SDK 8.0

ios - 无法将类型 '__NSTaggedDate' 的值转换为 'NSNumber'

ios - UIButton 图像即使有约束也会自动调整大小

swift2 - 在 Swift 2 中使用 NSSerialization.datawithJSON

xcode - 快速重载初始化()

ios - 为什么我的应用程序没有列出我的设置?