将自定义CGRect函数从Objective-C转换为Swift时出现问题
我一直在反复尝试将自定义的将自定义CGRect函数从Objective-C转换为Swift时出现问题,swift,swift2,xcode7,cgrect,Swift,Swift2,Xcode7,Cgrect,我一直在反复尝试将自定义的CGRect函数从Objective-C转换为Swift 我会取得一些小的进步,但最终我总是会陷入困境。以下是Objective-C中的工作函数: CGRect CGRectSmallestWithCGPoints(NSMutableArray *pointsArray, int numberOfPoints) { NSValue *firstValue = pointsArray[0]; CGFloat greatestXValue = [firstValue C
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;
}
错误从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,我在某些方面遇到了困难,比如一些可选的概念,但在此之前很久没有遇到过困难
我正在将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: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
对于(变量i=1;i
我希望这对您有所帮助。更实用的方法:
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
}
这个很好用,谢谢!不过为了清楚起见,当我们第一次定义smallestYValue时,应该使用CGPoint的y值,而不是x。不用担心,我想我会提到。再次感谢!
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
}