gpt4 book ai didi

Swift 2.0 Guard 语句使结构初始化程序失败

转载 作者:行者123 更新时间:2023-11-30 13:42:36 25 4
gpt4 key购买 nike

有一个类似名称的主题,但该示例由于用户错误而出现错误。我相信这个例子是一个实际的 XCode 问题。

我正在学习树屋教程,本着 swift 2.0 的精神,我在初始化程序中使用了保护语句而不是 if let 。除了使用保护语句之外,我的代码与指令相同。它有一个错误,表示“从初始化程序返回而不初始化所有存储的属性”。一旦我将其更改为 if let 语句,它就起作用了。也许我在某个地方犯了一个错误,但我盯着它看了至少一个小时,没有任何属性未初始化。

为了以防万一,我在 else 子句中将属性设置为 nil,但这并没有影响任何东西。

结构每日天气{

let maxTemp: Int?
let minTemp: Int?
let humidity: Int?
let precipChance: Int?
var summary: String?
var icon: UIImage? = UIImage(named: "default.png")
var largeIcon: UIImage? = UIImage(named: "default_large.png")
var sunriseTime: String?
var sunsetTime: String?
var day: String?

let dateFormatter = NSDateFormatter()

init(dailyWeatherDictionary: [String:AnyObject]) {

minTemp = dailyWeatherDictionary["temperatureMin"] as? Int
maxTemp = dailyWeatherDictionary["temperatureMax"] as? Int
guard let humidityFloat = dailyWeatherDictionary["humidity"] as? Double else { humidity = nil ; return }
humidity = Int(humidityFloat * 100)
guard let precipFloat = dailyWeatherDictionary["precipProbability"] as? Double else { precipChance = nil ; return }
precipChance = Int(precipFloat * 100)
summary = dailyWeatherDictionary["summary"] as? String
guard let
iconString = dailyWeatherDictionary["icon"] as? String,
iconEnum = Icon(rawValue: iconString) else { icon = nil ; largeIcon = nil ; return }
(icon, largeIcon) = iconEnum.toImage()

guard let sunriseDate = dailyWeatherDictionary["sunriseTime"] as? Double else { sunriseTime = nil ; return }
sunriseTime = timeStringFromUnixTime(sunriseDate)
guard let sunsetDate = dailyWeatherDictionary["sunsetTime"] as? Double else { sunsetTime = nil ; return }
sunsetTime = timeStringFromUnixTime(sunsetDate)
guard let time = dailyWeatherDictionary["time"] as? Double else { day = nil ; return }
day = dayStringFromUnixTime(time)

}


func timeStringFromUnixTime(unixTime: Double) -> String {

let date = NSDate(timeIntervalSince1970: unixTime)

dateFormatter.dateFormat = "hh:mm a"
return dateFormatter.stringFromDate(date)

}

func dayStringFromUnixTime(unixTime: Double) -> String {

let date = NSDate(timeIntervalSince1970: unixTime)
dateFormatter.locale = NSLocale(localeIdentifier: NSLocale.currentLocale().localeIdentifier)
dateFormatter.dateFormat = "EEEE"
return dateFormatter.stringFromDate(date)

}
}

最佳答案

我们来吧

struct S {
var i: Int?
init(b: Bool){
guard b == false else { return }
//if b == true { return }
i = 0 // if b == true, this statement doesn't execute
}
}

let s1 = S(b: true)
let s2 = S(b: false)
print(s1, s2) // S(i: nil) S(i: Optional(0))

因为 var i: Int? 有一个默认值 nil,即使 init 参数为 true 时 i = 0 无法访问,编译器也不会提示.

struct S {
let i: Int?
init(b: Bool){
guard b == false else { return }
//if b == true { return }
i = 0 // if b == true, this statement doesn't execute
}
}

不会编译,并出现错误:从初始化程序返回,而不初始化所有存储的属性注意:'self.i'未初始化,因为常量let i : Int?没有任何默认值

你的麻烦是,你从 init 返回。通常,如果您的初始化程序不是可失败/初始化的,请避免从初始化程序返回?/。如果初始化失败?唯一接受的返回值为 nil。

关于Swift 2.0 Guard 语句使结构初始化程序失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35386903/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com