How can I find all swift system modules I can import? - xcode

Is there anything in swift that works like frameworks list in Xcode while using Obj-C? I want to know what modules I can use by default. e.g.. CoreData, CoreImage, etc.

Method - 1
To use any Objective-C Framework in Swift, you need to create 'Bridging-header.h" file and import your framework there to use anywhere in your swift project.
To add Bridging header in Swift Project
Method - 2
You can direct import any framework.
E.g. import CoreData
The main problem is that Xcode doesn't provide "AutoComplete" option for "import" statement so that's the problem as of now.
Reference: Import Framework in Swift Project, Xcode
Edited:
Most Popular List of Swift Modules which you can get from Xcode:
https://github.com/andelf/Defines-Swift

Related

import Charts conflicts with cocoa pod library's name

I am trying to use Apple's Swift Charts (iOS 16 only) but it can't compile, I presume because the import statement is interpreted as targeting this library: https://github.com/danielgindi/Charts (that I am using in other places in the App and installed via Cocoapods) and not Apple's Swift Charts as I want to.
Using Swift Chart's API results in compile error: "Can not find 'Chart' in scope"
Any fix, workaround or better way to specify better what to import is welcomed, haven't found any other solution than nuking this library from my project, however I would need it for users with iOS 15 or less.
I seem to be unable to use Swift Charts because of this library

Using external libraries in Swift 4 playground

I've been trying to learn Swift 4, and therefore needed to use Xcode9 Beta as the IDE. I would like to load a large CSV data file to do some data analysis using a playground. The library I'm trying to use is CSVImporter, installed using Carthage, as recommended by the developer.
I've spent a week on this issue, trying to follow guides I've found online, such as here - Stackoverflow, here - Medium, but they all refer to previous versions of Swift and Xcode, and none seem to work.
In general their approach seems to be incorporating the playground into workspace with a project that uses the imported resource. I usually get the "No such module 'CSVImporter'".
My specific question is: How do I set up a Swift 4 Playground so that I can import CSVImporter with Xcode 9 Beta?
While I have this specific task, I think this would be of general interest to the community. I suspect that someone with a far clearer idea than I of how Xcode executes builds and resolves build dependencies would be able to address this fairly easily! Many thanks.
Basically, you have it right. Approach is:
Create new XCode project for Cocoa Framework under MacOS
Create Cartfile with your dependencies
Build dependencies (e.g. carthage update --platform macOS)
Import the .framework files that were built (from Carthage/Build/Mac)
Create extra build phases step that will run script /usr/local/bin/carthage copy-frameworks and copy imported frameworks (just follow Carthage's guidelines, it's all the same so far)
Create some .swift file (e.g. main.swift) that will just import frameworks that you have imported via Carthage
Save XCode project as a workspace file
Create new playground (also MacOS) and save it under the projects root folder of your "fake" framework
Add the playground into workspace (as of XCode 9 it's not possible to create playgrounds directly in the projects/workspaces, so you need to do this trick with adding an existing file to the workspace)
Build your project
In the playground file, import your "fake" framework (see step 1), and only after import your carthage dependency framework.
Profit
If your external libraries are SPM packages you can create a playground with Arena:
arena https://github.com/finestructure/Gala
🔧 resolving package dependencies
📔 libraries found: Gala
✅ created project in folder 'SPM-Playground'

How to Import Swift Source Code into a XCode Swift Project

I've written a working swift prototype. It contains a parent class and some subclasses. I want to be able to import the parent class into another project where I will create sub classes for it.
I created a new XCode project for a dynamic library, added the parent class to it and ran build. I've seen where you can't use static for Swift code.
I also created a different directory and set up a swift package. The build on it ran successfully.
When I add an import statement to the project where I want to use it, it says it can't find it. The statement is: "import RailsData", the name of the parent class.
I've tried adding the dynamic library project to "Linked Frameworks and Libraries". I also tried adding the swift file directly. XCode flags the import as not found.
The swift package manager example is all command line based so I'm not sure if will work with XCode projects. I would eventually like to make a package out of it.
Xcode 8.2, Swift 3.0. I'm pretty sure I'm missing something fundamental.
Update
I found a related question. It says to
Put your package manifest file into the same directory as the Xcode project, and then invoke swift package generate-xcodeproj
With a search I found this page that says:
Instructions for how to build a package are provided by the Package.swift manifest file.
So I copied the file to the root of my project and ran the command above. It created a new .xcodeproj. When I opened it, I saw my original files, but not anything from the package. That's not surprising as there is no reference to it's location in the file system. Also a mystery is what happens when you want additional packages. Do you keep generating new projects?
It seems like the missing link is the following command
swift package generate-xcodeproj
Actually Adding Swift 3 packages to Xcode 8 using the Swift package manager should answer your question.

Cannot debug Swift module/framework embedded in Objective-C app

Alternative titles (to aid searching)
Cannot debug Swift 2.3 framework linked to an Objective-C app in Xcode 8
error in auto-import: failed to get module 'XYZ' from AST context Xcode 8
Xcode 8 cannot debug Swift framework
warning: Swift error in module <XYZ>
Workaround for; Xcode Debugger cannot debug apps written in Objective-C only but that link against frameworks written in Swift only. (28312362)
I have an app written in Objective-C that links against some modules (frameworks) written in Swift 2.x.
Question
Everything (debugging etc.) works fine in xcode7, however when moving to xcode8 and updating the modules to use swift2.3 I was unable to debug the modules.
LLDB reported these errors:
warning: Swift error in module XYZ.
Debug info from this module will be unavailable in the debugger.
error: in auto-import:
failed to get module 'ABC' from AST context
This does not happen if I link the modules to an app built in Swift 2.3.
For me it was just as simple as it was painful and time consuming:
import SDWebImage was the PROBLEM,
because one of the frameworks had the SDWebImage already packed in it(and I couldn't see it), and that framework happened to be Objective-C, and the app was Swift.
I also added the SDWebImage to the project, because I use it in the classes I write, and that what created the mess the Xcode debugger couldn't deal with.
So basically, make sure you don't have ANYTHING duplicated in ANY way, I'd check for common things like SDWebImage for example.
use fr v instead po for debugging
For more debugging
https://www.codeproject.com/Articles/1181358/Debugging-with-Xcode
I discussed this issue with an Apple engineer named Sean at WWDC 2017.
My team spent weeks trying to figure this out, and it ended up being a bug on Apple's compiler, which we could never have figured out by ourselves. Also, it has a VERY easy workaround.
There happens to be a bug with the way the compiling flags get aggregated from the frameworks and the project, and the "pure Objective-C" project "activates" it.
Solution: add one single, empty Swift file ("Whatever.swift", or whatever) in your Objective-C project, making it not-pure-objective-c anymore (new->file->Swift file, don't create the bridging header. The file will only contain the import of Foundation).
And that's it. Problem solved.
tl:dr
Add a user defined setting under "Build Settings" for your app target.
SWIFT_VERSION = 2.3
More Info
I'm unsure if this is an Xcode 8 bug or if it's Apple policy (to try an force developers to Swift 3.0?). But... by default Xcode 8 installs the Swift 3.0 versions of the standard Swift runtime libraries.
When it comes to debugging with LLDM the Swift 2.3 modules fail to load (in to the Swift 3.0 runtime).
Forcing the app to use Swift 2.3 (or legacy Swift as Apple call it), fixes the issue.
Swift apps have this setting exposed by Xcode but you have to manually add it for an Objective-C app.
Further Advice
Port your Swift 2.3 code to Swift 3.0 as soon as possible, Apple won't support 2.x for very long.
In my case, I had to remove Objective-C Bridging header from build settings file. My Bridging header file did not do anything.. so it was okay.
I ran into the error in auto-import: failed to get module 'XYZ' from AST context message while attempting to debug in an 9.3 simulator. Switching to a 10.2 simulator resolved the issue.
I ran into this when building a framework with Carthage. Trying to debug would print the error, which referenced an Obj-C framework dependency.
I found this blog post which suggested adding a the following user-defined build setting to my project:
For Debug: REMOVE_HEADERS_FROM_EMBEDDED_BUNDLES = NO
For Release: REMOVE_HEADERS_FROM_EMBEDDED_BUNDLES = YES
This fixed the issue for me.
Try to restart Xcode. In my case it resolved an issue.
In my case there was a compiler error in the "C" code which was reported in LLDB, after fixing the error LLDB started working again.
As suggested by Tim https://stackoverflow.com/a/41876400/1840269 the root cause to our problem was a matter of duplicates.
We had a obj-c wrapper category for SDWebImage that was used from both obj-c and Swift.
When Importing the category from Swift everything blew up because of redefinition/duplicate import since the SDWebImage pod already exposed it self as a Swift module.
The solution? We re-implemented the obj-c category as a Swift extension - and kept using it from both Swift and obj-c by adding #objc in front of the extension and importing the #import "product-Swift.h" file from obj-c.
And maybe start with checking: https://developer.apple.com/library/content/qa/qa1947/_index.html.
I just took 2 days for me to resolve and debugging through print command all the time. Now I got the issue:
My project is in Swift and I was using objective-C library same as imported in pods for swift(old Objective-C project merged in this one) and was using import IQKeyboardManager as well as import "IQKeyboardManager.h" header as well. That conflicts and deleting the header one resolved my issue finally.
You can check with this type of duplicity in libraries.

Make headers of external Objective-C libraries available in Swift syntax

In my Swift project, I'd like to use RestKit and AFNetworking.
If I import them using CocoaPod as a static library and if I Cmd+Click on a class that I use, I can only see the header of this class in its Objective-C representation.
However my fellow developers only understand Swift code, but no Objective-C code. Therefore I like to import RestKit and AFNetworking in a way, that Cmd+Clicking shows the Swift representation of the header file, like it is for Apple frameworks.
Is there a possibility to achieve this?
I found a workaround to the problem. The Swift representation of Objective-C header files is only available, if you import RestKit and AFNetworking as Cocoa Touch Framework and not as Cocoa Touch Static Library.
Unfortunately RestKit and AFNetworking currently only have targets for static libraries.
However you can create a Cocoa Touch Framework for them and copy all Objective-C files into the framework and make them public (be careful with some imports in header files, see e.g. https://github.com/AFNetworking/AFNetworking/pull/2206).
You have to create this Cocoa Touch Framework on your own, you cannot use CocoaPods in the current version. I hope this is possible in the future.

Resources