我正在尝试检查UserNotifications是否已启用,如果不是,我想要发出警报.所以我有一个checkAvailability检查多项内容的功能,包括UserNotification授权状态.
func checkAvailabilty() -> Bool {
//
// other checking
//
var isNotificationsEnabled = false
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound], completionHandler: { (granted, error) in
if granted {
isNotificationsEnabled = true
}
else {
isNotificationsEnabled = false
}
})
}
if isNotificationsEnabled {
return true
}
else {
// Throw alert: Remind user to activate notifications
return false
}
}
Run Code Online (Sandbox Code Playgroud)
但是完成处理程序调用太迟了.函数已经返回false,之后执行colsure中的代码.
我试图将整个语句UNUserNotificationCenter.current().requestAuthorization()放在同步调度队列中,但这不起作用.
另一种方法是从闭包内部返回,但我不知道如何实现.
我有一个反应值,我想将其存储在 pinia 商店中。
const data = ref({});
async function loadData() {
fetch("...")
.then((res) => res.json())
.then((json) => (data.value = json));
}
loadData();
const myDataStore = useMyDataStore();
const { myData } = storeToRefs(myDataStore);
// Here I want to store data in the store, reactively
myData.value = data.value;
Run Code Online (Sandbox Code Playgroud)
但当我这样做时,反应性就会消失。如何将值存储在商店中,以便myData每次更新时data都会更新?
是否可以使用UISearchController而不SearchBar显示在 中Navigation Controller?我基本上希望SearchBar保留在屏幕上并在其下方显示 TableView:
我想做的是:我UITableViewController在故事板上创建了一个并将其链接到自定义NPTableViewController类。然后我这样做了:
let resultsTable = storyboard!.instantiateViewController(withIdentifier: "LocationSearch") as! NPTableViewController
searchController = UISearchController(searchResultsController: resultsTable)
searchController?.searchResultsUpdater = resultsTable
let searchBar = searchController!.searchBar
searchBar.sizeToFit()
searchBar.placeholder = "Search for places"
searchBar.frame.origin.y = 100.0
self.view.addSubview(searchBar)
Run Code Online (Sandbox Code Playgroud)
现在,当我运行它时,会searchBar显示 ,但是当我单击它时,背景会变暗,searchBar消失,我的resultsTable也不显示。
iphone uinavigationcontroller uisearchbar swift uisearchcontroller
我想调用方法"deleteCharactersInRange"但它不起作用.
这是苹果文档的摘录:
迅速
func deleteCharactersInRange(_ aRange:NSRange)
var day = ""
let stringRange:NSRange = NSMakeRange(0, 4)
day = day.deleteCharacterInRange(stringRange)
// I've also tried this, because in the Documentation
// I can't see wether the method is void or returns a String
day.deleteCharacterInRange(stringRange)
Run Code Online (Sandbox Code Playgroud)
我收到此错误消息:
'String'没有名为'deleteCharactersInRange'的成员
我正在尝试将我的代码(用Swift 3编写)转换为Swift 4,因为我@objc在需要的地方添加.Xcode已经做了相当不错的工作来自动修复它们,但我正在努力解决一些问题(所有使用相同的2种方法),Xcode无法帮助,它只是放在@objc我的代码中.
我navbarRightButtonAction(button:)在ViewController课堂上重写了一个这样的方法.
class ViewController: PBViewController {
override func navbarRightButtonAction(button: PBAdaptiveButton) {
...
}
}
Run Code Online (Sandbox Code Playgroud)
这是我收到警告的地方:
Override of instance method 'navbarRightButtonAction(button:)' from extension of PBViewController depends on deprecated inference of '@objc'
Run Code Online (Sandbox Code Playgroud)
然后我认为问题是我们在PBViewController类中看起来像这样:
extension PBViewController: PBNavigationBarDelegate {
func navbarRightButtonAction(button: PBAdaptiveButton) {
print("Override this method")
}
}
Run Code Online (Sandbox Code Playgroud)
所以我补充说@objc func navbarRightButtonAction(button: PBAdaptiveButton)但没有帮助.
然后我查看了PBNavigationBarDelegate协议
protocol PBNavigationBarDelegate {
func navbarRightButtonAction(button:PBAdaptiveButton)
}
Run Code Online (Sandbox Code Playgroud)
我补充说,@objc protocol PBNavigationBarDelegate但它也没有帮助.
我不知道如何修复弃用警告.