Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 271 Vote(s) - 3.52 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Swift REPL: how to import, load, evaluate, or require a .swift file?

#1
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.
Reply

#2
Now Swift REPL supports packages. We can do this by the following steps:

1. In Xcode, select menu File > New > Package. Choose a name for the package, for example MyLibrary.

2. Copy your codes or .swift files to the Sources/MyLibrary/ directory in your package.

3. Remember to make your interface `public`.

4. In the command line, go to the package directory and run REPL

Like this

cd MyLibrary/
swift run --repl

5. In the REPL, import your library

Like this

import MyLibrary

Now you can your codes in the REPL.
Reply

#3
You can copy/paste the source code into the repl and execute it.
Not ideal, obviously, but sometimes useful.
Reply

#4
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
Reply

#5
In swift,you can't import a typical *.swift file.<br>
----------


For ```Import Declaration``` can only be the following syntax:
>“
‌ import-declaration → attributes*opt* **import** import-kind*opt* import-path<br>
import-kind → typealias| struct| class| enum| protocol| var| func<br>
‌ import-path → import-path-identifier| import-path-identifier.import-path<br>
‌ import-path-identifier → identifier| operator”<br><br>
From: Apple Inc. “The Swift Programming Language (Swift 2)”。 iBooks.

which can be described as these formats:
>import ```module```<br>
import ```import kind``` ```module```.```symbol name```<br>
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.

0. Create a file. For example:

```// test.swift
import headtest
print("Hello World")
```

1. open your terminal
2. goto the directory where you create the file.
3. execute command line

```swift -F headtest test.swift```

And done.
Reply

#6
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.
Reply

#7
Simply insert the shebang line at the top of your script:

#!/usr/bin/env xcrun swift
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through