错误参数'height'必须在参数'left'之前

时间:2016-10-28 16:24:54

标签: swift

我正在制作一个小程序,用Swift绘制带有类的Shapes然后我得到错误参数'height'必须在参数'left'之前

import UIKit

class Shape {
    var name:String?
    var sides:Int?

    var top:CGFloat?
    var left:CGFloat?
    var strokeColor : CGColor = UIColor.blackColor().CGColor
    var fillColor : CGColor = UIColor.darkGrayColor().CGColor
    var lineWidth : CGFloat = 2
    init(name:String,sides:Int,top:CGFloat,left:CGFloat){
        self.name = name
        self.sides = sides
        self.top = top
        self.left = left
   }




    func Area() -> Double {
        return 0
    }

    func Perimeter() -> Double {

        return 0
    }


    func sayDetails(){
        print("This is a \(name) with \(sides) sides perimerter is \(Perimeter()) and Area is \(Area())")

    }


    func drawInContext(context : CGContextRef){

        print("DO NOTHING!!!")
    }


}



class rectShape : Shape {
    var height : Double?
    var width : Double?



    init(name : String, sides : Int , height : Double ,width : Double,top:CGFloat,left:CGFloat) {
        super.init(name: name, sides: sides,top:top,left:left)
        self.height = height
        self.width = width
    }


    override func Area() -> Double {
    return height!*width!
    }

        func Perimerter() -> Double {
                   return 2*(height!+width!)
        }



    override func drawInContext(context: CGContextRef) {
        let top : CGFloat = self.top!
        let left : CGFloat = self.left!

        CGContextSetStrokeColorWithColor(context, strokeColor)
        CGContextSetLineWidth(context, lineWidth)
        let myRect = CGRect(x: top, y: left, width: CGFloat(width!), height: CGFloat(height!))
        CGContextAddRect(context, myRect)


    }





}




class circleShape : Shape{

    var radius : Double?
    let pi : Double = 3.14159265358979323846


    init(name : String, sides : Int , raduis : Double , top : CGFloat, left : CGFloat) {
        super.init(name: name, sides: sides , top:top, left: left)
        self.radius = raduis
    }


    override func Area() -> Double {
        return pi * pi * radius!
    }

    override func Perimeter() -> Double {

        return 2 * pi * radius!
    }

    override func drawInContext(context: CGContextRef) {

        if self.top == nil{return}

        let top  : CGFloat = self.top!
        let left : CGFloat = self.left!

        CGContextSetStrokeColorWithColor(context, strokeColor)

        CGContextSetLineWidth(context, lineWidth)

        let myRect = CGRect(x: top, y: left, width: CGFloat(radius!), height: CGFloat(radius!))

        CGContextAddEllipseInRect(context, myRect)
    }


}

class RightTriangle : Shape {
    var sideA : Double?
    var sideB : Double?


    init(name : String, sides : Int , sideA : Double, sideB : Double, top: CGFloat, left : CGFloat) {
        super.init(name: name, sides: sides,top:top,left:left)
        self.sideA = sideA
        self.sideB = sideB
    }

    override func Area() -> Double {
        return sideA! * sideB! / 2
    }

    override func Perimeter() -> Double {
        let c = sqrt(sideA! * sideA! + sideB! * sideB!)
        return  sideA! + sideB! + c
    }

    override func drawInContext(context: CGContextRef) {
        let top : CGFloat = self.top!
        let left : CGFloat = self.left!

        CGContextSetStrokeColorWithColor(context, strokeColor)
        CGContextSetLineWidth(context, lineWidth)
        var myPoints : Array<CGPoint>=[]
        myPoints.append(CGPoint(x: top, y: left))
        myPoints.append(CGPoint(x: top, y: left+CGFloat(sideB!)))
        myPoints.append(CGPoint(x: top+CGFloat(sideA!) , y: left+CGFloat(sideB!)))
        myPoints.append(CGPoint(x: top, y: left))

        CGContextAddLines(context, myPoints, 4)


    }

}





class czfView : UIView {

    var drawObject : Array<Shape> = []

    override func drawRect(rect: CGRect) {
        let context = UIGraphicsGetCurrentContext()
        for each in drawObject {
            each.drawInContext(context!)
            CGContextStrokePath(context)
        }
    }
}





class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        let myDrawView = czfView(frame: CGRect(x: 0, y: 20, width: 200, height: 400))
        myDrawView.backgroundColor = UIColor.yellowColor()
        self.view.addSubview(myDrawView)

        let myRect = rectShape(name: "Rectangle", sides: 4, top: 10, left: 20, height: 30, width: 60)

        myRect.lineWidth = 10
        myDrawView.drawObjects.append(myRect)

        let myCircle = circleShape(name: "Circle", sides: 1, raduis: 60, top: 100, left: 100)
        myCircle.lineWidth = 10

        let myTriangle = RightTriangle(name: "Right Triangle", sides: 3, sideA: 80, sideB: 100, top: 100, left: 100)
        myDrawView.drawObject.append(myTriangle)
        myTriangle.lineWidth = 5
        myTriangle.strokeColor = UIColor.redColor().CGColor

        myRect.lineColor = UIColor.redColor().CGColor
        myRect.top = 120


    }

}

你们中的任何人都知道问题是什么吗? 问题出在底部

1 个答案:

答案 0 :(得分:2)

就像错误所说,你的论点不合时宜。

比较声明:

init(name : String, sides : Int , height : Double ,width : Double,top:CGFloat,left:CGFloat)

和(错误的)呼叫站点:

rectShape(name: "Rectangle", sides: 4, top: 10, left: 20, height: 30, width: 60)

应该是:

rectShape(name: "Rectangle", sides: 4, height: 30, width: 60, top: 10, left: 20)