在使用它来评估NSExpression之后更新Swift字典时,EXC_BAD_ACCESS [英] EXC_BAD_ACCESS when updating Swift dictionary after using it for evaluate NSExpression

查看:163
本文介绍了在使用它来评估NSExpression之后更新Swift字典时,EXC_BAD_ACCESS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用字典来评估一个表达式,当表达式具有变量并且字典实际上被NSExpression使用时,会发生一些事情,当尝试更新字典时,我会获得EXC_BAD_ACCESS,这仅在iPhone6中调试时发生,不在模拟器中,而不是iPhone 4S。

I'm using a dictionary to evaluate an expression, when the expression has variables and the dictionary is actually used by NSExpression, something happens and I get EXC_BAD_ACCESS when trying to update the dictionary, this only happens when debugging in an iPhone6, not in the simulator and not in an iPhone 4S.

    let strExpression = "a+b+20"
    let exp = NSExpression(format:strExpression)
    self.dictionary = ["a":10.0, "b":15.0, "c":25.0]
    let value:AnyObject = exp.expressionValueWithObject(self.dictionary, context: nil)
    let doubleValue = value as Double
    self.dictionary.updateValue(doubleValue, forKey: "c")    

真的很奇怪的是,如果我在创建字典后添加这一行,那么它会很好:

Something really weird is that if i add this line just after creating the dictionary, then it woks fine:

let newDic = self.dictionary    

我,使用iOS 8.1。感谢提前!

I,m using iOS 8.1. Thanks in advance!

推荐答案

使用@bensarz评论,我认为如果我把回应

With @bensarz comment, I thought it might be helpful for others searching for answers if I put the response into an actual answer instead of a comment.

Per @ LeeWhitney对类似帖子的回复:

看起来像一个编译器错误。

Looks like a compiler bug.

您是否尝试在Release和Debug之间切换,然后重建?如果调试工作但不能释放它可能是编译器/优化器错误的指示。

Have you tried switching between Release and Debug then rebuilding? If debug works but not release it can be an indication of a compiler/optimizer bug.

它是否也发生在模拟器中?

Does it happen in the simulator also?

您的代码对于iOS 8.1适用于XCode 6.1。

Your code works for me on iOS 8.1 with XCode 6.1.

解决方案:
问题似乎通过将Swift编译器 - 代码生成下的优化级别更改为无来解决。这个问题似乎是用最快的编译器优化级别。

Solution: The issue seems to be solved by changing the 'Optimization Level' under the 'Swift Compiler - Code Generation' to 'None'. The issue seems to be with the 'Fastest' Compiler optimization level.

此外,在编译器更改之前,我已经找到了一个原始的工作: strong>

Also, a work around that I've found original before the compiler change:

如果在字典中分配值之前使用let语句,似乎可以缓解问题。更多信息请见下列链接:

If you use a let statement prior to assigning values in the dictionary, it seems to alleviate the issue. More information found at link below:

EXC_BAD_ACCESS在iOS 8.1与Dictionary

这篇关于在使用它来评估NSExpression之后更新Swift字典时,EXC_BAD_ACCESS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆