gpt4 book ai didi

string - Swift - 无法理解 float 到字符串的处理

转载 作者:行者123 更新时间:2023-11-28 09:20:16 25 4
gpt4 key购买 nike

我一直在尝试使用 Apple 的新 Swift 语言在 X-Code 中进行 OS X 开发。我觉得我好像理解了基础知识,并且通读了指南至少对我来说它看起来很像 Python。但是,即使是最基本的算法,我也很难整理出来。

我尝试为计算器风格的应用程序创建一个简单的加法函数。我没有看到任何错误,它构建得很好,但对我不起作用……我错过了什么??

import Cocoa

class AppDelegate: NSObject, NSApplicationDelegate {

@IBOutlet var window: NSWindow

@IBOutlet var InputA : NSTextField

@IBOutlet var InputB : NSTextField

@IBOutlet var Ouptut : NSTextField

func applicationDidFinishLaunching(aNotification: NSNotification?) {
// Insert code here to initialize your application
}

func applicationWillTerminate(aNotification: NSNotification?) {
// Insert code here to tear down your application
}

@IBAction func Addition(sender : AnyObject) {
var result : Float = InputA.floatValue + InputB.floatValue
Ouptut.stringValue = String(result)

}

@IBAction func Subtraction(sender : AnyObject) {
}

@IBAction func Division(sender : AnyObject) {
}

}

最佳答案

您可以使用格式从 Float 构造一个新字符串,如下所示:

Ouptut.stringValue = String(format: "%.4f", result)

或者通过字符串插值,像这样:

Ouptut.stringValue = "\(result)"

关于string - Swift - 无法理解 float 到字符串的处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25276298/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com