gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 06:59:15 24 4
gpt4 key购买 nike

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

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com