2016-10-28 3 views
-3

я делал небольшую программу для рисования фигур с классами с Swift я получил аргумент ошибки «высоты» должен предшествовать аргумент «влево»ошибка Аргумент «высота» должен предшествовать аргумент «левую»

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 


    } 

} 

Do кто-нибудь из вас знает, в чем проблема? Проблема находится в нижней части

+1

Что линия является ошибка? Кроме того, ваш отступы являются своего рода ударом, можете ли вы исправить это? – Alexander

+0

На нижней стороне прокрутите вниз и найдите let myRect = rectShape (имя: «Прямоугольник», стороны: 4, вверху: 10, слева: 20, высота: 30, ширина: 60) – Dordor

+0

вы также можете скопировать его на Xcode и запустите его – Dordor

ответ

1

Как и в случае ошибки, ваши аргументы не в порядке.

Сравните заявление:

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) 
+0

спасибо, что это сработало – Dordor

Смежные вопросы