Reputation: 6679
I am trying to use 'weights' array to fill a graph with the below fetch request. But I am getting the error "Cannot invoke 'init' with argument of type 'NSNumber' and I have no idea why. 'weights' array should be an array of UInt16.
var weights : [Int16] = []
func weightFetchRequest() -> NSFetchRequest {
let appDelegate = UIApplication.sharedApplication().delegate as 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) as [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)] as NSNumber) //Error here
}
return 0
}
Upvotes: 1
Views: 2477
Reputation: 539845
There are two problems in the line
return CGFloat(weights[Int16(horizontalIndex)] as NSNumber)
weights[Int16(horizontalIndex)]
does not compile because an Int16
cannot be an array subscript. It should be weights[Int(horizontalIndex)]
.weights[...] as NSNumber
does not compile because there is no automatic bridging between the fixed-size integer types and NSNumber
,
it should be NSNumber(short: weights[...])
.So this would compile and work:
return CGFloat(NSNumber(short: weights[Int(horizontalIndex)]))
However, there is no need to use NSNumber
in between, it can be
simplified to
return CGFloat(weights[Int(horizontalIndex)])
Upvotes: 1