Swift方法&下标

一、方法(Method)

1、方法简介
  • 枚举、结构体、类都可以定义实例方法、类型方法。
    实例方法:通过实例对象调用。
    类型方法:通过类型调用,用static或者class关键子定义。
class Animal {
    static var count = 0
    init() {
        Animal.count += 1
    }
    static func getCount()  {
        print("调用了类方法",count)
    }
}

调用:

        let animal1 = Animal()
        let animal2 = Animal()
        let animal3 = Animal()
        print("Animal count:",Animal.count)
        //结果是3
  • self:在实例方法中代表对象,在类型方法中代表类。
  • 在类方法中static func getCount
    count等价于self.count,Animal.count,Animal.self.count,三种写法没有啥子区别。
2、mutating
  • 结构体和枚举是值类型,默认情况下,值类型的属性不能被自身的实例方法修改。如果没有添加mutating,那么报错如下:
    Left side of mutating operator isn't mutable: 'self' is immutable

  • 但是我们如果想要修改,该怎么办呢?
    在func关键字前加mutating可以允许这种修改行为,。
struct Point {
    var x = 0.0
    var y = 0.0
    mutating func moveBy(deltaX: Double, deltaY: Double) {
        x += deltaX
        y += deltaY
    }
}
3、discardableResult
  • 在func前面加上@discardableResult( @discardableResult mutating func moveBy),可以消除:函数调用后返回值未被使用的警告。⚠️
struct Point {
    var x = 0.0
    var y = 0.0
    mutating func moveBy(deltaX: Double, deltaY: Double) -> (Double) {
        x += deltaX
        y += deltaY
        return x+y
    }
}

调用:

     var p = Point()
     p.moveBy(deltaX: 1.0, deltaY: 2.0)

警告提示:

⚠️Result of call to 'moveBy(deltaX:deltaY:)' is unused

discard: | BrE dɪˈskɑːd, AmE dɪˈskɑrd | 丢弃、抛弃

二、subscript(下标)

1、下标简介

  • 使用subscript可以给任意类型(枚举、结构体、类)增加下标功能。
    subscript方法类似于实例方法、计算属性,本质就是方法(函数),通过下面的实例,我们可以知道,下标可以说为我们提供了一种更便捷的调用方式。
class MyPoint {
    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
        }
    }
}

调用:

        let myp = MyPoint()
        myp[0] = 33.33
        myp[1] = 66.66
        print(myp.x) //33.33
        print(myp.y) //66.66
        print(myp[0])//33.33
        print(myp[1])//66.66
  • subscript方法中定义的返回值类型决定了get方法的返回值类型,set方法中newValue的类型。
  • subscript可以接受多个参数,并且是任意类型。
  • subscript可以没有set方法,但是必须要有get方法,如果只有get方法,则可以省略set方法。
class Point {
var x = 0.0, y = 0.0 subscript(index: Int) -> Double {
        get {
            if index == 0 {
                return x
            } else if index == 1 {
return y }
return 0 }
} }
  • subscript可以设置参数标签。
struct Sum {
    static subscript(indexOne v1: Int,indexTwo v2: Int) -> Int{
        return v1+v2
    }
}
 print(Sum[indexOne: 22, indexTwo: 44])

  • subscript可以是类型方法。
class Sum {
    static subscript(v1: Int, v2: Int) -> Int{
        return v1+v2
    }
}
print(Sum[22, 44])

三、泛型和Any
泛型的优势:大大提升了代码的可复用性。而且同时也提升了代码的安全性。
1、Any的使用:

 func myPrintAny(any1:Any, any2:Any){
        print(any1)
        print(any2)
    }

2、泛型方法:

  func myPrint<T>(any1:T, any2:T){
        print(any1)
        print(any2)
    }

3、泛型的调用:

myPrint(any1: 1, any2: 1)
myPrint(any1: "1", any2: "1")
myPrint(any1: ["1","2"], any2: ["3","4"])

泛型链接
泛型和Any的区别
从表面上看,这好像和泛型极其相似。Any 类型和泛型两者都能用于定义接受两个不同类型参数的函数。然而,理解两者之间的区别至关重要:泛型可以用于定义灵活的函数,类型检查仍然由编译器负责;而 Any 类型则可以避开 Swift 的类型系统 (所以应该尽可能避免使用)。
如果你需要一个混合不同类型的集合,应该使用 Any .
相同类型的集合,应该使用T。
https://www.jianshu.com/p/05902b338be5

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容