我的应用程序中有一个错误。此错误的原因是 UIViewController 在UITableViewUI 主题从浅色更改为深色后更新单元格,反之亦然。
我可以修复这个bug,这个bug本身不是问题。
但是,我注意到一些奇怪的行为。当我最小化我的应用程序时,两者
func willTransition(to newCollection: UITraitCollection, with coordinator: UIViewControllerTransitionCoordinator)
Run Code Online (Sandbox Code Playgroud)
和
func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?)
Run Code Online (Sandbox Code Playgroud)
被叫两次。我没有更改 UI 主题,但是当我将以下代码添加到我的UIViewController
override func willTransition(to newCollection: UITraitCollection, with coordinator: UIViewControllerTransitionCoordinator)
{
super.willTransition(to: newCollection, with: coordinator)
Swift.print("Changing to", newCollection.userInterfaceStyle == .dark ? "dark" : "light", "mode.")
}
Run Code Online (Sandbox Code Playgroud)
我得到以下输出:
Changing to dark mode.
Changing to light mode.
Run Code Online (Sandbox Code Playgroud)
就像我说的,我所做的就是最小化我的应用程序(按主页按钮)。当实际更改 UI 主题时,它只会被调用一次,这很好。
我会在重新加载之前检查实际状态UITableView(即仅在应用程序状态处于非活动状态时重新加载它,这是使用通知中心更改 UI 主题时的状态),但我只是好奇。这对我来说似乎是一个错误。
为什么最小化应用程序时会触发委托两次?
我想发布 macOS 应用程序的更新。我有一个自动启动应用程序的登录项。我在助手应用程序中使用了以下代码:
if !alreadyRunning
{
DistributedNotificationCenter.default().addObserver(self, selector: #selector(AppDelegate.terminate), name: NSNotification.Name("killme"), object: mainIdentifier)
let path = Bundle.main.bundlePath as NSString
var components = path.pathComponents
components.removeLast()
components.removeLast()
components.removeLast()
components.append("MacOS")
components.append("MyAppName")
let newPath = NSString.path(withComponents: components)
NSWorkspace.shared.launchApplication(newPath)
}
else
{
self.terminate()
}
Run Code Online (Sandbox Code Playgroud)
已经工作三年了。然而,在 macOS Big Sur 中,该launchApplication函数现已被弃用,它表示我应该使用该openApplication函数。
我将应用程序的下部更改为:
let url = URL(fileURLWithPath: newPath)
let configuration = NSWorkspace.OpenConfiguration()
configuration.promptsUserIfNeeded = true
configuration.allowsRunningApplicationSubstitution = true
NSWorkspace.shared.openApplication(at: url, configuration: configuration, completionHandler: nil)
Run Code Online (Sandbox Code Playgroud)
事实上,我已经尝试了配置上可用的所有标志。会抛出几个不同的错误。它要么说不允许启动器应用程序启动我的主应用程序,要么说辅助应用程序无法运行终端命令。
一个简单的解决方法是忽略弃用警告,我想避免这种情况。我的 LoginItem 应用程序如何在 Big Sur 上运行我的主应用程序?
我正在尝试使用ScriptingBridge阅读iTunes当前正在播放的曲目。我在这里找到了其他两个线程,但似乎没有一个对我有用(macOS Mojave和iTunes 12.9.4)。
我使用以下命令创建了iTunes.h文件:
sdef /Applications/iTunes.app | sdp -fh --basename "iTunes"
Run Code Online (Sandbox Code Playgroud)
我正在创建脚本桥的实例,如下所示:
if let itunes: AnyObject = SBApplication(bundleIdentifier: "com.apple.iTunes")
{
if itunes.isRunning
{
Swift.print("iTunes is running...")
}
Swift.print(itunes.currentTrack?.name)
Swift.print(itunes.currentTrack?.album)
}
Run Code Online (Sandbox Code Playgroud)
该代码可以正常编译。运行代码时,得到以下输出:
iTunes is running...
nil
nil
Run Code Online (Sandbox Code Playgroud)
currentTrack为零,但iTunes正在播放歌曲。我在这里想念什么?我已经尝试禁用沙箱,但无济于事。我正在使用macOS Mojave,Xcode 10.2和iTunes 12.9.4。另外,当iTunes未运行时,它将启动吗?为什么?
是否会有另一种方法来获取iTunes当前播放的歌曲?
我创建了一个小部件,我想每三秒更新一次。
我有以下代码:
struct WidgetModel : TimelineEntry
{
var date: Date
var data: [Data]
}
func getTimeline(in context: Context, completion: @escaping (Timeline<WidgetModel>) -> Void)
{
let controller = DataController()
controller.refresh()
let nextRefresh = Calendar.current.date(byAdding: .second, value: 3, to: Date())!
let model = WidgetModel(date: nextRefresh, data: controller.data)
let timeline = Timeline(entries: [model], policy: .atEnd)
completion(timeline)
}
Run Code Online (Sandbox Code Playgroud)
但是,这三秒将被忽略。模拟器总是显示下一次刷新将在五分钟后。我还尝试使用 .after 而不是 .atEnd,但这没有任何效果。五分钟是最小值还是与模拟器有关?