为什么 Xcode 在我的 'if' 子句中显示警告 'Will never be executed'?
Why does Xcode show warning 'Will never be executed' on my 'if' clause?
我想在我的 tableview 背景上显示 note/label when/if 没有要加载的数据,或者正在获取数据等
我看不出我做错了什么。 Xcode 在这行代码上显示警告 "Will never be executed":if mostUpTodateNewsItemsFromRealm?.count < 1 {
方法在这里
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// create a lable ready to display
let statusLabel: UILabel = UILabel(frame: CGRectMake(0, 0, self.tableView.bounds.size.width, self.tableView.bounds.size.height))
statusLabel.textColor = globalTintColor
statusLabel.textAlignment = NSTextAlignment.Center
self.tableView.backgroundView = statusLabel
self.tableView.backgroundView?.backgroundColor = colourOfAllPickerBackgrounds
// 1) Check if we have tried to fetch news
if NSUserDefaults.standardUserDefaults().valueForKey("haveTriedToFetchNewsForThisChurch") as! Bool == false {
statusLabel.text = "Busy loading news..."
} else {
// If have tried to fetch news items = true
// 2) check church has channels
let numberOfChannelsSubscribedToIs = 0
if let upToDateSubsInfo = upToDateChannelAndSubsInfo {
let numberOfChannelsSubscribedToIs = 0
for subInfo in upToDateSubsInfo {
if subInfo.subscribed == true {
numberOfChannelsSubscribedToIs + 1
}
}
}
if numberOfChannelsSubscribedToIs < 1 {
// if no channels
// show messsage saying you aren't subscribed to any channels.
statusLabel.text = "Your church hasn't setup any news channels yet."
} else {
// 3) if we have tried to fetch news AND the church DOES have channels
// check if we have any news items to show
if mostUpTodateNewsItemsFromRealm?.count < 1 {
// If no news items
statusLabel.text = "Your church hasn't broadcast and news yet."
} else {
// if have tried to fetch AND church has channels AND there ARE news items
// remove the background image so doesn't show when items load.
self.tableView.backgroundView = nil
}
}
}
// in all circumstances there will be one section
return 1
}
您的代码首先创建了一个常量:
let numberOfChannelsSubscribedToIs = 0
然后你检查它是否小于1:
if numberOfChannelsSubscribedToIs < 1
既然是常数,就永远不会变。这意味着 if 子句将始终被执行。因此,else
子句永远不会被执行。
所以首先你需要创建这个常量变量:
var numberOfChannelsSubscribedToIs = 0
那就改一下:
if subInfo.subscribed == true {
numberOfChannelsSubscribedToIs + 1
}
对此:
if subInfo.subscribed == true {
numberOfChannelsSubscribedToIs += 1
}
这样,numberOFChannelSubscribedToIs
可以是0以外的数字,else子句就可以执行了。
var
和let
差别很大!
我想在我的 tableview 背景上显示 note/label when/if 没有要加载的数据,或者正在获取数据等
我看不出我做错了什么。 Xcode 在这行代码上显示警告 "Will never be executed":if mostUpTodateNewsItemsFromRealm?.count < 1 {
方法在这里
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// create a lable ready to display
let statusLabel: UILabel = UILabel(frame: CGRectMake(0, 0, self.tableView.bounds.size.width, self.tableView.bounds.size.height))
statusLabel.textColor = globalTintColor
statusLabel.textAlignment = NSTextAlignment.Center
self.tableView.backgroundView = statusLabel
self.tableView.backgroundView?.backgroundColor = colourOfAllPickerBackgrounds
// 1) Check if we have tried to fetch news
if NSUserDefaults.standardUserDefaults().valueForKey("haveTriedToFetchNewsForThisChurch") as! Bool == false {
statusLabel.text = "Busy loading news..."
} else {
// If have tried to fetch news items = true
// 2) check church has channels
let numberOfChannelsSubscribedToIs = 0
if let upToDateSubsInfo = upToDateChannelAndSubsInfo {
let numberOfChannelsSubscribedToIs = 0
for subInfo in upToDateSubsInfo {
if subInfo.subscribed == true {
numberOfChannelsSubscribedToIs + 1
}
}
}
if numberOfChannelsSubscribedToIs < 1 {
// if no channels
// show messsage saying you aren't subscribed to any channels.
statusLabel.text = "Your church hasn't setup any news channels yet."
} else {
// 3) if we have tried to fetch news AND the church DOES have channels
// check if we have any news items to show
if mostUpTodateNewsItemsFromRealm?.count < 1 {
// If no news items
statusLabel.text = "Your church hasn't broadcast and news yet."
} else {
// if have tried to fetch AND church has channels AND there ARE news items
// remove the background image so doesn't show when items load.
self.tableView.backgroundView = nil
}
}
}
// in all circumstances there will be one section
return 1
}
您的代码首先创建了一个常量:
let numberOfChannelsSubscribedToIs = 0
然后你检查它是否小于1:
if numberOfChannelsSubscribedToIs < 1
既然是常数,就永远不会变。这意味着 if 子句将始终被执行。因此,else
子句永远不会被执行。
所以首先你需要创建这个常量变量:
var numberOfChannelsSubscribedToIs = 0
那就改一下:
if subInfo.subscribed == true {
numberOfChannelsSubscribedToIs + 1
}
对此:
if subInfo.subscribed == true {
numberOfChannelsSubscribedToIs += 1
}
这样,numberOFChannelSubscribedToIs
可以是0以外的数字,else子句就可以执行了。
var
和let
差别很大!