gpt4 book ai didi

swift - If Else 语句与选择器的链接

转载 作者:行者123 更新时间:2023-11-30 11:38:24 26 4
gpt4 key购买 nike

这里是初学者编码器。我对 Xcode 有疑问。我正在开发一个星座应用程序,并且有一个关于将我的 If Else 语句与选择器输入值链接起来的问题。

当前应用程序中的逻辑是,用户选择列出的日期,点击按钮,然后弹出一条警报消息,其中包含与其输入相关的特定副本。如何在警报消息中打印 if else 语句?

TLDR;需要帮助将函数打印值与警报窗口消息链接起来

这是当前外观的示例,我还在此处的第 3 行收到错误 [无法将类型 '()' 的值转换为预期参数类型 'String?']。预先感谢您!

   let message = signselection()


let alert = UIAlertController (title: "Your Astrological Sign is...", message:message, preferredStyle: .alert)

let okAction = UIAlertAction(title: "OK", style: .default) {(action) -> Void in}

func signselection () {
if (curMonth == 0) {
if(curDay! < 20) {
print("Capricorn Message")
} else {
print("Aquarius Message")
}
}

if (curMonth == 1){
if(curDay! < 19) {
print ("Aquarius Message")
} else {
print ("Pisces Message")
} }

if (curMonth == 2){
if(curDay! < 20) {
print ("Pisces Message")
} else {
print ("Aries Message")
} }

最佳答案

signselection() 方法仅打印消息,但它应该返回一个字符串。这就是为什么您会收到错误消息,指出它无法将 void“()”转换为 String。函数声明应该是:

func signSelection() -> String

而不是 print("Message") 你应该

return "Message" //where "Message" is each message you're currently printing

您还应该将 okAction 添加到警报中:

alert.addAction(okAction)

关于swift - If Else 语句与选择器的链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49483227/

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