ios - EXC_BAD_INSTRUCTION when trying to pass data to UIViews -
i created call of uiview in draw graph. trying pass new data , have update.
when run app on simulator , click tab in the controller housing view in, receive error:
thread 1: exc_bad_instruction (code=exc_1386_invop, subcode=0x0)
at line:
let maxvalue = graphpoints.maxelement()
here code view:
@ibdesignable class graphview: uiview { var graphpoints :[int]! override init(frame: cgrect) { super.init(frame: frame) } init(graphpoints: [int]) { self.graphpoints = graphpoints super.init(frame: cgrectzero) } required init?(coder adecoder: nscoder) { super.init(coder: adecoder) } @ibinspectable var startcolor: uicolor = uicolor.redcolor() @ibinspectable var endcolor: uicolor = uicolor.greencolor() override func drawrect(rect: cgrect) { let width = rect.width let height = rect.height //set background clipping area let path = uibezierpath(roundedrect: rect, byroundingcorners: uirectcorner.allcorners, cornerradii: cgsize(width: 8.0, height: 8.0)) path.addclip() //2 - current context let context = uigraphicsgetcurrentcontext() let colors = [startcolor.cgcolor, endcolor.cgcolor] //3 - set color space let colorspace = cgcolorspacecreatedevicergb() //4 - set color stops let colorlocations:[cgfloat] = [0.0, 1.0] //5 - create gradient let gradient = cggradientcreatewithcolors(colorspace, colors, colorlocations) //6 - draw gradient var startpoint = cgpoint.zeropoint var endpoint = cgpoint(x:0, y:self.bounds.height) cgcontextdrawlineargradient(context, gradient, startpoint, endpoint, .drawsbeforestartlocation) //calculate x point let rightmargin:cgfloat = 40 let leftmargin : cgfloat = 10 let columnxpoint = { (column:int) -> cgfloat in //calculate gap between points let spacer = (width - rightmargin - leftmargin - 4) / cgfloat((self.graphpoints.count - 1)) var x:cgfloat = cgfloat(column) * spacer x += leftmargin + 2 print(x) return x } // calculate y point let topborder:cgfloat = 30 let bottomborder:cgfloat = 50 let graphheight = height - topborder - bottomborder let maxvalue = graphpoints.maxelement() let columnypoint = { (graphpoint:int) -> cgfloat in var y:cgfloat = cgfloat(graphpoint) / cgfloat(maxvalue!) * graphheight y = graphheight + topborder - y // flip graph print(y) return y } // draw line graph uicolor.whitecolor().setfill() uicolor.whitecolor().setstroke() //set points line let graphpath = uibezierpath() //go start of line graphpath.movetopoint(cgpoint(x:columnxpoint(0), y:columnypoint(graphpoints[0]))) //add points each item in graphpoints array //at correct (x, y) point in 1..<graphpoints.count { let nextpoint = cgpoint(x:columnxpoint(i), y:columnypoint(graphpoints[i])) graphpath.addlinetopoint(nextpoint) } //create clipping path graph gradient //1 - save state of context (commented out now) cgcontextsavegstate(context) //2 - make copy of path let clippingpath = graphpath.copy() as! uibezierpath //3 - add lines copied path complete clip area clippingpath.addlinetopoint(cgpoint( x: columnxpoint(graphpoints.count - 1), y:height)) clippingpath.addlinetopoint(cgpoint( x:columnxpoint(0), y:height)) clippingpath.closepath() //4 - add clipping path context clippingpath.addclip() let highestypoint = columnypoint(maxvalue!) startpoint = cgpoint(x:leftmargin, y: highestypoint) endpoint = cgpoint(x:rightmargin, y:self.bounds.height) cgcontextdrawlineargradient(context, gradient, startpoint, endpoint, .drawsbeforestartlocation) cgcontextrestoregstate(context) //draw line on top of clipped gradient graphpath.linewidth = 2.0 graphpath.stroke() //draw circles on top of graph stroke in 0..<graphpoints.count { var point = cgpoint(x:columnxpoint(i), y:columnypoint(graphpoints[i])) point.x -= 5.0/2 point.y -= 5.0/2 let circle = uibezierpath(ovalinrect: cgrect(origin: point, size: cgsize(width: 5.0, height: 5.0))) circle.fill() } //draw horizontal graph lines on top of let linepath = uibezierpath() //top line linepath.movetopoint(cgpoint(x:leftmargin, y: topborder)) linepath.addlinetopoint(cgpoint(x: width - rightmargin, y:topborder)) //center line linepath.movetopoint(cgpoint(x:leftmargin, y: graphheight/2 + topborder)) linepath.addlinetopoint(cgpoint(x:width - rightmargin, y:graphheight/2 + topborder)) //bottom line linepath.movetopoint(cgpoint(x:leftmargin, y:height - bottomborder)) linepath.addlinetopoint(cgpoint(x:width - rightmargin, y:height - bottomborder)) let color = uicolor(white: 1.0, alpha: 0.3) color.setstroke() linepath.linewidth = 1.0 linepath.stroke() }
and here code view controller in pass data view.
import uikit import quartzcore class progressviewcontroller: uiviewcontroller { @iboutlet weak var graphview: uiview! var firstgraph : graphview! override func viewdidload() { self.graphview = self.firstgraph self.firstgraph = graphview(graphpoints: [2240, 1983, 2171, 2017, 1842, 1992, 2347])
i'm new swift , i'm stumped on problem after looking everywhere answer. appreciated.
Comments
Post a Comment