当前位置:网站首页>Subscript in swift
Subscript in swift
2022-07-28 23:17:00 【Fluttering moth】
Subscript (subscript)
1、 Use subscript Any type can be given ( enumeration 、 Structure 、 class ) Add subscript function , Some places are also translated as : Subscript script
subscript The syntax of is similar to the instance method 、 Compute properties , Essence is method ( function )
2、subscript The return value type defined in determines ,get Method 's return value type ,set In the method newValue The type of .
class Point {
var x = 0.0, y = 0.0
subscript(index: Int) -> Double {
set {
if index == 0 {
x = newValue
} else if index == 1 {
y = newValue
}
}
get {
if index == 0 {
return x
} else if index == 1 {
return y
}
return 0
}
}
}
var p = Point()
p[0] = 11.1
p[1] = 22.2
print(p.x)
print(p.y)
print(p[0])
print(p[1])3、subscript Multiple parameters can be accepted , And of any type .
4、subscript There can be no set Method , But there must be get Method .
If only get Method , It can be omitted get
class Point {
var x = 0.0, y = 0.0
subscript(index: Int) -> Double {
if index == 0 {
return x
} else if index == 1 {
return y
}
return 0
}
}5、 You can set parameter labels
class Point {
var x = 0.0, y = 0.0
subscript(index i: Int) -> Double {
if i == 0 {
return x
} else if i == 1 {
return y
}
return 0
}
}
var p = Point()
p.y = 22.2
print(p[index: 1]) //22.26、 Subscripts can be type methods
class Sum {
static subscript(v1: Int, v2: Int) -> Int {
return v1 + v2
}
}
print(Sum[10, 20]) //307、 The essence is to call the inside set and get Method

8、 Structure 、 Class as the return value
class Point {
var x = 10, y = 10
}
class PointManager {
var point = Point()
subscript(index: Int) -> Point {
// set {
// point = newValue
// }
get {
return point
}
}
}
var pm = PointManager()
pm[0].x = 11
pm[0].y = 22Point If it's a class , You don't have to write set Method ,Point If it is a structure, you must write set Method .
9、 Receive subscripts of multiple parameters
class Grid {
var data = [
[0, 1, 2],
[3, 4, 5],
[6, 7, 8]
]
subscript(row: Int, column: Int) -> Int {
set {
guard row >= 0 && row < 3 && column >= 0 && column < 3 else {
return
}
data[row][column] = newValue
}
get {
guard row >= 0 && row < 3 && column >= 0 && column < 3 else {
return 0
}
return data[row][column]
}
}
}
var grid = Grid()
grid[0, 1] = 77
grid[1, 2] = 88
grid[2, 0] = 99
print(grid.data)边栏推荐
- Performance optimized APK slimming
- GCD summary
- 《MySQL数据库进阶实战》读后感(SQL 小虚竹)
- In 2020, the top ten domestic IC design enterprises will be exposed! These five industrial challenges still need to be overcome!
- Wheel 6: qserialport serial port data transceiver
- RouYi-Cloud平台 ---项目的启动、登录功能是怎么实现的、怎么样创建新模块
- Advanced C language: pointer (3)
- 一份来自奎哥的全新MPLS笔记,考IE必看 ----尚文网络奎哥
- This year, MediaTek 5g chip shipments are expected to reach 50million sets!
- 【图像分割】基于方向谷形检测实现静脉纹路分割附MATLAB代码
猜你喜欢

【雷达】基于核聚类实现雷达信号在线分选附matlab代码

Thesis reading (3) - googlenet of classification

定了!哪吒S全系产品将于7月31日上市发售

A new MPLS note from quigo, which must be read when taking the IE exam ---- quigo of Shangwen network

PCA学习

Servlet的使用手把手教学(一)

【C语言】三子棋小游戏实现

sql优化常用的几种方法
![[C language] implementation of three piece chess games](/img/53/7ee14e604c06fd77d65af29d6d92b8.png)
[C language] implementation of three piece chess games

Nacos配置热更新的4种方式、读取项目配置文件的多种方式,@value,@RefreshScope,@NacosConfigurationProperties
随机推荐
《MySQL数据库进阶实战》读后感(SQL 小虚竹)
Pgbench benchmark PostgreSQL
Symbol symbol type
leetcode 199. 二叉树的右视图
希捷发布全新RISC-V架构处理器:机械硬盘相关性能暴涨3倍
18 diagrams, intuitive understanding of neural networks, manifolds and topologies
[copy] Internet terms, abbreviations, abbreviations
ValueError: Using a target size (torch.Size([64])) that is different to the input size (torch.Size([
WebApplicationType#deduceFromClasspath
MySQL常用的日期时间函数
pgbench基准测试《postgresql》
A new paradigm of distributed deep learning programming: Global tensor
[mongodb] basic use of mongodb database, special cases, and the installation and creation process of mongoose (including the installation of mongoose fixed version)
6 个超级良心的开源教程!
Goer shares and Shanghai Taisi Weida growth cooperation agreement! Special SOC jointly promotes the development of TWS headphones
Target detection notes SSD
1.8tft color screen test code (stm32f407ve)
In 2020, the top ten domestic IC design enterprises will be exposed! These five industrial challenges still need to be overcome!
软件测试面试笔试题及答案(软件测试题库)
Wheel 6: qserialport serial port data transceiver