ios - Cannot invoke 'init' with argument of type 'NSNumber' (Swift) -


i trying use 'weights' array fill graph below fetch request. getting error "cannot invoke 'init' argument of type 'nsnumber' , have no idea why. 'weights' array should array of uint16.

var weights : [int16] = []   func weightfetchrequest() -> nsfetchrequest {             let appdelegate = uiapplication.sharedapplication().delegate appdelegate             let managedcontext = appdelegate.managedobjectcontext!             let fetchrequest = nsfetchrequest(entityname: "assessment")             fetchrequest.sortdescriptors = [nssortdescriptor(key: "nsdateofassessment", ascending: true)]             var error: nserror?              let fetchedresults = managedcontext.executefetchrequest(fetchrequest, error: &error) [assessment]?              if let assessments = fetchedresults {                 let weightss = assessments.map { assessment in assessment.weight }                  weights = weightss                  println(weights)                 println(weightss)             }             return fetchrequest         }  func linechartview(linechartview: jblinechartview!, verticalvalueforhorizontalindex horizontalindex: uint, atlineindex lineindex: uint) -> cgfloat {         if (lineindex == 0) {             return cgfloat(weights[int16(horizontalindex)] nsnumber) //error here         }         return 0     } 

there 2 problems in line

return cgfloat(weights[int16(horizontalindex)] nsnumber) 
  • weights[int16(horizontalindex)] not compile because int16 cannot array subscript. should weights[int(horizontalindex)].
  • weights[...] nsnumber not compile because there no automatic bridging between fixed-size integer types , nsnumber, should nsnumber(short: weights[...]).

so compile , work:

return cgfloat(nsnumber(short: weights[int(horizontalindex)])) 

however, there no need use nsnumber in between, can simplified to

return cgfloat(weights[int(horizontalindex)]) 

Comments

Popular posts from this blog

javascript - AngularJS custom datepicker directive -

javascript - jQuery date picker - Disable dates after the selection from the first date picker -