xcode - 在 Swift 2 中使用#

标签 xcode swift servicestack

嗨,我的 friend 把他客户的现有项目给了我,但它有太多错误。我一直在调试应用程序,刚好看到这行代码

class func saveFile(#data: NSData, filename: String, directory: NSSearchPathDirectory = .DocumentDirectory) -> Bool {
    var file = filePath(filename, directory: directory)
    return data.writeToFile(file, atomically: true)
}

注意到 # 了吗?那么#到底是什么?

这也是 # 函数的截图。

附加信息:我认为他们使用了这个库 Service Stack我认为它仅适用于 xamarin。

最佳答案

在 Swift 1 中,# 用于为函数参数提供相同的外部和内部名称。例如函数定义:

func save(#data: Float) {
    print(data)
}

相当于:

func save(data data: Float) {
    print(data)
}

这在 Swift 2 中被删除,外部名称必须显式声明。


外部参数名称用于使函数调用更加地道。例如:

func send(sender: String, receiver: String) {
    print("Sending from \(sender) to \(receiver)")
}

是这样调用的:

send("Cupertino", "New York")

通过添加外部参数,您可以在不更改主体的情况下使该函数调用更加地道:

func send(from sender: String, to receiver: String) {
    print("Sending from \(sender) to \(receiver)")
}

使代码更具可读性:

send(from: "Cupertino", to: "New York")

更多信息在 Apple docs .

关于xcode - 在 Swift 2 中使用#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36407401/

相关文章:

swift - 从展开中保存更改。与 <返回一起工作

ssl - ServiceStack SelfHost SSL 支持

c# - 服务堆栈 (REST) SOAP 和 WSDL 不工作

xcode - 如何在不包含 Mac OS X 10.6 SDK 的 Xcode 版本中构建它

ios - Firebase Firestore 多个查询

ios - 从 UIWebView 进行 PDF 分页

swift - Swift 中条件一致性的解决方法(原为 : Adding protocol to a constrained generic types)

redis - 防止servicestack RedisClient中的属性序列化

ios - 本地址栏中只有前缀时,使 iphone webview 转到不同的网址

iphone - 通过 CMake 构建时如何指定通用 iOS 应用程序?