无法将类型'__NSCFString'(0x103c93c50)的值转换为'NSNumber'(0x103535b88)。错误 [英] Could not cast value of type '__NSCFString' (0x103c93c50) to 'NSNumber' (0x103535b88). Error

查看:75
本文介绍了无法将类型'__NSCFString'(0x103c93c50)的值转换为'NSNumber'(0x103535b88)。错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个带有一行id整数PRIMARY KEY的SQLite表。我使用本教程将SQLite与我的应用程序集成。我正在尝试将其添加到Ints数组中。这是我的代码:



让所有=  从\(self.tableName)中选择id 
let fullArray = self .dbManager.loadDataFromDB(all)

for currentObject in fullArray {
println(currentObject [< span class =code-digit> 0 ])
println(_stdlib_getDemangledTypeName(currentObject [ 0 ]))
self .tableViewData.append(currentObject [ 0 ] as!Int) // tableViewData是一个Ints数组
}



println语句的结果:



2

Swift.ImplicitlyUnwrappedOptional

当我运行应用程序时,我收到以下错误:代码的最后一行。


无法将'__ NSCFString'(0x103c93c50)类型的值转换为'NSNumber'(0x103535b88)。

我做错了什么,我该如何解决?

解决方案

从string到int的类型转换是个问题。您无法使用类型转换从字符串对象中提取数值。你必须转换它。



我不太适合Swift,但这样的代码应该可以完成这项工作。



 currentObject [ 0 ]!。toInt() //   Swift 1.0  
让我:Int? = Int(currentObject [ 0 ]!) // Swift 2.0


I created an SQLite table with a row of id integer PRIMARY KEY. I used this tutorial to integrate SQLite with my app. I'm trying to add that to an array of Ints. Here's my code:

let all = "select id from \(self.tableName)"
let fullArray = self.dbManager.loadDataFromDB(all)

for currentObject in fullArray {  
    println(currentObject[0])
    println(_stdlib_getDemangledTypeName(currentObject[0]))
    self.tableViewData.append(currentObject[0] as! Int) //tableViewData is an array of Ints
}


Results of println statements:

2
Swift.ImplicitlyUnwrappedOptional
When I run the app, I get the following error at the last line of the code.

Could not cast value of type '__NSCFString' (0x103c93c50) to 'NSNumber' (0x103535b88).
What am I doing wrong, and how can I fix it?

解决方案

your type cast from string to int is the problem. You cant use a type cast to extract the numeric value from a string object. You must convert it.

I am not so fit on Swift but code like this should do the job.

currentObject[0]!.toInt() //Swift 1.0
let i:Int? = Int(currentObject[0]!) // Swift 2.0


这篇关于无法将类型'__NSCFString'(0x103c93c50)的值转换为'NSNumber'(0x103535b88)。错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆