importing a module in Swift - swift

I'm kicking the tires of Swift a little. I'm not using Xcode. I just have a simple .swift file and #!/usr/bin/env swift at the top of the file.
In the file, I have import Blahblah and a file called Blahblah in the same directory.
I get error: no such module 'Blahblah' however when running the .swift file.
How do I import the swift module? Is there something special I need to do?

Related

Importing SPM package in Objc file

I recently integrated SPM into my project and I am trying to import it an Objc .h file.
But Keep getting error of 'SideMenu/SideMenu-Swift.h' file not found
This is how I am importing it #import <SideMenu/SideMenu-Swift.h>
is there any other way to do this please?

ImportError: when importing from a local script library

Does importing from a local script lib work in qpython3?
I have a script I created in the Qpython projects3 directory of qpython.
projects3/MPU6502/
I have my main code here in this directory.
main.py
I have a local subdirectory,
projects3/MPU6502/tiny6502lib/
In this sub directory I have the source code,
RAM_8bit_Memory.py
From my main.py source code I load the library,
#-*-coding:utf8;-*-
#qpy:3
#qpy:console
# import 6502 library module
from tiny6502lib.RAM_8bit_Memory import *
when I execute the library import I get the ERRORL:
"ImportError: No module named tiny6502lib.RAM_8bit_Memory"
Is importing local script libraries broken in qpython3?
org.qpython.py/projects/MPU6502/main.py
org.qpython.py/projects/MPU6502/tiny6502lib/RAM_8bit_Memory.py
This code works perfectly on all other flavors of python3 (windows) and pythonista on ipad.
How do I get this import to work? Or does it not work at all?
thank you,
Just try to insert the following code before importing tiny6502lib.RAM_8bit_Memory
import sys
sys.path.append("/sdcard/qpython/projects3/MPU6502")
?

Swift 3: No such module 'os.log'

I was trying to compile a swift 3 file which contains nothing but:
import Cocoa
The compiler output was:
<unknown>:0: error: missing required module 'os.log'
So I edited the file to be:
import os.log
import Cocoa
And now the compiler output is:
test.swift:1:8: error: no such module 'os.log'
I suspect that the compiler is having trouble finding the legitimate module os.log. I should note that I'm editing the file in VIM, and the same program works fine in my Swift playground.
You need this declaration:
import os
source

Importing other libraries when using Swift as a scripting language

Heyo. I'm using Swift to make a simple web crawler for fun and practice. I made an Project.swift file and added it to a folder on my desktop. I now want to add SwiftyJSON to my project. I tried putting SwiftyJSON.swift in the same folder and adding import SwiftyJSON on top, but this did nothing. No import statement does not work at all. Is there any way to do this except pasting the whole file to the bottom of my project.swift file, or should I just stick to Python?
You have to start from the file named main.swift - that's your application entry point:
// main.swift
import Foundation
let data = "{\"message\" : \"Hello World\"}".dataUsingEncoding(NSUTF8StringEncoding)
let myJson = JSON(data:data!)
print(myJson["message"])
So you don't have to use include, but you'll need to specify all external dependencies when compiling:
swiftc SwiftyJSON.swift main.swift
./main
If you're using XCode Command Line Tool template, main.swift will be created for you by default, and you'd be able add more .swift files to your project and just use them, no need to use import.

Swift REPL: how to import, load, evaluate, or require a .swift file?

In the Swift REPL, how to import (a.k.a. load, evaluate, require) a typical text *.swift file?
I want to use the code from this file: ~/src/Foo.swift
Syntax like this doesn't work: import ~/src/Foo.swift
For comparison:
An equivalent solution in the Swift REPL for a framework is: import Foundation
An equivalent solution in the Ruby REPL for a *.ruby file is: require "~/src/foo"
These are similar questions that are /not/ what I'm asking:
How to use/make a Swift command-line script, executable, module, etc.
How to use/make an XCode playground, project, library, framework, etc.
How to launch the REPL with a pre-existing list of files.
You need to use -I, so if your modulename.swiftmodule file is in ~/mymodules than launch swift with
swift -I ~/mymodules
and then you will be able to import your module
import module name
Should be that easy
In swift,you can't import a typical *.swift file.
For Import Declaration can only be the following syntax:
“
‌ import-declaration → attributesopt import import-kindopt import-path
import-kind → typealias| struct| class| enum| protocol| var| func
‌ import-path → import-path-identifier| import-path-identifier.import-path
‌ import-path-identifier → identifier| operator”
From: Apple Inc. “The Swift Programming Language (Swift 2)”。 iBooks.
which can be described as these formats:
import module
import import kind module.symbol name
import module.submodule
import head.swift is incompatible with import import-kind module.symbol-name
Usually compile the files you want to import as a framework.Then it can be regarded as a module. use -F framework_directory/ to specify 3rd-party frameworks' search path.
Create a file. For example:
// test.swift
import headtest
print("Hello World")
open your terminal
goto the directory where you create the file.
execute command line
swift -F headtest test.swift
And done.
Simply insert the shebang line at the top of your script:
#!/usr/bin/env xcrun swift
You can copy/paste the source code into the repl and execute it.
Not ideal, obviously, but sometimes useful.
Now Swift REPL supports packages. We can do this by the following steps:
In Xcode, select menu File > New > Package. Choose a name for the package, for example MyLibrary.
Copy your codes or .swift files to the Sources/MyLibrary/ directory in your package.
Remember to make your interface public.
In the command line, go to the package directory and run REPL
Like this
cd MyLibrary/
swift run --repl
In the REPL, import your library
Like this
import MyLibrary
Now you can your codes in the REPL.
It looks like it's not possible to import file and get Xcode to use REPL for it using specifications you gave. I think you can still do it creating a proper framework, but it's not exactly what you was looking.