你能用Swift作为一种web编程语言吗?

时间:2021-12-14 15:05:59

Could you technically use Swift as a programming language for building a website / web app?

你能在技术上使用Swift作为构建网站/ web应用的编程语言吗?


Update: Looks like a lot of people are working on this.

更新:看起来很多人都在做这个。


Official support from Apple: https://swift.org/server-apis/

苹果官方支持:https://swift.org/server-apis/

7 个解决方案

#1


48  

Theoretically, any program that can output plain text can be used for CGI (Common Gateway Interface), that includes Swift. So, yes, you can use it for web programming. But there is no extra facilities as in PHP or EJB/JSP to make it easier for you. May be you can start making these facilities by yourself and be the first one to do so.

理论上,任何可以输出纯文本的程序都可以用于CGI(公共网关接口),包括Swift。所以,是的,你可以将它用于web编程。但是,在PHP或EJB/JSP中没有额外的功能来简化您的工作。也许你可以自己动手做这些设施,成为第一个这样做的人。

#2


25  

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.

我不确定这个主题现在有多活跃,但是如果将来有人发现这个话题,我想在这里宣布,我已经开始研究用Swift编写FastCGI应用程序的框架。

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...

当然,您可以使用任何Objective-C web框架(尽管相当少),当然也可以使用Swift作为脚本语言来转储HTML字符串以供CGI反编译。然而,(我将在这里得到所有哲学上的东西)我认为,如果我敢用这个词的话,接受这样一种方法并不是非常“迅速”的。斯威夫特的设计师们经历了很多麻烦,以确保它不仅仅是“只是”objc——没有C(事实上,我几乎希望他们没有说过,因为它经常被错误引用)。Swift也比objc更接近功能家族(同时仍然保留了实体对象的方向,并管理添加了python风格的脚本)。这为我认为需要探索的设计选择开辟了一个全新的世界。另外,类型安全……但我跑题了…

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

所有这一切都说明,我自己创建了一个模块化的“微框架”,可以让您在非常高的级别上用Swift编写web应用程序。在撰写本文时,我将它归类为概念的功能证明,但它仍然没有我想要的那么抽象,并且缺少了许多在现实生活中需要的关键特性。捐款当然是受欢迎的。希望有人能从中受益。https://github.com/ianthetechie/SwiftCGI

#3


23  

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):

作为一种web编程语言,Swift的工作非常完美。您可以执行python,比如脚本(用于快速开发或简单任务),然后使用XCode(用于速度)编译相同的代码:

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...

将以下脚本复制到Mac上的/Library/WebServer/CGI-Executables/TestCGI (OSX Mavericks或Yosemite中带有Xcode 6的Yosemite)。x安装)。使用chmod +x TestCGI使其可执行。使用httpd启动调用http://localhost/cgi-bin/TestCGI。您将得到调用参数回显……

#!/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.

苹果现在(12/03/15)开放了Foundation库(https://github.com/apple/swift corelibs-foundation)的源代码,允许在Linux服务器上进行编译。

#4


3  

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.

是的,你可以用Swift创建web应用。Tailor是允许您这样做的web框架之一。它的源代码在Github上。

#5


2  

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.

斯威夫特做的和objective - ec一样,但更容易。因此,您可以使用一个非常简单的CGI-Interface(参见http://www.sveinbjorn.org/objectivecgi)来分析和回答服务器端的客户端请求(包括GET和POST)。

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.

由于Swift在字符串处理方面很强,因此在抽象方面,它介于ObjectiveC和PHP/Perl之间。对于许多用途,甚至不需要SQLite或MySQL frontend。只需将数据放入服务器的某个文本文件中即可。

#6


1  

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

根据其他的答案,你可以使用Apple Swift作为网站/应用程序实现的一部分。

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.

此外,其他公司也在开发Swift语言编译器,以进一步扩展其功能。例如,RemObjects最近宣布在其元素编译器的下一个主要版本中,他们将引入一个新的编译器前端,称为“Silver”,这是一个Swift编译器。

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.

作为现有编译器技术的前端,该技术已经支持ObjectPascal (Oxygene)和c# (Hydrogene),这意味着您将能够使用Swift语言开发. net和Java应用程序(包括Android)。对于。net支持,这显然意味着您可以使用Swift来创建ASP。网的网站/应用程序。

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.

注意,这是每个平台的本机编译器——不涉及源代码转换/交叉编译、平台抽象或兼容框架,就像Xamarin(例如)一样。RemObjects编译器生成特定于平台的本机代码(例如。net的MSIL, JVM的Java字节码等)。它还允许开发人员本地使用平台库。例如,在为Cocoa开发时,Cocoa类会被直接暴露到语言中,Java类也会被暴露在Java中,。net框架也会被暴露在。net中。

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).

无论您是使用ObjectPascal、c#还是Swift语言前端(如果您愿意,您可以在项目中混合和匹配语言),都是如此。

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

8.0版本已经处于测试阶段一段时间了,所以被许可方已经在使用它了。:)

#7


1  

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

在可可与爱的文章中,您可以找到一个HTTP服务器的示例。它是用object -c编写的:http://www.cocoawithlove.com/2009/07/simple- extensiblehttp - serverin cocoa.html

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

我尝试创建一个swift版本: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

浏览网页时,我发现了非常好的HTTP server的swift版本。值得一看:https://github.com/glock45/swifter

#1


48  

Theoretically, any program that can output plain text can be used for CGI (Common Gateway Interface), that includes Swift. So, yes, you can use it for web programming. But there is no extra facilities as in PHP or EJB/JSP to make it easier for you. May be you can start making these facilities by yourself and be the first one to do so.

理论上,任何可以输出纯文本的程序都可以用于CGI(公共网关接口),包括Swift。所以,是的,你可以将它用于web编程。但是,在PHP或EJB/JSP中没有额外的功能来简化您的工作。也许你可以自己动手做这些设施,成为第一个这样做的人。

#2


25  

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.

我不确定这个主题现在有多活跃,但是如果将来有人发现这个话题,我想在这里宣布,我已经开始研究用Swift编写FastCGI应用程序的框架。

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...

当然,您可以使用任何Objective-C web框架(尽管相当少),当然也可以使用Swift作为脚本语言来转储HTML字符串以供CGI反编译。然而,(我将在这里得到所有哲学上的东西)我认为,如果我敢用这个词的话,接受这样一种方法并不是非常“迅速”的。斯威夫特的设计师们经历了很多麻烦,以确保它不仅仅是“只是”objc——没有C(事实上,我几乎希望他们没有说过,因为它经常被错误引用)。Swift也比objc更接近功能家族(同时仍然保留了实体对象的方向,并管理添加了python风格的脚本)。这为我认为需要探索的设计选择开辟了一个全新的世界。另外,类型安全……但我跑题了…

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

所有这一切都说明,我自己创建了一个模块化的“微框架”,可以让您在非常高的级别上用Swift编写web应用程序。在撰写本文时,我将它归类为概念的功能证明,但它仍然没有我想要的那么抽象,并且缺少了许多在现实生活中需要的关键特性。捐款当然是受欢迎的。希望有人能从中受益。https://github.com/ianthetechie/SwiftCGI

#3


23  

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):

作为一种web编程语言,Swift的工作非常完美。您可以执行python,比如脚本(用于快速开发或简单任务),然后使用XCode(用于速度)编译相同的代码:

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...

将以下脚本复制到Mac上的/Library/WebServer/CGI-Executables/TestCGI (OSX Mavericks或Yosemite中带有Xcode 6的Yosemite)。x安装)。使用chmod +x TestCGI使其可执行。使用httpd启动调用http://localhost/cgi-bin/TestCGI。您将得到调用参数回显……

#!/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.

苹果现在(12/03/15)开放了Foundation库(https://github.com/apple/swift corelibs-foundation)的源代码,允许在Linux服务器上进行编译。

#4


3  

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.

是的,你可以用Swift创建web应用。Tailor是允许您这样做的web框架之一。它的源代码在Github上。

#5


2  

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.

斯威夫特做的和objective - ec一样,但更容易。因此,您可以使用一个非常简单的CGI-Interface(参见http://www.sveinbjorn.org/objectivecgi)来分析和回答服务器端的客户端请求(包括GET和POST)。

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.

由于Swift在字符串处理方面很强,因此在抽象方面,它介于ObjectiveC和PHP/Perl之间。对于许多用途,甚至不需要SQLite或MySQL frontend。只需将数据放入服务器的某个文本文件中即可。

#6


1  

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

根据其他的答案,你可以使用Apple Swift作为网站/应用程序实现的一部分。

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.

此外,其他公司也在开发Swift语言编译器,以进一步扩展其功能。例如,RemObjects最近宣布在其元素编译器的下一个主要版本中,他们将引入一个新的编译器前端,称为“Silver”,这是一个Swift编译器。

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.

作为现有编译器技术的前端,该技术已经支持ObjectPascal (Oxygene)和c# (Hydrogene),这意味着您将能够使用Swift语言开发. net和Java应用程序(包括Android)。对于。net支持,这显然意味着您可以使用Swift来创建ASP。网的网站/应用程序。

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.

注意,这是每个平台的本机编译器——不涉及源代码转换/交叉编译、平台抽象或兼容框架,就像Xamarin(例如)一样。RemObjects编译器生成特定于平台的本机代码(例如。net的MSIL, JVM的Java字节码等)。它还允许开发人员本地使用平台库。例如,在为Cocoa开发时,Cocoa类会被直接暴露到语言中,Java类也会被暴露在Java中,。net框架也会被暴露在。net中。

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).

无论您是使用ObjectPascal、c#还是Swift语言前端(如果您愿意,您可以在项目中混合和匹配语言),都是如此。

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

8.0版本已经处于测试阶段一段时间了,所以被许可方已经在使用它了。:)

#7


1  

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

在可可与爱的文章中,您可以找到一个HTTP服务器的示例。它是用object -c编写的:http://www.cocoawithlove.com/2009/07/simple- extensiblehttp - serverin cocoa.html

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

我尝试创建一个swift版本: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

浏览网页时,我发现了非常好的HTTP server的swift版本。值得一看:https://github.com/glock45/swifter