Importing Swift Files into other Swift Files in Xcode6 - xcode

Let's say I have a file called myAppDelegate.swift and another called myWindowController.swift. How do I import myWindowController into myAppDelegate, because currently if I just type
import myWindowController
It says that it can't recognize that module. What's the step-by-step process for importing swift files into other swift files in Xcode6?

You don't need to import swift files in the same target into each other. The import statement is used for importing entire modules, not individual files. myWindowController should be accessible from myAppDelegate without needing to import.

Related

Xcode sometimes doesn't require imports at the top of the file

I am faced with a strange issue. I recently rebuilt my Xcode project file using a tool called Xcode gen, I now have hundreds of errors in my files related to imports, for example:
import Foundation
struct BillingHistoryDetailTableCellViewModel {
let contentColor: UIColor?
// Implementation
}
error: Cannot find type 'UIColor' in scope
Why am I suddenly faced with this error now? Xcode had no issue compiling this kind of file before.
Is there some kind of setting or flag required to fix this?
Answering this for others:
If your project happens to include objective-c code and the bridge file includes imports to frameworks that use UIKit then this will be imported globally across your project, hence Xcode not asking for the import in files where you reference objects in UIKit.
This seems kinda like a bad side effect of using an objective c bridge as it's easy to miss the adding the import statements.

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.

UIKit importing in Struct definition [duplicate]

After upgrading Xcode to 7.3, I just found that some modules are stricken out while importing, like in this screenshot:
But after adding the module (so just pressing enter) everything is fine. What does that mean? The module here is written in Swift and works fine.
This is a bug. We have fixed it in 218010af, which should be included in the Swift 2.2.1 release and is included in the 2016-04-12-a developer snapshot.
The strikethrough occurs if you try to import a module that has already been imported by your file or module:
In this case, I have already imported Foundation (which implicitly imports CoreFoundation), so Xcode is telling you that there is no need to import either module again.
It usually happens when a framework is already imported by any other framework you have already been imported.
For example, UIKit is already imported with Foundation so you don't need to import it manually.
I changed the order of the imports
import Foundation
import UIKit
import LayerKit
import Atlas < red line
import Foundation
import UIKit
import Atlas
import LayerKit
Some notes that may be causing it:
LayerKit importing Atlas even though LayerKit is the lower level API
Atlas is the UI layer
Both were Cocoapod imports
Error appeared when I created an 2nd Schema for App Store/Enteprise
releases and cleaned one and switch to the other.
Deleting Derived
Data didnt clear it.
So tried rearranging them and red line disappeared

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

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

Using DbLinq in IronPython

I'm trying to use dblinq in my IronPython application but am having some problems getting started. When trying to import dblinq classes,using clr.AddReference() it does not seem to see dblinq.
I can build but not access the library
import clr
clr.AddReference("DbLinq")
clr.AddReference("System.Data.Linq")
from System.Data.Linq import DataContext
exit = raw_input("press any key to quit")
1:
My dblinq source is in a subfolder called Resources
clr.AddReferenceToFileAndPath("Resources/DbLinq.dll") => file not found
clr.AddReference("DbLinq") => could not add reference to dblinq
clr.AddReferenceToFileAndPath("C:/Development/DBLinq2/Dblinq.dll" => success
but still cannot use the classes using from DbLinq import ...
Solution
import clr
clr.AddReferenceToFileAndPath("%s\Resources\DbLinq.dll" %os.getcwd())
clr.AddReference("DbLinq")
from Npgsql import *
from DbLinq import Data
For libraries like DbLinq that come with dependencies or are split into several DLLs/modules you can programmatically add their location to allow the runtime to find all required files.
import sys
sys.path.append(r"C:\Temp\DbLinq-0.20.1")
You can then reference all required assemblies as usual:
clr.AddReference("DbLinq")
If there is one primary assembly you can also use clr.AddReferenceToFileAndPath (with an absolute path) which will implicitly add the referenced assemblies location to the path.
clr.AddReference(r"C:\Temp\DbLinq-0.20.1\DbLinq.dll")

Resources