[英]How swift protocol Identifiable's @available working
protocol Identifiable is defined available above iOS 13 in Swift > Misc协议 Identifiable 定义在 Swift > Misc 中的 iOS 13 以上可用
/// A class of types whose instances hold the value of an entity with stable identity.
@available(OSX 10.15, iOS 13, tvOS 13, watchOS 6, *)
public protocol Identifiable {
/// A type representing the stable identity of the entity associated with `self`.
associatedtype ID : Hashable
/// The stable identity of the entity associated with `self`.
var id: Self.ID { get }
}
@available(OSX 10.15, iOS 13, tvOS 13, watchOS 6, *)
extension Identifiable where Self : AnyObject {
/// The stable identity of the entity associated with `self`.
public var id: ObjectIdentifier { get }
}
But when I setup deployment target to under iOS 13.0 ex) below 11.0 there are not any compile error occur.但是,当我将部署目标设置为低于 11.0 的 iOS 13.0 ex) 时,不会发生任何编译错误。
struct People: Identifiable {
var id: String {
return ""
}
}
let people = People()
print(people.id) // There are no compile error
Question is How Swift.Identifiable's @available annotation working?问题是 Swift.Identifiable 的 @available 注释如何工作?
You can declare an object as conforming to a protocol even if you support iOS versions that don't have that protocol, you just won't be able to use that protocol conformance in those versions.即使您支持没有该协议的 iOS 版本,您也可以将 object 声明为符合协议,您将无法在这些版本中使用该协议一致性。
For example, you would not be able to do this: people as? Identifiable
例如,您将无法做到这一点:
people as? Identifiable
people as? Identifiable
without wrapping it in a #available
block.无需将其包装在
#available
块中即可people as? Identifiable
。
You can access people.id
without a compiler error because it's just a regular String
property that is still part of your struct even without the Identifiable
conformance.您可以访问
people.id
而不会出现编译器错误,因为它只是一个常规的String
属性,即使没有Identifiable
一致性,它仍然是您的结构的一部分。
If your object was declared as a class and was relying on the default id
property that it gets from the protocol conformance then you wouldn't be able to access the id
property without an availability check:如果您的 object 被声明为 class 并且依赖于从协议一致性获得的默认
id
属性,那么您将无法在没有可用性检查的情况下访问id
属性:
class People: Identifiable {
}
let people = People()
print(people.id) // compiler error outside of #available check
For the latest xcode 12.5 and iOS 14.5 has the same issue.对于最新的 xcode 12.5 和 iOS 14.5 具有相同的问题。
It has a fix in 10.5.2 version它在 10.5.2 版本中有修复
just update your podfile with: pod 'RealmSwift', '~> 10.5.2'只需更新您的 podfile: pod 'RealmSwift', '~> 10.5.2'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.