简体   繁体   English

具有多个参数的Swift函数

[英]Swift Function with multiple parameters

I can't seem to figure out whats the issue here, 我似乎无法找出问题所在,

I have a swift code and I need it to gather multiple parameters. 我有一个快速代码,我需要它来收集多个参数。 Please help: 请帮忙:

func myBioData(myNameIs, myAgeIs) {
   println("Hello, my name is (myNameIs) and I am (myAgeIs) years old");
}

There are multiple things you are doing wrong within your code here. 您在此处的代码中有很多事情做错了。 To start off here is the right code and I will dissect it for you 从这里开始是正确的代码,我将为您剖析

func myBioData(myNameIs : String, var myAgeIs : Int) {
    println("Hello, my name is \(myNameIs) and I am \(myAgeIs) years old")
}
myBioData("Nick", 26)

First of all when you declare a parameter in Swift you need to explicitly state the type of the variable so you need to do something like this 首先,在Swift中声明参数时,您需要明确声明变量的类型,因此您需要执行以下操作

(myNameIs: String)

Second thing to notice is that in Swift if you do not explicitly say that it's a variable that you are passing in Swift assumes that you are passing in a constant. 要注意的第二件事是,在Swift中,如果您没有明确地说它是您要传递的变量,则Swift会假设您正在传递常量。 Depending on what you are trying to do it may make a difference so I just added one as a constant and other as a variable to simply show you 根据您尝试执行的操作,它可能会有所不同,因此我只添加了一个作为常量,另一个添加为变量只是为了向您展示

Lastly when you are trying to add variables between a println you need to add a "\\" before placing the variable/constant name so the syntax would be (myNameIs) would be right. 最后,当您尝试在println之间添加变量时,需要在放置变量/常量名称之前添加“ \\”,这样语法(myNameIs)才是正确的。 Also, you should not put a semicolon at the end of the statement 另外,您不应在语句末尾添加分号

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM