Ios 升级到swift 3后。发布构建时结构分配未按预期工作,这是错误还是我的错误?
我的项目在xcode 8.3.3和xcode 9上使用swift 3。它们有相同的问题。在转换为swift 3之前,使用swift 2.3.3*xcode 8.2是可以的 问题:一个函数返回一个struct,但stuct的值不是预期值,而是默认值 注:使用inout可以解决问题,但我仍然想知道真正的原因 我编写了一个简单的演示代码:Ios 升级到swift 3后。发布构建时结构分配未按预期工作,这是错误还是我的错误?,ios,swift,xcode,swift3,Ios,Swift,Xcode,Swift3,我的项目在xcode 8.3.3和xcode 9上使用swift 3。它们有相同的问题。在转换为swift 3之前,使用swift 2.3.3*xcode 8.2是可以的 问题:一个函数返回一个struct,但stuct的值不是预期值,而是默认值 注:使用inout可以解决问题,但我仍然想知道真正的原因 我编写了一个简单的演示代码: import UIKit protocol EncodingProtocol { mutating func decodeFromStream() }
import UIKit
protocol EncodingProtocol {
mutating func decodeFromStream()
}
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let ip: EventIpRes = EventIpRes()
ip.decodeFromStream()
print("111 \(ip.m_ip)")
}
}
struct IpInfo : EncodingProtocol {
var ip: Int = 0
mutating func decodeFromStream() {
ip = 110110110
}
}
class EventIpRes : EncodingProtocol{// #2. not impelement EncodingProtocol, everything is ok.
var m_ip = IpInfo()
func decodeFromStream(){
let testIp = ProtoBufSerializer.decodeStruct(event: m_ip) as! IpInfo
print("222 \(testIp)") // #0. Problem: testIp is default value..
m_ip = testIp
}
}
class ProtoBufSerializer {
class func decodeStruct(event: EncodingProtocol)-> EncodingProtocol{
// print("33 \(event)")// #1. uncomment this line, everything is ok.
var new = event
new.decodeFromStream()
return new
}
}
#0。问题:testIp是默认值
我尝试过的一些测试:
- 构建配置:调试正常,发布模式有问题
- 发布构建,swift编译器优化级别:快速,整个模块优化有问题,其他都可以
PS:虽然演示代码看起来很奇怪,但我认为它在语法上是正确的。它应该可以正常工作。…PS:虽然演示代码看起来很奇怪,但我认为它在语法上是正确的。它应该很好用。我可以复制基本上,我会说,任何时候你从调试和发布配置中得到不同的输出,这是苹果想要听到的错误。文件位于(或插入
print
语句更改输出时,这是一个错误。基本上发生的是编译器错误地优化了某些内容。)@matt,感谢您的回复。我将向苹果公司报告问题。:)修正PS:虽然演示代码看起来很奇怪,但我认为它在语法上是正确的。它应该很好用。我可以复制基本上,我会说,任何时候你从调试和发布配置中得到不同的输出,这是苹果想要听到的错误。文件位于(或插入print
语句更改输出时,这是一个错误。基本上发生的是编译器错误地优化了某些内容。)@matt,感谢您的回复。我将向苹果公司报告问题。:)固定在