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

营销网站建设的规则辅导机构

营销网站建设的规则,辅导机构,网站运营策划ppt,湘潭网站建设 排名磐石网络一个朴实无华的目录 今日学习内容:1.Swift 泛型1.1泛型类型1.2扩展泛型类型1.3类型约束1.4关联类1.5Where 语句 2.Swift 访问控制2.1语句函数类型访问权限:元组的访问级别与元组中访问级别最低的类型一致子类的2.2访问级别不得高于父类的访问级别2.3常量…

一个朴实无华的目录

  • 今日学习内容:
    • 1.Swift 泛型
      • 1.1泛型类型
      • 1.2扩展泛型类型
      • 1.3类型约束
      • 1.4关联类
      • 1.5Where 语句
    • 2.Swift 访问控制
      • 2.1语句函数类型访问权限:元组的访问级别与元组中访问级别最低的类型一致子类的
      • 2.2访问级别不得高于父类的访问级别
      • 2.3常量、变量、属性、下标访问权限:不能拥有比它们的类型更高的访问级别。
      • 2.4Setter的访问级别可以低于对应的Getter的访问级别
      • 2.5构造器和默认构造器访问权限:在每个子类的 init() 方法前使用 required 关键字声明访问权限。
      • 2.6类型别名:任何你定义的类型别名都会被当作不同的类型,以便于进行访问控制。一个类型别名的访问级别不可高于原类型的访问级别。

今日学习内容:

1.Swift 泛型

1.1泛型类型

// 定义一个交换两个变量的函数
func swapTwoValues<T>(_ a: inout T, _ b: inout T) {let temporaryA = aa = bb = temporaryA
}var numb1 = 100
var numb2 = 200print("交换前数据:  \(numb1)\(numb2)")
swapTwoValues(&numb1, &numb2)
print("交换后数据: \(numb1)\(numb2)")var str1 = "A"
var str2 = "B"print("交换前数据:  \(str1)\(str2)")
swapTwoValues(&str1, &str2)
print("交换后数据: \(str1)\(str2)")

Stack 基本上和 IntStack 相同,占位类型参数 Element 代替了实际的 Int 类型。

以上实例中 Element 在如下三个地方被用作占位符:

创建 items 属性,使用 Element 类型的空数组对其进行初始化。
指定 push(_😃 方法的唯一参数 item 的类型必须是 Element 类型。
指定 pop() 方法的返回值类型必须是 Element 类型。

struct Stack<Element> {var items = [Element]()mutating func push(_ item: Element) {items.append(item)}mutating func pop() -> Element {return items.removeLast()}
}var stackOfStrings = Stack<String>()
print("字符串元素入栈: ")
stackOfStrings.push("google")
stackOfStrings.push("runoob")
print(stackOfStrings.items);let deletetos = stackOfStrings.pop()
print("出栈元素: " + deletetos)var stackOfInts = Stack<Int>()
print("整数元素入栈: ")
stackOfInts.push(1)
stackOfInts.push(2)
print(stackOfInts.items);

1.2扩展泛型类型

为其添加了一个名为 topItem 的只读计算型属性,它将会返回当前栈顶端的元素而不会将其从栈中移除:

struct Stack<Element> {var items = [Element]()mutating func push(_ item: Element) {items.append(item)}mutating func pop() -> Element {return items.removeLast()}
}extension Stack {var topItem: Element? {return items.isEmpty ? nil : items[items.count - 1]}
}var stackOfStrings = Stack<String>()
print("字符串元素入栈: ")
stackOfStrings.push("google")
stackOfStrings.push("runoob")if let topItem = stackOfStrings.topItem {print("栈中的顶部元素是:\(topItem).")
}print(stackOfStrings.items)
实例中 topItem 属性会返回一个 Element 类型的可选值。当栈为空的时候,topItem 会返回 nil;当栈不为空的时候,topItem 会返回 items 数组中的最后一个元素。以上程序执行输出结果为:字符串元素入栈: 
栈中的顶部元素是:runoob.
["google", "runoob"]

1.3类型约束

// 非泛型函数,查找指定字符串在数组中的索引
func findIndex(ofString valueToFind: String, in array: [String]) -> Int? {for (index, value) in array.enumerated() {if value == valueToFind {// 找到返回索引值return index}}return nil
}let strings = ["google", "weibo", "taobao", "runoob", "facebook"]
if let foundIndex = findIndex(ofString: "runoob", in: strings) {print("runoob 的索引为 \(foundIndex)")
}

1.4关联类

// Container 协议
protocol Container {associatedtype ItemType// 添加一个新元素到容器里mutating func append(_ item: ItemType)// 获取容器中元素的数var count: Int { get }// 通过索引值类型为 Int 的下标检索到容器中的每一个元素subscript(i: Int) -> ItemType { get }
}// Stack 结构体遵从 Container 协议
struct Stack<Element>: Container {// Stack<Element> 的原始实现部分var items = [Element]()mutating func push(_ item: Element) {items.append(item)}mutating func pop() -> Element {return items.removeLast()}// Container 协议的实现部分mutating func append(_ item: Element) {self.push(item)}var count: Int {return items.count}subscript(i: Int) -> Element {return items[i]}
}var tos = Stack<String>()
tos.push("google")
tos.push("runoob")
tos.push("taobao")
// 元素列表
print(tos.items)
// 元素个数
print( tos.count)
以上程序执行输出结果为:["google", "runoob", "taobao"]
3

1.5Where 语句

// 扩展,将 Array 当作 Container 来使用
extension Array: Container {}func allItemsMatch<C1: Container, C2: Container>(_ someContainer: C1, _ anotherContainer: C2) -> Boolwhere C1.ItemType == C2.ItemType, C1.ItemType: Equatable {// 检查两个容器含有相同数量的元素if someContainer.count != anotherContainer.count {return false}// 检查每一对元素是否相等for i in 0..<someContainer.count {if someContainer[i] != anotherContainer[i] {return false}}// 所有元素都匹配,返回 truereturn true
}
var tos = Stack<String>()
tos.push("google")
tos.push("runoob")
tos.push("taobao")var aos = ["google", "runoob", "taobao"]if allItemsMatch(tos, aos) {print("匹配所有元素")
} else {print("元素不匹配")
}
以上程序执行输出结果为:匹配所有元素

2.Swift 访问控制

除非有特殊的说明,否则实体都使用默认的访问级别 internal。
请添加图片描述

2.1语句函数类型访问权限:元组的访问级别与元组中访问级别最低的类型一致子类的

2.2访问级别不得高于父类的访问级别

2.3常量、变量、属性、下标访问权限:不能拥有比它们的类型更高的访问级别。

2.4Setter的访问级别可以低于对应的Getter的访问级别

class Samplepgm {fileprivate var counter: Int = 0{willSet(newTotal){print("计数器: \(newTotal)")}didSet{if counter > oldValue {print("新增加数量 \(counter - oldValue)")}}}
}let NewCounter = Samplepgm()
NewCounter.counter = 100
NewCounter.counter = 800
counter 的访问级别为 fileprivate,在文件内可以访问。以上程序执行输出结果为:计数器: 100
新增加数量 100
计数器: 800
新增加数量 700

2.5构造器和默认构造器访问权限:在每个子类的 init() 方法前使用 required 关键字声明访问权限。

2.6类型别名:任何你定义的类型别名都会被当作不同的类型,以便于进行访问控制。一个类型别名的访问级别不可高于原类型的访问级别。

public protocol Container {typealias ItemTypemutating func append(item: ItemType)var count: Int { get }subscript(i: Int) -> ItemType { get }
}struct Stack<T>: Container {// original Stack<T> implementationvar items = [T]()mutating func push(item: T) {items.append(item)}mutating func pop() -> T {return items.removeLast()}// conformance to the Container protocolmutating func append(item: T) {self.push(item)}var count: Int {return items.count}subscript(i: Int) -> T {return items[i]}
}func allItemsMatch<C1: Container, C2: Containerwhere C1.ItemType == C2.ItemType, C1.ItemType: Equatable>(someContainer: C1, anotherContainer: C2) -> Bool {// check that both containers contain the same number of itemsif someContainer.count != anotherContainer.count {return false}// check each pair of items to see if they are equivalentfor i in 0..<someContainer.count {if someContainer[i] != anotherContainer[i] {return false}}// all items match, so return truereturn true
}var tos = Stack<String>()
tos.push("Swift")
print(tos.items)tos.push("泛型")
print(tos.items)tos.push("Where 语句")
print(tos.items)var eos = ["Swift", "泛型", "Where 语句"]
print(eos)
以上程序执行输出结果为:["Swift"]
["Swift", "泛型"]
["Swift", "泛型", "Where 语句"]
["Swift", "泛型", "Where 语句"]
http://www.dtcms.com/wzjs/558553.html

相关文章:

  • 网站设计稿精品网站建设费用
  • 做一个免费网站eclipse网页制作教程
  • swoole wordpress南京关键词优化软件
  • 网站建设需要服务器支持 吗网站建设基本流程
  • 昌平网站建设浩森宇特flash网站设计实例
  • 网站建设 模板中心wordpress dux4.2
  • 网站策划ps阳西县住房和城乡建设部网站
  • 网站建设上qq图标去除网站描述在关键字前可以吗
  • 传统网站开发c2c电子商务网站
  • 企业培训网站郑州北环附近网站建设
  • 网站的前期推广时空网站建设的可行性分析
  • 淘宝内部卷网站怎么做wordpress后台图
  • 彩票网站怎么做赚钱wordpress点击tag跳回首页
  • 网站开发人员工具种类网站建设公司销售招聘
  • 网站建设公司外链怎么做男女做暖暖不要钱的试看网站
  • 企业建站1年网站运营一月多少钱
  • 阿里云网站建设最后什么样子网站在国内服务器在国外
  • 网站投票怎么做制作网站公司合同注意事项
  • 移动网站制作公司网站设计制作开发
  • 杭州seo网站哪家好wordpress展示类主题
  • 广州网站建设与实验seo免费外链工具
  • 建设网站好难重庆网站建设红衫
  • 制作宣传网站有哪些可以充值的网站怎么做
  • 打码网站做的比较好的是哪些flask做大型网站开发
  • 免费的中文logo网站wordpress博客 免费
  • 犀牛云 做网站网页制作代码格式
  • 做ag视频大全网站老吕爱分享 wordpress
  • 自己电脑做网站访问速度做宠物网站赚钱吗
  • 北京网站建设 seo公司哪家好番禺品牌型网站建设
  • 受欢迎的惠州网站建设wordpress解决速度