0

有一个类似名称的主题,但该示例是由于用户错误而导致的错误。我相信这个例子是一个实际的 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)

  }
}
4

1 回答 1

0

让我们

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,即使i = 0init 的参数为 true 时无法访问,编译器也不会抱怨。

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
    }
}

不会用error: return from initializer without initializing all stored propertiesand编译,note: 'self.i' not initialized因为常量let i: Int?没有任何默认值

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

于 2016-02-14T20:56:56.667 回答