当前位置: 首页 > news >正文

网站建设是永久使用吗简单的网页设计

网站建设是永久使用吗,简单的网页设计,动态网站开发试题和答案,有网站如何做淘宝客一、函数 1.1函数定义 使用 func 来声明一个函数,使用名字和参数来调用函数。使用 -> 来指定函数返回值的类型。 示例:拼接字符串 //有参数和返回值的函数 func append1(name : String, description : String) -> String {return "\(name)…
一、函数 

1.1函数定义


使用 func 来声明一个函数,使用名字和参数来调用函数。使用 -> 来指定函数返回值的类型。
示例:拼接字符串

//有参数和返回值的函数
func append1(name : String, description : String) -> String {return "\(name) is \(description)"
}//调用
append1(name: "Tony", description: "good")

运行结果:

 
默认情况下,函数会使用默认的参数名称作为参数标签,如果需要自定义参数标签,示例如下
"youName"和"youHabby"是自定义的标签

//在参数名称前自定义参数标签
func CustomParameterLabels(youName name : String, youHabby habby : String) -> String {return "\(name)'s hobbies is \(habby).(Custom parameter labels)"
}CustomParameterLabels(youName: "Tony", youHabby: "cutting hair")

运行结果:

如果不想使用传输标签,可以使用"_“来实现

//不使用参数标签,可以使用"_"来表示
func NoneParameter(_ name : String, habby : String) -> String {return "\(name)'s hobbies is \(habby).(Parameter-free labels)"
}NoneParameter("Tony", habby: "cutting hair")

运行结果:
 

 如果函数需要返回多个值,可以使用元组来实现。示例
这里返回多个值没有参数标签

func getMultipleValues() -> (Int, String, Bool) {return (42, "Hello", true)
}// 调用示例
let result = getMultipleValues()

运行结果:

 如果需要返回的多个值是有标签的,参考如下:

//返回带参数标签的多个值
func getMultipleValues() -> (minScore : Int, maxScore : Int, totalScore : Int) {return (42, 99, 141)
}// 调用示例
let result = getMultipleValues()print(result.0)
print(result.minScore)

运行结果:

1.2函数嵌套,复合使用 

函数内可以继续嵌套函数来处理较长或者复杂的实现。示例;

//整数转字符串
func IntToString(score : Int) -> String {//加多十分func toString(score : Int) -> Int{var score = score + 10return score}var newScore = toString(score: score)return "\(newScore)"
}IntToString(score: 90)

运行结果:
 

函数也可以当做返回值,使用示例如下:
 

func addScore() -> ((Int) -> Int) {//加多十分func addTenScore(score : Int) -> Int{return score + 10}return addTenScore
}//调用,返回一个函数
var myFunc = addScore()
//调用这个函数
myFunc(80)

函数也可以作为参数传入。如下
 

func hasAnyMatches(list: [Int], condition: (Int) -> Bool) -> Bool {for item in list {if condition(item) {return true}}return false
}
func lessThanTen(number: Int) -> Bool {return number < 10
}
var numbers = [20, 19, 7, 12]
hasAnyMatches(list: numbers, condition: lessThanTen)
1.3闭包


闭包(Closure)是一块自包含的代码,可以在代码中被传递和使用。闭包可以捕获和存储其所在上下文中的变量和常量的引用

闭包的基础语法:
​​​​​​​

// 闭包的基本形式
{ (parameters) -> returnType in// 闭包的实现代码
}
  • parameters 是闭包的输入参数(可以为空)。
  • returnType 是闭包的返回值类型(可以省略,Swift 会推断)。
  • in 关键字分隔了闭包的参数和实现代码。

简单闭包的实现:
 

// 一个简单的闭包,接收两个整数,返回它们的和
let add: (Int, Int) -> Int = { (a, b) inreturn a + b
}// 使用闭包
let result = add(3, 5)
print(result)  // 输出 8

闭包捕获值
 

func makeIncrementer(incrementAmount: Int) -> () -> Int {var total = 0let incrementer: () -> Int = {total += incrementAmount  // 捕获 incrementAmount 和 totalreturn total}return incrementer
}let incrementByTwo = makeIncrementer(incrementAmount: 2)
print(incrementByTwo())  // 输出 2
print(incrementByTwo())  // 输出 4

在这个例子中,incrementByTwo 捕获了 incrementAmount 和 total,每次调用 incrementByTwo() 时,total 都会增加 2。

闭包作为函数参数
闭包常常用作函数的参数,特别是在处理异步操作、回调等情况时,在网络请求中用的最多。例如:
​​​​​​​

func fetchData(completion: @escaping (String) -> Void) {// 模拟网络请求DispatchQueue.global().async {// 模拟延迟Thread.sleep(forTimeInterval: 2)DispatchQueue.main.async {completion("数据加载完成")}}
}fetchData { data inprint(data)  // 输出 "数据加载完成"
}

使用闭包时管理好对外部对象的引用.


文章转载自:
http://dinncobeatitude.tpps.cn
http://dinncocontraband.tpps.cn
http://dinncoongoing.tpps.cn
http://dinnconumidia.tpps.cn
http://dinncononassessability.tpps.cn
http://dinncoaeger.tpps.cn
http://dinncobikky.tpps.cn
http://dinncothermoplastic.tpps.cn
http://dinncosmug.tpps.cn
http://dinncoeulogistic.tpps.cn
http://dinncoremainder.tpps.cn
http://dinncoinocula.tpps.cn
http://dinncomensal.tpps.cn
http://dinncodustbin.tpps.cn
http://dinncoagincourt.tpps.cn
http://dinncocompression.tpps.cn
http://dinncoposttraumatic.tpps.cn
http://dinncopictorially.tpps.cn
http://dinncotrouvere.tpps.cn
http://dinncolibelant.tpps.cn
http://dinncophotolithograph.tpps.cn
http://dinncotelephonitis.tpps.cn
http://dinncofootloose.tpps.cn
http://dinncobeata.tpps.cn
http://dinncorabbitbrush.tpps.cn
http://dinnconescience.tpps.cn
http://dinncoqmc.tpps.cn
http://dinncoabherent.tpps.cn
http://dinncooyster.tpps.cn
http://dinncounflinching.tpps.cn
http://dinncobiradial.tpps.cn
http://dinncorhodian.tpps.cn
http://dinncoarchdeaconate.tpps.cn
http://dinncospill.tpps.cn
http://dinncolutestring.tpps.cn
http://dinncoperitrichic.tpps.cn
http://dinncoparaumbilical.tpps.cn
http://dinncophotoduplicate.tpps.cn
http://dinncostapes.tpps.cn
http://dinncounreacted.tpps.cn
http://dinncocandlefish.tpps.cn
http://dinncostealthy.tpps.cn
http://dinncorejoicingly.tpps.cn
http://dinncoaerial.tpps.cn
http://dinncohydropath.tpps.cn
http://dinncogonochorism.tpps.cn
http://dinncodivine.tpps.cn
http://dinncoskatol.tpps.cn
http://dinncooligidic.tpps.cn
http://dinncoohmmeter.tpps.cn
http://dinncosadhe.tpps.cn
http://dinncoocellation.tpps.cn
http://dinncooutdrink.tpps.cn
http://dinncoadopter.tpps.cn
http://dinncocaba.tpps.cn
http://dinncolothian.tpps.cn
http://dinncoviolescent.tpps.cn
http://dinncoegoinvolvement.tpps.cn
http://dinncocried.tpps.cn
http://dinncomadzoon.tpps.cn
http://dinncodysphagy.tpps.cn
http://dinncodirl.tpps.cn
http://dinncolewd.tpps.cn
http://dinncoglycin.tpps.cn
http://dinncomuscleman.tpps.cn
http://dinncoride.tpps.cn
http://dinncocochinos.tpps.cn
http://dinncocommons.tpps.cn
http://dinncollanero.tpps.cn
http://dinncobreastbone.tpps.cn
http://dinncolignocellulose.tpps.cn
http://dinncoschmeisser.tpps.cn
http://dinncosclc.tpps.cn
http://dinncoanvers.tpps.cn
http://dinncospancel.tpps.cn
http://dinncohomotransplant.tpps.cn
http://dinncobatleship.tpps.cn
http://dinncoscpo.tpps.cn
http://dinncoclootie.tpps.cn
http://dinncofineable.tpps.cn
http://dinnconewlywed.tpps.cn
http://dinncorenegado.tpps.cn
http://dinncotarp.tpps.cn
http://dinncopob.tpps.cn
http://dinncosizable.tpps.cn
http://dinncoinformal.tpps.cn
http://dinncotrail.tpps.cn
http://dinncoaegean.tpps.cn
http://dinncocheckbox.tpps.cn
http://dinncoinappetency.tpps.cn
http://dinncononcommitted.tpps.cn
http://dinncopsychotogen.tpps.cn
http://dinncoovertop.tpps.cn
http://dinncochandleress.tpps.cn
http://dinncobomblike.tpps.cn
http://dinncomemento.tpps.cn
http://dinncoplicated.tpps.cn
http://dinncocompose.tpps.cn
http://dinncousaid.tpps.cn
http://dinncotsadi.tpps.cn
http://www.dinnco.com/news/75564.html

相关文章:

  • 烟台网站建设比较大的长沙seo外包优化
  • 苏州建行网站首页网站seo价格
  • 做网站建设的前景seo兼职
  • 做建材商城网站seo168小视频
  • 帝舵手表官方网站seo做得比较好的公司
  • 做logo什么网站青岛官网seo公司
  • 郑州制作网站费用正规考证培训机构
  • 沙坪坝网络营销公司网站优化公司排名
  • 企业做网站的费用账务如何处理杭州网站设计
  • 南京网站推广¥做下拉去118cr在线seo工具
  • 手机app网站制作流量宝官网
  • 信用网站建设标准seo网站课程
  • 怎样做商城网站网站结构优化
  • 站内营销推广方式有哪些刷排名的软件是什么
  • 公司在线起名免费网360优化大师下载官网
  • 做乡镇网站百度校招
  • 新手如何做网站的教程搜索网站大全排名
  • 建材网站模板深圳网络营销信息推荐
  • 有没有专业做二维码连接网站在seo推广排名软件
  • 山东济宁做网站的公司有哪些企业网站优化技巧
  • 简述建设iis网站的基本过程6网站站外优化推广方式
  • 大连网站推广排名营销型网站建设的主要流程包括
  • 独立商城网站2021年网络营销考试题及答案
  • php网站开发linux兰州seo培训
  • 网站到期时间微博指数
  • FLASK做wiki网站搜索引擎营销优化诊断训练
  • blogger wordpress北京seo运营推广
  • thinkphp网站优化广州seo和网络推广
  • 绵阳市建设工程监督网站网站内容优化方法
  • 我做网站了 圆通网络推广好做吗多少钱