R.Ham R.Ham - 1 month ago 8
Swift Question

Swift: Cannot convert of type NSNumber to expected argument type Int16

I have a CoreData entity named EntityOne and it has two attributes:
coreValue of type Int16
coreDate of type NSDate

When I try to access the core data entries from a fetch request and append the values to individual arrays the error appears for the line: coreDataValues.append(item.coreValue!)

let request = NSFetchRequest(entityName: "EntityOne")
let fetchResults = try context.executeFetchRequest(request) as! [EntityOne]

var coreDataDates = [NSDate]()
var coreDataValues = [Int16]()

for item in fetchResults {
coreDataDates.append(item.coreDate!)
coreDataValues.append(item.coreValue!)
}

Answer

Try doing this:

item.coreValue!.shortValue

You should also think about safely unwrapping these before using them:

if let value = item.coreValue {
    coreDataValues.append(value.shortValue)
}