Click here to Skip to main content
15,885,365 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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:

Objective-C
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?
Posted

1 solution

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.

C++
currentObject[0]!.toInt() //Swift 1.0
let i:Int? = Int(currentObject[0]!) // Swift 2.0
 
Share this answer
 
Comments
Member 11360293 16-Sep-15 13:45pm    
Thanks for the answer! But I get an error saying "'AnyObject' does not have a member named 'toInt()'"

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900