Re: [fmdb] FMDB and Swift

379 views
Skip to first unread message

BareFeetWare

unread,
Jul 29, 2014, 7:04:26 PM7/29/14
to fm...@googlegroups.com
On 30 Jul 2014, at 8:18 am, John Zumsteg <john.z...@gmail.com> wrote:

Has anyone had success implementing FMDB in a Swift IOS 8 project, using Xcode 6.0 beta? I've been trying for a couple days to do that, with a notable lack of success. 

This line of objective-c creates an instance os FMDatabase, with the database at dbpath:

db = [FMDatabase databaseWithPath:dbPath];

I think the Swift equivalent to this is:

var db = FMDatabase.databaseWithPath(aPath:dbPath)

but the compiler won't allow that, giving me the error: "databaseWithPath is unavailable: use object construction 'FMDatabase(path:)'

Perhaps you want:

var db = FMDatabase.databaseWithPath(dbPath)

Thanks, 
Tom 

Tom Brodhurst-Hill 
BareFeetWare 👣

-- 
iPhone/iPad/iPod and Mac software development, specialising in databases 
-- 
Follow us on Twitter: http://twitter.com/barefeetware/ 

John Zumsteg

unread,
Jul 29, 2014, 7:23:56 PM7/29/14
to fm...@googlegroups.com
Thanks, Tom, but no success.

Using var db = FMDatabase.databaseWithPath(dbPath) doesn't compile. I get a message that "databaseWIthPath is not available: use object construction FMDatabase(path:)". And that's correct, I think.

Digging into how Swift initializes Objective-c classes, the correct call is:

var db = FMDatabase(path:dbPath)

Swift automatically allocates a new FMDatabase, then looks for instance methods with "initWith". It strips off the "initWith," lowercases the remainder, and uses that as a parameter. So the FMDatabase initWithPath(NSString* path) becomes the line above. That call compiles and executes, but I get a nil database. More debugging to follow.

John 

Dave Reed

unread,
Jul 29, 2014, 8:22:12 PM7/29/14
to fm...@googlegroups.com


On Tuesday, July 29, 2014 7:04:26 PM UTC-4, barefeettom wrote:
On 30 Jul 2014, at 8:18 am, John Zumsteg <john.z...@gmail.com> wrote:

Has anyone had success implementing FMDB in a Swift IOS 8 project, using Xcode 6.0 beta? I've been trying for a couple days to do that, with a notable lack of success. 

This line of objective-c creates an instance os FMDatabase, with the database at dbpath:

db = [FMDatabase databaseWithPath:dbPath];

I think the Swift equivalent to this is:

var db = FMDatabase.databaseWithPath(aPath:dbPath)

but the compiler won't allow that, giving me the error: "databaseWithPath is unavailable: use object construction 'FMDatabase(path:)'

Perhaps you want:

var db = FMDatabase.databaseWithPath(dbPath)

Thanks, 
Tom 


Based on the compiler error message, I'd try:
var db = FMDatabase(path:dbPath)

HTH,
Dave 

John Zumsteg

unread,
Jul 29, 2014, 11:23:21 PM7/29/14
to fm...@googlegroups.com
Thanks, Dave. I just figured out that this works. But now when I execute a query, I end up with a run-time error that the db is not open, although it appears to be open.

I think these problems are related more to me trying to learn the peculiarities of running Objective-c classes in a Swift project. Just gonna take more trial-and-error; mostly error!

Anyway, thanks for your response.

John

Dave Reed

unread,
Jul 30, 2014, 1:39:16 PM7/30/14
to fm...@googlegroups.com

I don't know how familiar you are with FMDB (i.e., if you've used it with Objective-C before) but you do have to call an "open" method after creating the FMDatabase object before you can actually execute queries as the README shows under the "opening" section: https://github.com/ccgus/fmdb

I've experimented a bit with Swift (with Core Data not FMDB) but am by no means an expert. If you have a sample project you can put on github, I'm willing to take a quick look at it.

Dave

John Zumsteg

unread,
Jul 31, 2014, 5:57:20 PM7/31/14
to fm...@googlegroups.com
Dave,

Here's the embarrassing confession: I have used FMDB for half a dozen apps over the last five years, and I still forgot the open() statement. Once I added that, things started working.

Thanks for your help.

John

Dave Reed

unread,
Jul 31, 2014, 7:05:30 PM7/31/14
to fm...@googlegroups.com

I lost a half hour of my life once because of that. Fortunately for me, once is usually enough to keep me from making the same mistake again (or at least let me immediately realize it so I don't lose much time the second time I make the mistake).

Glad you got it working.
Reply all
Reply to author
Forward
0 new messages