[I originally posted this under the Server category by mistake; I am reposting here.]
I can’t get a Swift CBL project to build. I started with some simple things, more or less along the lines of the tutorial but got serious compiler errors. (I worked my through the tutorial in Objective-C first.) I desperately need a solution for a CBL project I am trying to do.
After many attempts to figure out what was going on, I decided to check my understanding of CBL project setup for Swift by trying to “recreate” the Swift version of the Grocery-Sync-iOS demo. (It would be lovely to have a Swift version of the tutorial’s Building your first Couchbase Lite iOS app.)
I am getting build errors with symbols that obviously should be defined, such as:
Here’s what I did to recreate the project (I think):
Create a new 1-window iOS app in Xcode
Copy everything from Grocery-Sync’s DemoAppDelegate.swift to the new project’s AppDelegate.swift
Copy everything from Grocery-Sync’s RootViewController.swift to the new project’s ViewController.swift
Copied Grocery-Sync’s Utilities.Swift to my project’s source folder and added it to the project
Added CouchbaseLite.framework and CouchbaseLiteListener.framework to the project
Followed the instructions for Creating a New Project on the CBL site’s tutorial to change the Other Linker Flags to -ObjC in Build Settings and added the 5 frameworks listed in the documentation to the Build Phases Link Binary with Libraries
My project is called cb1, so I (manually) created a cb1-Bridging-Header.h that contains #import <CouchbaseLite/CouchbaseLite.h>
Then I build, and get a slew of failures due to undefined symbols such as those listed earlier in the post.
I can’t follow the tutorial past that to write a simple example, because I’m working in Swift. What am I missing? The Grocery-Sync project builds, but mine doesn’t. Obviously I’ve omitted a necessary step.
I would think this post and, hopefully, responses with a solution will be of interest to anyone trying to build their first Swift CBL app.
Greatly appreciate your help. That’s what it looked like to me, but I couldn’t see what was wrong. Well, things got worse. First of all, I noticed that to my surprise the bridging header was not in the source folder (though it was in my project correctly). But there was nothing in the “Objective-C Bridging Header” build setting. So I moved cb3-Bridging-Header.h to my source file and added cb3/cb3-Bridging-Header.h. Now it’s a complete mess. I’m glad I didn’t see something like this originally or I might have given up completely. I can send you snapshots of the relevant parts of the screen.
That text in the build results window is selectable, so you can copy/paste it.
So, this is telling you that you’ve got undefined symbols, which generally means a library is missing. If you search/google for “SCNetworkReachability…” you’ll see that those are in the SystemConfiguration framework. You must have forgotten to add that to the linked libraries? (I just checked and it’s in the list in the docs.)
(To be clear, none of this is specific to Couchbase Lite — you’ll need to deal with bridging headers any time you use non-Swift code in a Swift project, and you’ll often need to add frameworks if you use an external library that uses those frameworks.)
Oops. Thanks. I had gone through the routine of adding the library dependencies a number of times and didn’t realize I had missed one. Yeah, I should have searched for the missing symbols. Build succeeded! The missing bridge specification was the key. I should have checked that, because Xcode rarely asks me whether I want it to make a bridging header when I create new projects or add the CB libraries. I thought it was supposed to. I don’t know why it doesn’t.
[I realize some of this is not specific to CB, but I don’t know enough about either to work my way through this – I will once I’m done! – and I really appreciate your help.]
I thought the build succeeded but when I went back to working on it I am getting a compiler error:
My demo compiles and runs successfully up until the point I get a blank simulator screen. Then I get an error Thread 1: EXC_BAD_INSTRUCTION with details that don’t help me in the beginning of this function:
That’s a crash, not a compile error. And it looks like it’s happening while UIKit is displaying your views, so it’s likely a problem with your xib or perhaps one of your methods that gets called when a view loads.
Look at the Debug navigator to see the stack. If it’s not showing any stack frames above the one you mentioned, then look at the bottom of the navigator for an icon button that looks like a square between two narrow rectangles, whose tooltip says “Show only stack frames with debug symbols…” and make sure it’s turned off (not blue). That will show you the entire stack.
This is basic Xcode stuff, not specific to Couchbase. I understand that you’re new to iOS development, but we really don’t have the resources to help with general troubleshooting like this. If you can isolate this down to a problem involving the Couchbase Mobile framework, we can look at it.
Thanks, that’s all great information. Until I work through this process I won’t understand what’s CBL and what’s not. Fine with me if you just say “not a CBL issue” and maybe give me a pointer or too. Not asking you to debug my program.
The obstacles I am running into may be obstacles when I start a Swift CBL application from scratch. In that case, what I am learning from you people should be turned into notes or a new tutorial explaining to people who want to use Swift with CBL what files and settings are necessary. It shouldn’t be this hard for me to reproduce the Swift Grocery-Sync project, even if I am not yet as familiar with Swift and Xcode as I know I should be.
Once this is working and once I have a simple CBL application built from scratch, I would be happy to send you notes on what I had to do to get both of those working. It’s going to become important that you support Swift more fully.