Can you use Swift as a web programming language?

Swift

Swift Problem Overview


Swift Solutions


Solution 1 - Swift

In theory, of course.

Any program that can output plain text can also be used for CGI(Common Gateway Interface) which includes Swift as well.

Therefore, yes, you can use Swift for web programming. However, currently, there are no additional libraries(like there are in PHP or EJB/JSP) to make this process easy for you. Take a look at some popular web frameworks for Swift such as Vapor.

Solution 2 - Swift

Not sure how active this topic is anymore, but in case anyone finds this in the future, I'd like to announce here that I have started working on a framework for writing FastCGI applications in Swift.

Of course you can use any Objective-C web framework (though there are rather few), and you can certainly use Swift as a scripting language to dump out HTML strings to be regurgitated by CGI. However, (I'm going to get all philosophical here) I think that settling for such an approach is not very "Swift-like" if I dare use the term. The Swift designers went through a lot of trouble to make sure that it's more than "just" Obj-C without the (constraints of) C (in fact, I almost wish they hadn't said that since it's so often misquoted). Swift is also much closer to the functional family than Obj-C (while still retaining solid object orientation AND managing to add Python-esque scriptability). This opens up a whole new world of design choices that I think need to be explored. Also, type safety... but I digress...

All this to say, I've taken it upon myself to create a modular "microframework" that will let you write web apps in Swift at a very high level. At the time of this writing, I'd classify it as a functional proof of concept, but it's still not as abstract as I'd like and is missing a number of key features that would be necessary for real-world use. Contributions are of course welcome. Hope somebody finds this helpful. https://github.com/ianthetechie/SwiftCGI

Solution 3 - Swift

Swift works just perfect as a web programming language. You can do python like scripting (for rapid development or simple tasks) and later compile the same code with XCode (for speed):

Copy the following script to /Library/WebServer/CGI-Executables/TestCGI on your Mac (OSX Mavericks or Yosemite with Xcode 6.x installed). Make it executable with chmod +x TestCGI. With httpd started call http://localhost/cgi-bin/TestCGI. You will get the call parameters echoed...

#!/usr/bin/env xcrun swift

import Foundation

print("Content-Type: text/html")
print("Content:")
print("")
print("1. Process.argument(s):<br />")
for s in Process.arguments {
    print(s + "<br />")
}
print("<br />")

let env: Dictionary = NSProcessInfo().environment

if let requestMethod = env["REQUEST_METHOD"] {
    print("2. Request method is: \(requestMethod)<br /><br />")
}

print("3. Number of environment variables: \(env.count)<br /><br />")

print("4. List environment:<br />")
for key in env.keys {
    print("\(key) == \(env[key]!)<br />")
}

Apple has now (12/03/15) open sourced the Foundation library (https://github.com/apple/swift-corelibs-foundation) to allow compilation on Linux Servers.

Solution 4 - Swift

Yes, you can.

Swift does the same as ObjectiveC, but does it easier. Therefore, you could use a very simple CGI-Interface (see http://www.sveinbjorn.org/objectivecgi) to analyse and answer client requests (both GET and POST) on the server side.

Since Swift is strong in string manipulation, it will be somewhere between ObjectiveC and PHP/Perl in terms of abstraction. For many purposes, a SQLite or MySQL frontend isn't even needed. Just drop your data into some text file on the server.

Solution 5 - Swift

Yes, you can create web apps in Swift. Tailor is one of the web frameworks which allows you to do that. Its source code is on Github.

Solution 6 - Swift

As per the other answers, you can use Apple Swift in any number of ways as part of a web site/app implementation.

Additionally however, other companies are also developing Swift language compilers which extend the capabilities even further. For example, RemObjects recently announced that in the next major release of their Elements compiler, they would be introducing a new compiler front-end dubbed "Silver", which is a Swift compiler.

As a front end onto their existing compiler technology which already supports ObjectPascal (Oxygene) and C# (Hydrogene), this means that you will be able to develop .NET and Java applications (including Android) using the Swift language. With .NET support, that obviously means you could use Swift to create an ASP.NET web site/app.

Note that this is a native compiler for each platform - there is no source code translation/cross compilation or platform abstraction or compatibility framework involved, as there is with (for example) Xamarin. The RemObjects compiler produces platform specific, native code (i.e. MSIL for .NET, Java Bytecode for JVM etc etc). It also allows the developer to consume platform libraries natively. i.e. when developing for Cocoa, Cocoa classes are exposed directly into the language, ditto Java classes when developing for Java, .NET framework for .NET etc.

This is the case whether you are using the ObjectPascal, C# or Swift language front-ends (and you can mix and match languages in a project, if you so desire).

The 8.0 release has been in beta for a while already so existing Elements licensee's have been playing with it already. :)

Solution 7 - Swift

In the article Cocoa with Love, you could find an example of HTTP server. It is written in Obj-C: http://www.cocoawithlove.com/2009/07/simple-extensible-http-server-in-cocoa.html

Here is my attempt to create a swift version: https://github.com/grzegorzleszek/HTTPSwiftServer

Browsing the web, I've found really nice swift version of HTTP server. It is worth to have a look: https://github.com/glock45/swifter

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBrian WeinreichView Question on Stackoverflow
Solution 1 - SwiftCommanderHKView Answer on Stackoverflow
Solution 2 - SwiftianthetechieView Answer on Stackoverflow
Solution 3 - SwiftKlausView Answer on Stackoverflow
Solution 4 - SwiftKlausView Answer on Stackoverflow
Solution 5 - SwiftniutechView Answer on Stackoverflow
Solution 6 - SwiftDelticsView Answer on Stackoverflow
Solution 7 - SwiftGreg LeszekView Answer on Stackoverflow