简体   繁体   English

Swift:无法将NSNumber类型转换为预期的参数类型Int16

[英]Swift: Cannot convert of type NSNumber to expected argument type Int16

I have a CoreData entity named EntityOne and it has two attributes: 我有一个名为EntityOne的CoreData实体,它具有两个属性:

coreValue of type Int16 类型Int16 coreValue
coreDate of type NSDate NSDate类型的coreDate

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!) 当我尝试从提取请求访问核心数据条目并将值附加到单个数组时,该行出现错误: 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!)
            }

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)
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法转换“UnsafePointer”类型的值<T> &#39; 到预期的参数类型 &#39;UnsafePointer<Int16> &#39; - Cannot convert value of type 'UnsafePointer<T>' to expected argument type 'UnsafePointer<Int16>' 无法在Swift 3中将字符串类型的值转换为预期的参数类型int - cannot convert value of type string to expected argument type int in swift 3 Swift CoreData - 无法将“Int”类型的值赋给“Int16”类型 - Swift CoreData — Cannot assign value of type 'Int' to type 'Int16' 无法将时间&#39;Int&#39;的值转换为Swift 2中的预期参数类型&#39;NSPropertyListReadOptions&#39; - Cannot convert value of time 'Int' to expected argument type 'NSPropertyListReadOptions' in Swift 2 将Int16类型的Integer保存到Core Data-Swift - Saving an Integer of type Int16 to Core Data - Swift 无法使用参数“ NSNumber”(Swift)的类型调用“ init” - Cannot invoke 'init' with argument of type 'NSNumber' (Swift) Swift错误,无法将Int类型转换为Int 16 - Swift error, cannot convert type Int to int 16 无法转换“字符串”类型的值? 到预期的参数类型 &#39;Int64&#39; swift ios - Cannot convert value of type 'String?' to expected argument type 'Int64' swift ios Swift 1.2到Swift 2:无法将类型的值转换为预期的参数类型 - Swift 1.2 to swift 2: Cannot convert value of type to expected argument type 无法转换“inout NSNumber”类型的值? 到预期的参数类型 &#39;AutoreleasingUnsafeMutablePointer<AnyObject?> &#39; 错误 - Cannot convert value of type 'inout NSNumber?' to expected argument type 'AutoreleasingUnsafeMutablePointer<AnyObject?>' error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM