@available(*, unavailable, message: "Please using init(). This view using YogaKit to layout subviews.")
override public init(frame _: CGRect) {
fatalError("init(frame:) has not been implemented")
}//*:所有平台
//unavailable:不可用
//message:信息
所有平台上这个初始化方法不可用
-
Swift 2.0 中,引入了可用性的概念。对于函数,类,协议等,可以使用**@available** 声明这些类型的生命周期依赖于特定的平台和操作系统版本。而**#available** 用在判断语句中(if, guard, while等),在不同的平台上做不同的逻辑。
@available放在函数(方法),类或者协议前面。表明这些类型适用的平台和操作系统。
@available(iOS 9, *) func myMethod() {
// do something
}
@available还有其他一些参数可以使用,分别是:
// deprecated = 版本号:从指定平台某个版本开始过期该声明
// obsoleted = 版本号:从指定平台某个版本开始废弃(注意弃用的区别,deprecated是还可以继续使用,只不过是不推荐了,obsoleted是调用就会编译错误)该声明
// message = 信息内容:给出一些附加信息
// unavailable : 指定平台上是无效的
// renamed = 新名字:重命名声明
@available(iOS, introduced: 6.0, deprecated: 9.0, message:"")
func myMethod() { }
@available(iOS, introduced: 8.0, obsoleted: 10.0, message:"")
func myMethod() { }
#available 用在条件语句代码块中,判断不同的平台下,做不同的逻辑处理
if #available(iOS 8, *) {
// iOS 8 及其以上系统运行
}
guard #available(iOS 8, *) else {
return //iOS 8 以下系统就直接返回
}
fatalError:类似于别的语言中的断言,assert
/// Unconditionally prints a given message and stops execution.
///
/// - Parameters:
/// - message: The string to print. The default is an empty string.
/// - file: The file name to print with `message`. The default is the file
/// where `fatalError(_:file:line:)` is called.
/// - line: The line number to print along with `message`. The default is the
/// line number where `fatalError(_:file:line:)` is called.
public func fatalError(_ message: @autoclosure () -> String = String(), file: StaticString = #file, line: UInt = #line) -> Never
当触发fatalError时候会强制抛出错误
@available(*, unavailable, message: "Please using init(). This view using YogaKit to layout subviews.")
override public init(frame _: CGRect) {
fatalError("init(frame:) has not been implemented")
}//*:所有平台
//unavailable:不可用
//message:信息