How can I use swift in Terminal? - xcode

I read What's new in Xcode 6. The article introduces some new feature about Xcode 6, and it says:
Command Line
Xcode’s debugger includes an interactive version of the Swift language, known as the REPL (Read-Eval-Print-Loop). Use Swift syntax to evaluate and interact with your running app or write new code in a script-like environment. The REPL is available from within LLDB in Xcode’s console, or from Terminal.
I want to know how to get the REPL?

sudo xcode-select -switch /Applications/Xcode.app/Contents/Developer
then you can do one of these:
xcrun swift
lldb --repl
As of Xcode 6.1 - typing swift in the terminal launches the REPL as well.

Alternatively, if you don't want to mess up your current dev environment, you can just run:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift

Step 1: Open Terminal
Step 2: Type "swift"
Step 3: There's no step 3
Example:
GoldCoast:~ macmark$ swift
Welcome to Swift! Type :help for assistance.
1> println("Hello, world")
Hello, world
2> var myVariable = 42
myVariable: Int = 42
3> myVariable = 50
4> let myConstant = 42
myConstant: Int = 42
5> println(myVariable)
50
6> let label = "The width is "
label: String = "The width is "
7> let width = 94
width: Int = 94
8> let widthLabel = label + String(width)
widthLabel: String = "The width is 94"
9> :exit
GoldCoast:~ macmark$

In Xcode 6.1.1 with Command Line Tools installed you can execute scripts by referencing directly to /usr/bin/swift the following way:
#!/usr/bin/swift
let variable: String = "string"
print("Test \(variable)")

In the same fashion as running Swift from the Terminal, you can also execute scripts.
Just use the following shebang, and run your script. (As per Chris Lattner, creator of Swift)
#!/usr/bin/env xcrun swift -i

If any one cares a simple Swift script shebang:
#!/usr/bin/env xcrun --sdk macosx swift
If specific target version is required
#!/usr/bin/env xcrun --sdk macosx swift -target x86_64-macosx10.11
If specific toolchain is required (like you want to use Swift 2.3 but you are using Xcode 8)
#!/usr/bin/env xcrun --toolchain com.apple.dt.toolchain.Swift_2_3 --sdk macosx swift -target x86_64-macosx10.11
If you want to use Swift 2.2 in your Xcode 7.3.1, let's assume Xcode 7.3.1 is located at /Applications/Xcode7.app
sudo xcode-select -s /Applications/Xcode7.app/
xcrun --sdk macosx swift
from now on the default active developer directory changed, you can check that using:
xcode-select -p
If you want to use snapshots provided by Swift.org, you should not miss Installation here.
as first answered by me in Run swift script from Xcode iOS project as build phase

** update as of xcode6 beta 4 **
this can also be done on xcode preferences. simply go to xcode -> preferences -> locations.
for command line tools simply select the version you want from drop down list options, refer picture below. (swift requires path to be xcode6's path).
I will leave my previous answer below as well.
what Kaan said and you can also use an apple script to make simple application so you can use it to switch back and forth.
open apple script > paste this below code & export it as an application so with just one click you can switch to default path or beta path (to use swift)
set xcode6Path to "xcode-select -switch /Applications/Xcode6-Beta.app/Contents/Developer"
set xcodeDefaultPath to "xcode-select -switch /Applications/Xcode.app/Contents/Developer"
display dialog "set xcode sdk path to " buttons {"xcode 6", "default"} default button 1
copy result as list to {buttonPressed}
if buttonPressed is "default" then
try
do shell script xcodeDefaultPath with administrator privileges
end try
else
try
do shell script xcode6Path with administrator privileges
end try
end if
then run > xcrun swift
disclaimer
the script assumes you have both xcode6-beta & xcode5 installed.
if you're a new developer who's trying out only xcode6beta you will not need any script or setting path manually. simply run xcrun swift as the path is already set for you.
when xcode6 is finally released you will need to reset your path back to default from this simple app and never use it again.

After installing the official Xcode 6.1 release, there is a swift command in /usr/bin/swift.
Bear in mind that if you have a Python different from the Apple-supplied Python in the path, swift can fail with ImportError: No module named site. In that case, make sure that you do export PATH=/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/sbin before calling swift.

The xcrun command will use the DEVELOPER_DIR environment variable to override the currently selected Xcode installation (as set with xcode-select). You can use that to construct a single command that'll run swift on the command line and put you in the REPL. That looks like this:
/usr/bin/env DEVELOPER_DIR=/Applications/Xcode6-Beta.app/Contents/Developer xcrun swift
and you can alias that to just 'swift':
alias swift="/usr/bin/env DEVELOPER_DIR=/Applications/Xcode6-Beta.app/Contents/Developer xcrun swift"
As an interesting side note, you can use the same kind of invocation to run a swift script just like you'd use bash or python by adding a -i:
#!/usr/bin/env DEVELOPER_DIR=/Applications/Xcode6-Beta.app/Contents/Developer xcrun swift -i
println("Hello World!")
Of course, once Xcode 6 is released officially and you switch to that as your default developer tools, you can drop the DEVELOPER_DIR=.. bits and just use "xcrun swift".

make sure you install xcode 6.0 ,but not 6.1
If you get an error:
<unknown>:0: error: the SDK 'MacOSX10.9.sdk' does not support Swift
just run
xcrun --sdk iphonesimulator8.0 swift
or you can
export SDKROOT="iphonesimulator8.0"
and then
xcrun swift
Use "xcodebuild -showsdks" to list the available SDK names.
if you install xcode 6.1,just
sudo xcode-select -s /Applications/*your-Xcode-6.1-path.app*/Contents/Developer
xcrun swift

For XCode6, run these commands:
$ sudo xcode-select -s /Applications/Xcode.app/Contents/Developer/
$ xcrun swift
If you get an error:
<unknown>:0: error: the SDK 'MacOSX10.9.sdk' does not support Swift
try:
xcrun swift -sdk /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator.sdk

open Terminal,
$sudo xcode-select -switch /Applications/Xcode6-Beta6.app/Contents/Developer
Notice: The Xcode6-Beta6.app should be replaced to appropriate version you installed
Then put this line alias swift='xcrun swift' to ~/.bash_profile
And,
$source ~/.bash_profile
$swift
There you go!

With the help of Swift REPL(Read Eval Print Loop).
Developers familiar with interpreted languages will feel comfortable in this command-line environment, and even experienced developers will find a few unique features
Launch Terminal.app and type swift and press enter. You’ll then be in the Swift REPL.
1> print("Hello Swift REPL")
Hello Swift REPL
2> 10 + 20
$R0: Int = 30
3> var name = "Yogendra Singh"
name: String = "Yogendra Singh"
4> print(name)
Yogendra Singh
5>

Related

Kulture error - kpm command not found

I recently installed Sublime Text 3 with the aim of coding some C# on the Mac. I then followed the steps on http://www.omnisharp.net for the installation of the plugins and everything works fine until I try to build a simple "HelloWorld" project. Then I get the error
/Users/guevara/Library/Application Support/Sublime Text 3/Packages/Kulture/build.sh: line 21: kpm: command not found
I did check that I have the ASP.NET 5 KVM for MAC installed. Following the steps at http://www.enterpriseframework.com/post/2014/12/02/how-to-mac-vnext-step-by-step-setup-and-first-app also works, so the kpm command has been installed through Homebrew.
Any idea what I am doing wrong or why Kulture can't find the kpm command?
Thanks,
Alex
Look at the last line of installing kvm on OS X instructions at https://github.com/aspnet/Home:
Run command source kvm.sh on your terminal if your terminal cannot understand kvm.
but with the beta3 there's an issue in Kulture's build.sh, I have updated 4 1st lignes:
ver=`cat ~/.k/alias/default.alias`
add_to_path=$HOME"/.k/runtimes/"$ver"/bin"
export PATH=$PATH:/usr/local/bin:$add_to_path
[ -s $HOME"/.k/kvm/kvm.sh" ] && . $HOME"/.k/kvm/kvm.sh"

Error when trying to run Swift from terminal

After entering
~ sudo xcode-select -switch /Applications/Xcode6-Beta3.app/Contents/Developer
and then
~ xcrun swift
I get an error
ImportError: No module named site
Assertion failed: (err == 0), function ~Mutex, file /SourceCache/lldb/lldb-320.4.106.2/source/Host/common/Mutex.cpp, line 246.
Abort trap: 6
What am I missing? Several tutorials online give this code, so I'm guessing it must be some sort of setup issue.
I had a similar error and resolved it by removing the entry to my 3rd-party Python distribution (Enthought Canopy, in my case) from the PATH environment variable. This was a manual hack in my Terminal session and is good for only that session but it was as simple as this:
~ [1]$ swift
ImportError: No module named site
~ [2]$ echo $PATH
/Users/tdiller/Library/Enthought/Canopy_64bit/User/bin:/usr/local/git/bin:~/bin:/Library/Frameworks/Python.framework/Versions/Current/bin:/opt/local/bin:/opt/local/sbin:/usr/local/texlive/2012/bin/universal-darwin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin:/usr/local/git/bin:/usr/local/go/bin
~ [3]$ PATH=PATH=/usr/local/git/bin:~/bin:/Library/Frameworks/Python.framework/Versions/Current/bin:/opt/local/bin:/opt/local/sbin:/usr/local/texlive/2012/bin/universal-darwin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin:/usr/local/git/bin:/usr/local/go/bin
~ [4]$ export PATH
~ [5]$ swift
Welcome to Swift version 1.2. Type :help for assistance.
1>
I'm not sure what in Canopy-64bit/User/bin/ intereferes with Swift, but removing that item on the path seems to do the trick if you want to use the Swift REPL. There doesn't seem to be a problem with the EPD-style entry (/Library/Frameworks/Python.framework...).
Of course, Canopy won't work from this Terminal session, but I'm not sure it's important to have Python REPL and Swift REPL working in the same session.
It should be
$ xcode-select -switch /Applications/Xcode6-Beta3.app/Contents/Developer
See this article for more 1. Learn Swift by running Scripts

Ruby compile failed while installing on Mountain Lion 10.8?

This is the command I entered in the terminal while following "Ruby on Rails development with Mac OS X Mountain Lion", and its result:
rbenv install 2.0.0-p247
Downloading openssl-1.0.1e.tar.gz...
-> https://www.openssl.org/source/openssl-1.0.1e.tar.gz
Installing openssl-1.0.1e...
BUILD FAILED
Inspect or clean up the working tree at /var/folders/x8/19f_wwjx4gd26z6qdq874tj40000gs/T/ruby-build.20130919211135.42003
Results logged to /var/folders/x8/19f_wwjx4gd26z6qdq874tj40000gs/T/ruby- build.20130919211135.42003.log
Last 10 log lines:
RANLIB =true
ARFLAGS =
PERL =/usr/bin/perl
SIXTY_FOUR_BIT_LONG mode
DES_UNROLL used
DES_INT used
RC4 uses uchar
RC4_CHUNK is unsigned long
sh: make: command not found
/usr/local/bin/ruby-build: line 387: make: command not found
Please help!
/usr/local/bin/ruby-build: line 387: make: command not found means rbenv can't compile Ruby because the necessary tools aren't available.
Have you installed Xcode?
Have you installed its associated command-line applications? That's the second step needed, and is found inside the Xcode application.
Have you recently updated Xcode? If so, you might need to update the command-line tools too.

xcodebuild cause fatal error but in Xcode, compilation is OK

I have a weird thing when I try to compile with xcodebuild.
If I open the project on the mac with XCode, the code compile without any warning.
If I use the following command line :
xcodebuild -configuration Debug -target myApp PROVISIONING_PROFILE=B5AD0E27-B224-4962-B0DC-XXXXXXXX
I have some compilation error :
/Users/myUser/.jenkins/jobs/myApp/workspace/prj/Controllers/DeclarerEtape1Adresse.m:75:6: error: receiver type 'DeclarerEtape1Adresse' for instance message does not declare a method with selector 'rechercheAdresse' [4]
[self rechercheAdresse];
^
1 error generated.
This errors can appear in external code (taken from github). It seems that the compilator is much strict in command line mode than with Xcode.
Is there any specific configuration in command line ?
Thank you for your feedback.
James, you're the boss :p I tried xcodebuild -version on both environment and the ci server used an old SDK 4.2.X. So I used the sudo xcode-select -switch /Applications/Xcode.app/Contents/Developer and it fixes the issue

XCode cannot create IPA

My application compiles OK, and verifies.
But each of the Organizer options Validate..., Share..., Submit... just fail silently.
I'm using XCode version 3.2.5
I found the Console application had some information:
30/12/10 13:51:27 Xcode[8458] Running /usr/bin/xcrun with (
"-sdk",
iphoneos,
PackageApplication,
"-v",
"/Users/xxxxxx/Library/Application Support/Developer/Shared/Archived Applications/C201D5C0-2AB4-494B-A560-806AE36EF9A7.apparchive/Xxxxxx.app",
"-o",
"/var/folders/52/528Jj01wGtKYzlqffjXrck+++TI/-Tmp-/0610AAC0-E549-4F07-9496-08EFD6DFCAC3-8458-0000F00C5DE14F9F/Xxxxxx.ipa",
"--sign",
"iPhone Distribution: Xxxxxx",
"--embed",
"/Users/xxxxxx/Library/MobileDevice/Provisioning Profiles/66403280-7962-4A73-92D1-8FF34F65866C.mobileprovision"
)
30/12/10 13:51:27 [0x0-0x439439].com.apple.Xcode[8458] sh: /Developer/usr/bin/xcodebuild: No such file or directory
30/12/10 13:51:27 [0x0-0x439439].com.apple.Xcode[8458] /Developer/usr/bin/xcodebuild fails with 32512 - Unknown error: 32512
After searching around for a while, it appears that my install was missing the directory /Developer/usr/bin/.
In a Terminal shell, I linked the latest XCode version directory:
ln -s /Developer/XCode_3.2.5/usr/ /Developer/usr
and it fixed the problem.
You should try to package your IPA file through command line, so you can have full log in it. Here is a post I wrote, check it out.
http://www.nanaimostudio.com/blog/2011/4/17/xcode-build-and-archive-sharing-problem-and-solution.html

Resources