美文网首页
swift associatedtype和typealias

swift associatedtype和typealias

作者: 山有木枝壮 | 来源:发表于2018-11-23 10:37 被阅读20次

typealias

1、可以用来对已有的类型进行重命名,比如在表示两点之间的距离的时候,可以使用typealias将x和y轴的距离Double表示为Distance。

    func distance(p1: CGPoint, p2: CGPoint) -> Double {
        let x = Double(p1.x - p2.x)
        let y = Double(p1.y - p2.y)
        return sqrt(x * x + y * y)
    }
    
    typealias Distance = Double
    func distance2(p1: CGPoint, p2: CGPoint) -> Double {
        let x = Distance(p1.x - p2.x)
        let y = Distance(p1.y - p2.y)
        return sqrt(x * x + y * y)
    }

通过上面的表述,可以使语义更加清晰,特别是在做一些数据计算的时候,不会出现很多Int,Double的数字处理,可以使用更加语义明确的表达需要计算的数据。
2、可以对闭包进行重新命名,这样在做参数传递的时候更加清晰

    typealias Success = (_ result: String) -> Void
    typealias Failure = (_ error: String) -> Void
 
    public func excuteNetworking(_ successBlock: Success, failBlock: Failure) {
        
    }

3、协议使用associatedtype的时候,可以用来对关联类型重定义,协助协议实现泛型功能,下面会说到

associatedtype

1、关联类型作为协议实现泛型的一种方式,可以在协议中预先定义一个占位符,实现协议的时候再确定这个占位符具体的类型。
下面是一个通用的协议,以及实现协议的类

protocol Food {
    func desc() -> String
}

class Apple: Food {
    func desc() -> String {
        return "apple"
    }
}

protocol Animal {
    func eat(food: Food)
}

class People: Animal {
    func eat(food: Food) {
        print("eat \(food.desc())")
    }
}

let p = People()
p.eat(food: Apple())

上面的例子中,人继承自动物,实现eat方法,方法中的Food是代表食物,也是一个协议,现在是假设人可以吃所有的食物。
再看下面的例子,如果把动物吃的食物换成associatedType类型(下面的代码编译不过)

protocol Animal2 {
    associatedtype F: Food
    
    func eat(food: F)
}

class People2: Animal2 {
    typealias F = Food
    
    func eat(food: F) {
        print("eat \(food.desc())")
    }
}
使用assotiatedtype的协议

编译器会报错,大致意思是人没有实现动物中的关联类型,关联类型需要是一个确定的类型,而不是一个协议,我们将上面的People2改成如下形式,让F是一个具体的类型,比如Apple,就可以了

protocol Animal2 {
    associatedtype F: Food
    
    func eat(food: F)
}

class People2: Animal2 {
    typealias F = Apple
    
    func eat(food: F) {
        print("eat \(food.desc())")
    }
}

let p = People2()
p.eat(food: Apple())

对于上面的写法,虽然用这个例子不太好解释,上面是限定了人吃东西只能吃苹果,这样显然是不合适的,但是如果是熊猫或者是病人,我们就可以限定他吃的东西只能是某种具体的食物。

在实际应用中,如果协议使用了关联类型,那么这个协议就失去了动态派发的特性,比如上面的例子人就只能吃苹果了。如果你给他吃一个梨,那他就消化不了

class Orange: Food {
    func desc() -> String {
        return "orange"
    }
}

let p = People2()
p.eat(food: Orange())

当然,我们也可以将People定义为泛型,这样在确认人的类型的时候需要确定他只能吃什么

class People2<F: Food>: Animal2 {
    typealias MF = F
    
    func eat(food: MF) {
        print("eat \(food.desc())")
    }
}

let p: People2<Apple> = People2()
p.eat(food: Apple())

1、Swift - 关键字(typealias、associatedtype)
2、Swift3-associatedtype用法
3、swift的protocol带有associatedtype

相关文章

  • swift associatedtype和typealias

    typealias 1、可以用来对已有的类型进行重命名,比如在表示两点之间的距离的时候,可以使用typealias...

  • Swift: typealias、associatedtype

    typealias的用法: typealias是给现有的类型(包括系统和自定义的)进行重新命名,然后就可以用该别名...

  • Swift 5.0-typealias与associatedty

    Swift 中关键字typealias重命名与associatedtype关联对象,在实际开发中比较常见。 typ...

  • Swift - Kingfisher 之 Kingfisher.

    前言 在学习Swfit语法的时候,就看到 AssociatedType 和 typealias 这两个关键字,但是...

  • Swift associatedtype

    在协议中除了定义属性和方法外,我们还能定义类型的占位符,让实现协议的类型来指定具体的类型。 添加 associat...

  • swift - typealias详解

    typealias 如果挑出swift中好用的功能,我认为typealias 应该是首当其冲的了typealias...

  • 1. swift 和OC 比较

    typealias 和 OC的typedef 2.Swift中的Any 与 AnyObject、AnyClass ...

  • Swift - typealias

    typealias用来为已存在的类型重新定义名称的。 通过命名,可以使代码变得更加清晰。使用的语法也很简单,使用 ...

  • Swift中associatedtype

  • Swift 中类型别名的用途

    什么是 typealias? 当我们回忆那些 Swift 强大的语言特性时,很少有人会首先想到 typealias...

网友评论

      本文标题:swift associatedtype和typealias

      本文链接:https://www.haomeiwen.com/subject/xgglqqtx.html