2017-03-27 16:08:24 shenhaifeiniao 阅读数 0
  • nullptr和static_assert

    快速了解C++11常见特性及其在Qt中的体现,立即运用到实际开发中,为进一步学习C++11奠定基础。

    2141课时 0分钟 348人学习 安晓辉
    免费试看

在Swift中,通过assert实现断言,assert可以帮助开发者比较容易的发现和定位错误。
一个断言断定条件是true.通过声明一个断言来确保某个必要的条件是满足的,以便继续执行接下来的代码。如果条件满足了,那么代码像往常一样执行,如果不满足了,代码就停止执行了,应用也随之停下来了。
例:

 let number = 3
 //第一个参数为判断条件,第二个参数为条件"不满足"时的打印信息。
 assert(number > 3, "number 不大于3")

如果断言被触发(number <= 3时),将会强制结束程序,并打印相关信息:

assertion failed: number 不大于3: file/Users/mac/Desktop/test/test/ViewController.swift, line 17

注意点:

断言可以引发程序终止,并不是被设计用来避免不在发生无效情况的。在那些无效条件有可能触发的情况下,断言是一种有效的调试手段,可以确保在应用发布之前,在开发过程中就可以关注这些条件。

使用场景:

在条件有可能为false的情况,但是只有在true的情况下才可以继续运行代码,使用断言。比如下面的情况:

自定义整型下标传给自定义索引实现的参数时,下标表达式的值有可能太大或者太小
向函数传值的时候,一个无效值意味着函数不能完成相应的任务
一个Optional类型目前为nil,但是必须要有一个非nil值以便接下来的代码可以成功的执行

转载自http://blog.csdn.net/feng2qing/article/details/51241095

2017-03-02 18:09:33 frankisbaby 阅读数 248
  • nullptr和static_assert

    快速了解C++11常见特性及其在Qt中的体现,立即运用到实际开发中,为进一步学习C++11奠定基础。

    2141课时 0分钟 348人学习 安晓辉
    免费试看

In some cases, it is simply not possible for your code to continue execution if a particular condition is not satisfied. In these situations, you can trigger an assertion in your code to end code execution and to provide an opportunity to debug the cause of the absent or invalid value.
在某些情况下,如果不满足特定条件,您的代码不可能继续执行。在这些情况下,您可以在代码中触发断言以结束代码执行,并提供调试缺少或无效值的原因的机会。
使用断言的场合
整数下标索引传递到自定义下标实现,但下标索引值可能太低或太高。
一个值被传递给一个函数,但是一个无效的值意味着该函数不能完成它的任务。
当前是一个可选值nil,但非nil值对于后续代码成功执行至关重要。
1.可能越界的时候
2.可能为nil,nil的时候影响较大
3.从其他地方传递过来的值,可能不对对下边影响比较大。
使用方法
这里写图片描述
注释
断言模式在release阶段会自动关闭。

2016-04-25 11:39:34 feng2qing 阅读数 5784
  • nullptr和static_assert

    快速了解C++11常见特性及其在Qt中的体现,立即运用到实际开发中,为进一步学习C++11奠定基础。

    2141课时 0分钟 348人学习 安晓辉
    免费试看

在Swift中,通过assert实现断言,assert可以帮助开发者比较容易的发现和定位错误

一个断言断定条件是true.通过声明一个断言来确保某个必要的条件是满足的,以便继续执行接下来的代码。如果条件满足了,那么代码像往常一样执行,如果不满足了,代码就停止执行了,应用也随之停下来了。

代码

let number = 3
//第一个参数为判断条件,第二个参数为条件"不满足"时的打印信息。
assert(number > 3, "number 不大于3")
//如果断言被触发(number <= 3时),将会强制结束程序,并打印相关信息:
assertion failed: number 不大于3: file /Users/mac/Desktop/test/test/ViewController.swift, line 17

注意点

断言可以引发程序终止,并不是被设计用来避免不在发生无效情况的。在那些无效条件有可能触发的情况下,断言是一种有效的调试手段,可以确保在应用发布之前,在开发过程中就可以关注这些条件。

使用场景:

在条件有可能为false的情况,但是只有在true的情况下才可以继续运行代码,使用断言。比如下面的情况:

  • 自定义整型下标传给自定义索引实现的参数时,下标表达式的值有可能太大或者太小
  • 向函数传值的时候,一个无效值意味着函数不能完成相应的任务
  • 一个Optional类型目前为nil,但是必须要有一个非nil值以便接下来的代码可以成功的执行
2018-12-04 12:58:03 weixin_33828101 阅读数 0
  • nullptr和static_assert

    快速了解C++11常见特性及其在Qt中的体现,立即运用到实际开发中,为进一步学习C++11奠定基础。

    2141课时 0分钟 348人学习 安晓辉
    免费试看

程序的运行需要按照我们预先设计好的路线执行,想要执行某些操作必然需要满足一定的条件,不满足条件就无法继续向下执行。

其实Swift语言为我们提供了这样的函数:assert和pecondition。

assert和precondition是做什么的?

从字面意思即可看出,assert是断言(必须成立),precondition是先决条件,是满足条件才能继续往下。两者的区别就是assert只在debug环境下生效,而precondition是debug和release都生效。

官方代码

/// Performs a traditional C-style assert with an optional message.
///
/// Use this function for internal sanity checks that are active during testing
/// but do not impact performance of shipping code. To check for invalid usage
/// in Release builds, see `precondition(_:_:file:line:)`.
///
/// * In playgrounds and `-Onone` builds (the default for Xcode's Debug
///   configuration): If `condition` evaluates to `false`, stop program
///   execution in a debuggable state after printing `message`.
///
/// * In `-O` builds (the default for Xcode's Release configuration),
///   `condition` is not evaluated, and there are no effects.
///
/// * In `-Ounchecked` builds, `condition` is not evaluated, but the optimizer
///   may assume that it *always* evaluates to `true`. Failure to satisfy that
///   assumption is a serious programming error.
///
/// - Parameters:
///   - condition: The condition to test. `condition` is only evaluated in
///     playgrounds and `-Onone` builds.
///   - message: A string to print if `condition` is evaluated to `false`. The
///     default is an empty string.
///   - file: The file name to print with `message` if the assertion fails. The
///     default is the file where `assert(_:_:file:line:)` is called.
///   - line: The line number to print along with `message` if the assertion
///     fails. The default is the line number where `assert(_:_:file:line:)`
///     is called.
public func assert(_ condition: @autoclosure () -> Bool, _ message: @autoclosure () -> String = default, file: StaticString = #file, line: UInt = #line)

/// Checks a necessary condition for making forward progress.
///
/// Use this function to detect conditions that must prevent the program from
/// proceeding, even in shipping code.
///
/// * In playgrounds and `-Onone` builds (the default for Xcode's Debug
///   configuration): If `condition` evaluates to `false`, stop program
///   execution in a debuggable state after printing `message`.
///
/// * In `-O` builds (the default for Xcode's Release configuration): If
///   `condition` evaluates to `false`, stop program execution.
///
/// * In `-Ounchecked` builds, `condition` is not evaluated, but the optimizer
///   may assume that it *always* evaluates to `true`. Failure to satisfy that
///   assumption is a serious programming error.
///
/// - Parameters:
///   - condition: The condition to test. `condition` is not evaluated in
///     `-Ounchecked` builds.
///   - message: A string to print if `condition` is evaluated to `false` in a
///     playground or `-Onone` build. The default is an empty string.
///   - file: The file name to print with `message` if the precondition fails.
///     The default is the file where `precondition(_:_:file:line:)` is
///     called.
///   - line: The line number to print along with `message` if the assertion
///     fails. The default is the line number where
///     `precondition(_:_:file:line:)` is called.
public func precondition(_ condition: @autoclosure () -> Bool, _ message: @autoclosure () -> String = default, file: StaticString = #file, line: UInt = #line)

/// Indicates that an internal sanity check failed.
///
/// Use this function to stop the program, without impacting the performance of
/// shipping code, when control flow is not expected to reach the call---for
/// example, in the `default` case of a `switch` where you have knowledge that
/// one of the other cases must be satisfied. To protect code from invalid
/// usage in Release builds, see `preconditionFailure(_:file:line:)`.
///
/// * In playgrounds and -Onone builds (the default for Xcode's Debug
///   configuration), stop program execution in a debuggable state after
///   printing `message`.
///
/// * In -O builds, has no effect.
///
/// * In -Ounchecked builds, the optimizer may assume that this function is
///   never called. Failure to satisfy that assumption is a serious
///   programming error.
///
/// - Parameters:
///   - message: A string to print in a playground or `-Onone` build. The
///     default is an empty string.
///   - file: The file name to print with `message`. The default is the file
///     where `assertionFailure(_:file:line:)` is called.
///   - line: The line number to print along with `message`. The default is the
///     line number where `assertionFailure(_:file:line:)` is called.
public func assertionFailure(_ message: @autoclosure () -> String = default, file: StaticString = #file, line: UInt = #line)

/// Indicates that a precondition was violated.
///
/// Use this function to stop the program when control flow can only reach the
/// call if your API was improperly used. This function's effects vary
/// depending on the build flag used:
///
/// * In playgrounds and `-Onone` builds (the default for Xcode's Debug
///   configuration), stops program execution in a debuggable state after
///   printing `message`.
///
/// * In `-O` builds (the default for Xcode's Release configuration), stops
///   program execution.
///
/// * In `-Ounchecked` builds, the optimizer may assume that this function is
///   never called. Failure to satisfy that assumption is a serious
///   programming error.
///
/// - Parameters:
///   - message: A string to print in a playground or `-Onone` build. The
///     default is an empty string.
///   - file: The file name to print with `message`. The default is the file
///     where `preconditionFailure(_:file:line:)` is called.
///   - line: The line number to print along with `message`. The default is the
///     line number where `preconditionFailure(_:file:line:)` is called.
public func preconditionFailure(_ message: @autoclosure () -> String = default, file: StaticString = #file, line: UInt = #line) -> Never

/// 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 = default, file: StaticString = #file, line: UInt = #line) -> Never

复制代码

这么长,肯定没有耐心看。我摘出来

public func assert(_ condition: @autoclosure () -> Bool, _ message: @autoclosure () -> String = default, file: StaticString = #file, line: UInt = #line)
public func precondition(_ condition: @autoclosure () -> Bool, _ message: @autoclosure () -> String = default, file: StaticString = #file, line: UInt = #line)
public func assertionFailure(_ message: @autoclosure () -> String = default, file: StaticString = #file, line: UInt = #line)
public func preconditionFailure(_ message: @autoclosure () -> String = default, file: StaticString = #file, line: UInt = #line) -> Never
public func fatalError(_ message: @autoclosure () -> String = default, file: StaticString = #file, line: UInt = #line) -> Never
复制代码

condition是Bool,true值则不成立,false则成立程序挂掉。

assertionFailure和preconditionFailure都是直接挂掉,不用加判断条件。fatalError是致命错误,程序挂掉。

转载于:https://juejin.im/post/5c0676b76fb9a049a711962d

2014-06-08 15:54:16 woaifen3344 阅读数 0
  • nullptr和static_assert

    快速了解C++11常见特性及其在Qt中的体现,立即运用到实际开发中,为进一步学习C++11奠定基础。

    2141课时 0分钟 348人学习 安晓辉
    免费试看

断言(Assertions)

Optionals使得我们可以检测值是否存在。在某些情况下,如果某个值不存在或者没有提供特定的满足条件,代码不应该继续往下执行。

在这些情况下,可以使用触发断言来终止执行并提供调试。

断言是在运行时检测条件是否为true,如果为true,就继续往下执行,否则就在这里中断。

断言对应的关键字是assert

如:

let age = 18
// 这里age >= 18表示式的结果是true,所以这里断言什么也不做,代码继续往下执行
assert(age >= 18, "A person'a age cannot be less than eighteen")
// 这里由于 age >= 22表达式的结果是false,所以这里就会出现中断,打印出
// ""A person's age cannot be less than 22"
assert(age > 22, "A person's age cannot be less than 22")

什么时候使用断言呢?

包含下面的情况时使用断言:

1、整型下标索引作为值传给自定义索引实现的参数时,但下标索引值不能太低也不能太高时,使用断言

2、传值给函数但如果这个传过来的值无效时,函数就不能完成功能时,使用断言。

3、Optional值当前为nil,但是后面的代码成功执行的条件是要求这个值不能为nil,使用断言

Swift Assert 断言

阅读数 0

swift 断言(assert)

阅读数 622