Xcode autocomplete does not work in Sources folder of Swift playgrounds - swift

I am working on a Swift playground that contains multiple files in the Sources folder alongside the main file. When I type something, for instance, "UI", in the main file, I get the expected autocomplete suggestions. However, when I do the same in files in the Sources folder, I get a "dumb" autocomplete menu as seen in this question: Xcode 9 Autocomplete Not Working 100% - Partially Working
I have tried deleting DerivedData as suggested in the above question, and have even gone to the lengths of reinstalling Xcode entirely. However, the problem has not gone away. I have created a new playground to ensure it is not specific to this project, and the same problem occurs.
Edit: I just tried the same thing on a different Mac and the same problem was observed. I suppose this is simply a bug in Xcode that has nothing to do with this particular installation or project.

In order to enable autocompletion, you can embed your Playground in a regular Xcode project (e.g. an iOS application). I recommend creating a dummy project for that purpose. Simply drag and drop your playground in this dummy project and make sure to check "Add to target".
Then you can navigate to
Target -> Build Phases -> Compile Sources -> + -> Add other
and add all the files from your source folder. Please note, that you don't need to actually copy the files, a reference is enough for this purpose.
After this process all your source files are built against this dummy target and you can use autocompletion as usual. As far as I know, this is the best practice for debugging Playgrounds right now. Anyway I am curious, if there is an easier way to achieve that.

Swift playgroundbooks do not support autocomplete. Try writing the code in a regular Swift playground, and then move the code over to the playgroundbook.

This works for me every time,
Restart/Start -> Open any existing Xcode project(use autocomplete) -> Open playground
Autocomplete will work as usual

Related

getting module not found after importing packages in swift

I've done a bit of swift, but I've never tried to use a third party package before. I created a brand new swift app, with Mac/iOS version and tests from the wizard. I went to xcode, right clicked on the sidebar, and chose add packages, and put in a package by url:
https://github.com/witekbobrowski/EPUBKit.git
It goes away for a little bit - and three package dependencies appear on my screen:
and when I look at my project settings, I can see them there:
(I actually added the zip package separately after the first one didn't work, just to test it, wondering whether something was wrong with the package)
Anyway - I can now see the packages, browse into them, see code or whatever - and the package found its own dependencies, so everything looks like it's working fine. But if I go into my swift app main, and add
import EPUBKit
or
import Zip
it just says "No such module ...".
I've obviously missed a step - what am I doing wrong - how do I fix it? I don't even know how to investigate it, because everything I look at just looks completely fine and I don't get any errors. I tried cleaning builds, restarting Xcode, using the real version of Xcode 13.2.1, using Xcode 13.3 beta - and nothing works.
NEW LEARNINGS
so - I tried changing how I'm running it to produce an IOS app - and it's working fine - but when I produce a Mac app, it's like the package isn't even there? Why would that be, and how do I make it work for Mac as well as IOS?
Author of EPUBKit here, thanks for taking interest in my library!
The issue is reproducible in a new project which kinda makes sense but don't at the same time. I am glad Xcode is not adding a package to every single target in my project but at the same time why does it assume that I want it in the iOS target? Anyway, I know that this question was already answered in the comment and the problem is solved but I might as well just drop more detailed instructions. Maybe it will help someone.
1. Select Project from Project Navigator
Select te target that is not working for you, in this case it's the macOS target. In the General tab you will see the Frameworks, Libraries, and Embedded Content section. If the package is missing from the list, tap the plus button at the bottom.
2. Add Missing Library
Look for the one you are missing, in this case its of course EPUBKit and tap the Add button.
3. Success
Now the library is successfully added to your target. Go ahead and build the project to run and enjoy your app :)

Xcode cannot find my Localizable.stringsdict file

I am trying to add pluralizable localization to my Xcode project, using Xcode 11.2.1
I followed the steps in this tutorial: https://medium.com/#vitaliikuznetsov/plurals-localization-using-stringsdict-in-ios-a910aab8c28c , which is a pretty basic instruction on how these things work. Mine, however, did not work, SO, I tried the following:
0) Actually running the tutorial code to make sure it works on my copy of Xcode. It does.
1) Copying the example Localizable.stringsdict from the tutorial into my program, and using
the keys from said tutorial in my calls, in case I had made a bad Localizable.stringsdict
or was calling it wrong. This failed.
2) Creating the Localizable.stringsdict as a property list, as some older questions suggested, instead of a Stringsdict file when creating it new, and copying the known 'good xml' of the example plist into it. This failed.
3) I tried removing all localized content from the project, and re-adding the strings file and the stringsdict file. Now it can't find either of them.
Clearly I must be missing some configuration, some step, some way of saying HEY XCODE LOCALIZE THESE. When I look at the 'Localizations' tab in the Project, there is only 1 file localized, for "Base"
Yesterday, I succeeded to localize Swift App using this tutorial.
Please try to follow this tutorial. Thanks
Number 4 was the clue here. My project has a lot of targets. Add your stringsdict file to the targets it needs to be added to. Derp.
I added the dict file to the proper target and everything works fine.
Always check the obvious stuff first.
Open your localized stringdict file, then in the right inspector click on Identity and Type you will see localization block with a button, click on it and project will recognize your file now

Finding the right way to include static libraries in Xcode

First of all I have to say that I'm new with Xcode and I found some things a bit comfusing for beginners.
For now I want to include SQLite support in my test app. I found the wrapper "FMDB" which seems very easy to use.
I was already able to include the .m and .h files into my project and setting the header line for bridging to Swift.
But if I build my app then I will get some link errors. I think the problem is that the sqlite library is missing. But I've no idea to include it. So I searched the web for some tutorials but all I found seems too old and didn't help me.
What I've already done are the following steps: I selected the top item of my project in the Project Explorer. Then I selected my build target and switched to the tab "Build Phases". But now I see no possibility to include the libsqlite.a file. I followed some suggestions and typed the keyword "sqlite" in the search field but I only get the message "No results found."
I'm using Xcode 8 + Swift 3 on Mac Sierra.
I think that U should find libsqlite.a file in the filesystem and only then add libsqlite.a to project by right click on project files in Xcode 8 and selecting "add files to " and select you .a library from system location like Homebrew's sqlite root folder, only then you can select this static library in "+" linking menu. Please read this answer:
https://stackoverflow.com/a/15974037/2835276
I solved my problem by including sqlite3.c and sqlite3.h from sqlite.org so every wrapper works fine.

How to manually add SSZipArchive to my project

First off, I'm a Mac OSX newbie, both using and programming, but it's been thrust upon me. My project is moving along nicely, but I need to unzip a file, and it looks like SSZipArchive seems like the way to go. I want to manually add it to my project, so that it stays all together in my source code control system. Others, esp. the release engineer, shouldn't need to do anything, and we need to have all the source code local.
I've tried a million ways and back, only to keep getting the dreaded "No Such module" error. I've researched for a couple days now, and still no closer. So I'm starting from scratch in a new project to say what I'm doing and to plead for some help here. All this is being done on an El Capitan laptop, using Xcode 7.3.1, with Swift 2.2.
Create a Cocoa Application project, calling it 'ziptest', using Swift, nothing else checked. Build it just for fun.
Drag 'n' Drop the SSZipArchive folder found in the master.zip downloaded from the SSZipArchive GitHub site into my project. I'm not sure where it should go, or if it matters - top level, between ziptest project icon and the ziptest group or inside the ziptest group. But I'll put it inside the ziptest group. Now I'm faced with a new question - for the "Added folders", do I want to "Create groups" or "Create folder references". I do copy the items, because, as I said, I want to add them to SCCS later. I saw somewhere here on Stackexchange to make sure it is Group, so that's what I'll do. Build again and it still works.
I know I need a "bridging header" file, so I'll create it. I thought at some point, Xcode asked if I wanted to create it, but the above step doesn't do that. After much experimention, I found that if I d'n'd the SSZipArchive.m/.h files directly, it asks me. It doesn't put anything into it, but at least it creates it and updates the Build Settings. But I do this now manually by right clicking on the ziptest group folder, select New File... and create an empty Header File called "ziptest-Bridging-Header.h", following the pattern from the Xcode created one, being sure to check off the ziptest Target.
I add:
#import "SSZipArchive.h"
to that file. And building still works.
I added ziptest-Bridging-Header.h as the Objective-C Bridging Header in the Build Settings. And building still works.
Now comes the moment of truth. I add:
#import SSZipArchive
to AppDelegate.swift and I get the dreaded No such module 'SSZipArchive'
What am I doing wrong?
Edited to add: Oh, I also added libz.dylib to the Build Phases -> Link Binary With Libraries part. Doesn't help, of course, because we haven't even gotten to the link phase yet.

Xcode 7.3 autocomplete is so frustrating

There is a new autocomplete in Xcode. Probably might be useful because it checks not only beginning of names etc. But I found that very often it doesn't find a class name or a const name at all etc. I need to type in entire name by myself. Over all I found it makes my life harder and coding more time consuming. Is there a way to switch to the old way it used to work?
Xcode 7.3.1
In Xcode > Preferences > Text Editing
uncheck Enable type-over completions
restart Xcode
It seems that clearing the checkbox "Enable type-over completions" in XCode -> Preferences -> Text Editing does the trick. At least in my case autocompletion fell back to a sort of old way, so it could autocomplete the class name that I had to type in manually before that.
This is by no means an adequate solution, BUT it has allowed me to (barely) maintain my sanity the past few days:
After every build, you need to trash your Derived Data folder. You can find this folder in Xcode > Preferences > Locations > Derived Data. Just trash the whole thing and it'll kick off a re-indexing step that should restore proper autocomplete functionality.
Unfortunately, I've found that once I build, the autocomplete behavior reverts to its broken state.
just open Xcode derived data folder and delete the folder
/Users/yourUserName/Library/Developer/Xcode/DerivedData
then restart Xcode, now autocompletion works like a charm
I have the impression that some 'parts' of autocompletion simply fail after a while. I use to restart the Mac to get it back working. But sometimes it fails quite soon again.
Maybe the answer of #Alex Bykov combined with a restart will do the trick.
Anyway: auto-completion of Xcode always was crap. AppCode used to get it much better. Unfortunately not yet with swift.
Closed Xcode, opened Xcode, let it index, it worked.
it doesn't find a class name
As a work-around, you can try to press
Command + Shift + K and Command + B
Several times,it works temporarily.
Xcode 7.3.1
I will share another posible reason, that after couple of days we found out. We have multiple schemes, and in one of them, the bridging header was importing a file that didn't exist anymore. So, it didn't break while compiling and running (the header belongs to another scheme) but it caused the autocompletion to break (couldn't find any objective-c class).
Hope it would help someone!
After having tried different methods:
Delete Derived Data
Switching Module Enabled off in Build Settings
Full Clean
Relaunch
Only this worked:
Find any commented out (/* abc */) code after #end in your files and delete.
Credit to Max_B:
https://forums.developer.apple.com/thread/7439
In my case, other projects were auto-completing correctly. If all your projects fail to correctly predict code, then it might be a different issue, and the other answers might work.
I ran into this issue while trying to make an OS X app and I was able to fix this issue by making sure that the Xcode 7.3 documentation and the OS X 10.11.4 documentation was actually downloaded. Doing this fully restored my autocomplete functionality. My full instructions are below as well as in my answer to a similar question: https://stackoverflow.com/a/39420664/3444925
I had this problem myself and after looking through all the other similar questions & answers about this, I couldn't find a solution. However, I finally found what worked for me.
Go to Xcode -> Preferences -> Components. There you will probably find a screen that looks like the following:
This shows that the documentation has not has not been downloaded and therefore, any attempts to re-index or re-build the application without downloading the documentation would prevent you from being able to use the autocompletion functionality.
Once I downloaded the Xcode 7.3 Documentation and the OSX 10.11.4 Documentation, this was enough for me to get the autocomplete functionality back (I was trying to build an OS X app, so feel free to download as much documentation as is relevant for you).
I didn't run into this until I created a couple of new class files. Other classes worked fine, but autocomplete would NOT work for anything in the new files...
FIX (for me) - I had to add those files to ALL of my targets, including the unit test targets even though I wasn't using them yet.