是否有guard相当于检查变量是否nil?如果是这样,我将如何翻译这样的语句来guard代替使用?
if post["preview"]! != nil {
//do stuff
} else {
//handle case where the variable is nil
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试在恢复互联网连接并且updateOnConnection变量为真时调用函数.这是我的代码:
func checkForConnection() {
let host = "reddit.com"
var context = SCNetworkReachabilityContext(version: 0, info: nil, retain: nil, release: nil, copyDescription: nil)
let reachability = SCNetworkReachabilityCreateWithName(nil, host)!
SCNetworkReachabilitySetCallback(reachability, { (_, flags, _) in
if flags.rawValue == 0 { //internet is not connected
} else { //internet became connected
if self.updateOnConnection {
self.refreshWallpaper()
}
}
}, &context)
SCNetworkReachabilityScheduleWithRunLoop(reachability, CFRunLoopGetMain(), kCFRunLoopCommonModes)
}
Run Code Online (Sandbox Code Playgroud)
我的问题是线条:
if self.updateOnConnection {
self.refreshWallpaper()
}
Run Code Online (Sandbox Code Playgroud)
导致错误:" A C function pointer cannot be formed from a closure that captures …
我希望我的OSX应用程序在用户的屏幕可用时调用一个函数,例如:他们的计算机从睡眠状态唤醒或者用户打开他们的屏幕.基本上每当用户从没有活动屏幕变为活动时,我希望我的功能被调用.
我不确定这样做的最佳方法是:
其中哪一个似乎是最好的方法,或者我应该完全做其他事情?某种示例Swift代码非常有用,因为实现其中任何一个的代码片段似乎很少.谢谢.
我正在尝试将自己的图像添加为条形按钮项,但我不知道如何使图像正确缩放.Apple的人机界面指南建议我的图像为44x44像素,但是当我使用44x44像素图像时,它对于工具栏而言太大了,您可以看到:
当我使用较小版本的图像时,它在Retina显示屏上看起来像素化.我该怎么办?
我的Mac应用程序有一个NSMenu其代表职能validateMenuItem,并menuWillOpen永远不会被调用。到目前为止,在线解决方案都没有帮助。
好像我做的一切都是对的:
我想描述我的问题的最好方法是发布相关代码。任何帮助,将不胜感激。
import Cocoa
class UIManager: NSObject, NSMenuDelegate {
var statusBarItem = NSStatusBar.system().statusItem(withLength: -2)
var statusBarMenu = NSMenu()
var titleMenuItem = NSMenuItem()
var descriptionMenuItem = NSMenuItem()
// ...
override init() {
super.init()
createStatusBarMenu()
}
// ...
func createStatusBarMenu() {
// Status bar icon
guard let icon = NSImage(named: "iconFrame44")
else { NSLog("Error setting status bar icon image."); return }
icon.isTemplate = true
statusBarItem.image = icon
// Create Submenu items
let viewOnRedditMenuItem = …Run Code Online (Sandbox Code Playgroud) 在我的代码中,cell如果它在第一tableView部分中,则将是一种类型,如果不是,则将是另一种类型.
var cell = UITableViewCell()
if indexPath.section == 0 {
cell = tableView.dequeueReusableCellWithIdentifier("UpcomingWorkoutCell", forIndexPath: indexPath) as! MyWorkoutsTableViewCell
cell.nameLabel.text = workout.trainer.name
cell.dateLabel.text = workout.getDateString()
cell.timeLabel.text = workout.getTimeString()
cell.trainerImage.image = workout.image
} else {
cell = tableView.dequeueReusableCellWithIdentifier("PastWorkoutCell", forIndexPath: indexPath) as! PastWorkoutTableViewCell
cell.nameLabel.text = workout.trainer.name
cell.dateLabel.text = workout.getDateString()
cell.timeLabel.text = workout.getTimeString()
cell.trainerImage.image = workout.image
cell.ratingControl.rating = workout.rating // Exclusive to PastWorkoutCell
}
Run Code Online (Sandbox Code Playgroud)
有没有更好的方法来写这个?我可以以某种方式摆脱最初的声明作为一个UITableViewCell并通过一行缩短我的代码?
此外,MyWorkoutsTableViewCell和PastWorkoutCell几乎具有相同的所有属性.我可以避免在if语句的每个分支中重复代码来设置所有这些代码吗?现在单元格是UITableViewCell没有任何属性的,所以我似乎无法在两个分支之外进行.
许多在线教程似乎赞成使用if-let或guard-let语句从UserDefaults中检索可选值.这有意义吗?我读到Apple打算让我们立即为UserDefaults提供默认值.
使用guard语句安全地解包这些值会导致更多代码,如果您的应用程序立即初始化这些值,这似乎是多余的.从UserDefaults强制解包值是否有任何缺点?