Swift 5强制独占内存

Swift 5 将带来改进的 Swift 程序内存安全性,在程序的其他部分修改变量时,不允许通过其他变量名来访问这些变量。这个变更对现有应用程序的行为和 Swift 编译器本身都有重要影响。

Swift 5强制独占内存Swift 5强制独占内存
在多种情况下会发生独占内存访问问题。编译器可以静态地捕获大部分问题,剩下的只能在运行时处理。只能在运行时处理的问题包括具有转义闭包、类类型属性、静态属性和全局变量的排他性违规。

为了更好地说明这个问题,我们可以考虑一个相当普遍的情况:修改一个函数的 inout 变量,这个函数执行了一个闭包,这个闭包使用同一作用域内的两个不同的名称访问上述的变量:

func modifyTwice(_ value: inout Int, by modifier: (inout Int) -> ()) {
  modifier(&value)
  modifier(&value)
}

func testCount() {
  var count = 1
  modifyTwice(&count) { $0 += count }
  print(count)
}

在这个例子中,因为使用 count 同时作为 modifyTwice 和 modifier 的 inout 参数,所以出现了问题。我们不清楚 print 语句应该打印出什么内容。第一次 count 变量递增,它的值递增到 2。但是,当执行第二次加法时,要添加到 $0 的 count 值是多少?这可能取决于很多因素,因为内存操作不一定是瞬时的。更糟糕的是,编译器可能会引入优化,进一步使这种情况复杂化。

这个问题不仅与通过不同变量名同时修改内存的不可预测性有关,也与编译器的复杂性有关。

这可能会导致意外和混乱的结果。它还导致编译器和标准库的实现具有很大的保守性,它们通常必须确保程序的基本可靠性(没有崩溃或未定义的行为),即使是在不寻常的情况下。

所有这些意味着如果发现独占访问冲突,使用 Swift 5 编译器编译的应用程序将在运行时崩溃。这个行为以前在 Swift 4 编译器调试模式下可用,因此,仅在运行时模式下测试过的程序在使用 Swift 5 编译时有崩溃的风险。

Swift 4 编译器可用,因此仅在运行时模式下测试的程序在使用 Swift 5 编译时可能会崩溃。

修复访问独占违规的一般方法是复制数据。在我们的示例中,这将归结为:

func modifyTwice(_ value: inout Int, by modifier: (inout Int) -> ()) {
  modifier(&value)
  modifier(&value)
}

func testCount() {
  var count = 1
  let increment = count
  modifyTwice(&count) { $0 += increment }
  print(count)
}

实际上,访问独占违规检查可能会被禁用,但强烈建议不要这样做:

虽然禁用运行时检查可能可以解决性能问题,但这并不意味着独占违规是安全的。如果没有启用强制执行,程序员必须遵守独占规则。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Author:Wallace Wang ISBN-10:1484248643 Year:2019 Pages:648 Language:English File size:23.1 MB Learn how to integrate all the interface elements iOS users have come to know and love, such as buttons, switches, pickers, toolbars, and sliders. In this edition of the best selling book, you’ll also learn about touch gestures, table views, and collection views for displaying data on a user interface. Assuming little or no working knowledge of the Swift programming language, and written in a friendly, easy-to-follow style, this book offers a comprehensive course in iPhone and iPad programming. The book starts with a gentle introduction to using Xcode and then guides you though the creation of your first simple application. You’ll start with designing basic user interfaces and then explore more sophisticated ones that involve multiple screens such as navigation controllers, tab bars, tool bars, page views, and split views that are particularly useful on the larger screens of the iPad and certain iPhone models. And there’s much more! Beginning iPhone Development with Swift 5 covers the basic information you need to get up and running quickly to turn your great ideas into working iOS apps. Once you’re ready, move on to Pro iPhone Development with Swift 5 to learn more of the really unique aspects of iOS programming and the Swift language. What You Will Learn Discover what data persistence is, and why it’s important Build cool, crisp user interfaces Display data in Table Views Work with all the most commonly used iOS Frameworks Who This Book is For Aspiring iOS app developers new to the Apple Swift programming language and/or the iOS SDK.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值