Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift “(Int,Int)”与“CGPoint”不同_Swift - Fatal编程技术网

Swift “(Int,Int)”与“CGPoint”不同

Swift “(Int,Int)”与“CGPoint”不同,swift,Swift,我得到错误:“Int,Int”与“CGPoint”不相同 如何将Int,Int转换为CGPoint let zigzag = [(100,100), (100,150),(150,150), (150,200)] override func drawRect(rect: CGRect) { // Get the drawing context. let context = UIGraphicsGetCurrentContext() // Create

我得到错误:“Int,Int”与“CGPoint”不相同

如何将Int,Int转换为CGPoint

let zigzag = [(100,100),
    (100,150),(150,150),
    (150,200)]

override func drawRect(rect: CGRect)
{
    // Get the drawing context.
    let context = UIGraphicsGetCurrentContext()

    // Create the shape (a vertical line) in the context.
    CGContextBeginPath(context)

    //Error is here
    CGContextAddLines(context, zigzag, zigzag.count)

    // Configure the drawing environment.
    CGContextSetStrokeColorWithColor(context,UIColor.redColor().CGColor)

    // Request the system to draw.
    CGContextStrokePath(context)
}

如果它告诉你使用CGPoint,就使用它!只是数字,数字是一对整数


如果它告诉你使用CGPoint,就使用它!只是数字,数字是一对整数

CGContextAddLines需要一个CGPoint数组。如果你已经有了 数组的Int,Int元组,然后可以用

let points = zigzag.map { CGPoint(x: $0.0, y: $0.1) }
CGContextAddLines需要一个CGPoint数组。如果你已经有了 数组的Int,Int元组,然后可以用

let points = zigzag.map { CGPoint(x: $0.0, y: $0.1) }

避免创建相同类型实例所需的样板代码的另一种方法是使CGPoint实现arraylteralconvertible,通过分配CGFloat数组使其可初始化:

然后按如下方式使用:

let zigzag:[CGPoint] = [
    [100,100],
    [100,150],
    [150,150],
    [150,200]
]
请注意:

从风格上看,这看起来不太好——如果可以将文本用于元组,那就好了,但我不知道有什么方法可以做到这一点 如果使用空数组,则使用x=0和y=0初始化CGPoint 如果使用具有一个元素的数组,则使用y=0对其进行初始化 如果使用的值超过2个,则忽略第2个值之后的所有值
避免创建相同类型实例所需的样板代码的另一种方法是使CGPoint实现arraylteralconvertible,通过分配CGFloat数组使其可初始化:

然后按如下方式使用:

let zigzag:[CGPoint] = [
    [100,100],
    [100,150],
    [150,150],
    [150,200]
]
请注意:

从风格上看,这看起来不太好——如果可以将文本用于元组,那就好了,但我不知道有什么方法可以做到这一点 如果使用空数组,则使用x=0和y=0初始化CGPoint 如果使用具有一个元素的数组,则使用y=0对其进行初始化 如果使用的值超过2个,则忽略第2个值之后的所有值 还有一个:

func CGPoints(points:(x:CGFloat, y:CGFloat)...) -> [CGPoint] {
    return map(points) { CGPoint($0) }
}

let zigzag = CGPoints(
    (100,100),(100,150),(150,150),(150,200)
)
还有一个:

func CGPoints(points:(x:CGFloat, y:CGFloat)...) -> [CGPoint] {
    return map(points) { CGPoint($0) }
}

let zigzag = CGPoints(
    (100,100),(100,150),(150,150),(150,200)
)

CGPointMake100,100使用CGPointMake100,100使用CGPointMake100,100使用CGPointMake100,但是我想使用Int,Int,感谢CGPointx:100,y:100也可以使用zigzag.count,而函数需要一个UInt,所以必须显式转换:UIntzigzag.count可以将Int,Int转换为CGPoint吗或者CGPointx:100,y:100也就是zigzag.count是Int,而函数需要一个UInt,因此必须显式转换:UIntzigzag.count是否可以将Int,Int转换为CGPoint谢谢,你的解决方案是awesome@lijinma谢谢:。还非常感谢@MartinR修复了我的代码行self.y=elements.count>1中的错误…谢谢,您的解决方案是awesome@lijinma谢谢:。还要感谢@MartinR修复了我代码行self.y=elements.count>1中的一个错误。。。