小编nic*_*ing的帖子

我在 Swift 中对地址(罕见)进行地理编码时出错

我有一个 SearchBar,它更新某个 Binding 字符串,该字符串是地理编码的潜在位置匹配列表。有时,当我输入一个位置时,我会得到以下信息:

libswiftCore.dylib`_swift_runtime_on_report:
->  0x1054f7180 <+0>: pushq  %rbp //Error on this line says "= Thread 1: Fatal Error: Duplicate keys 
    of type 'GeocodedPlacemark' were found in a Dictionary. This usually means either that the type 
    violates Hashable's requirements, or that members of the dictionary were mutated after 
    insertion."
    0x1054f7181 <+1>: movq   %rsp, %rbp
    0x1054f7184 <+4>: popq   %rbp
    0x1054f7185 <+5>: retq   
    0x1054f7186 <+6>: nopw   %cs:(%rax,%rax)
Run Code Online (Sandbox Code Playgroud)

唯一的问题是,它让我不知道错误的来源在哪里......所有这些数字中是否有一些线索或在那一行找到的“pushq”关键字可以将我引导到它所指的字典?

旁注:此错误可能在每 15 次左右的搜索中发生一次,因此非常罕见。

搜索条码如下:

import SwiftUI
import Mapbox
import MapboxGeocoder

struct SearchBar: View …
Run Code Online (Sandbox Code Playgroud)

debugging dictionary swift swiftui

5
推荐指数
1
解决办法
226
查看次数

键盘偏移在 SwiftUI 2 中不再起作用

所以我有一个 ObservableObject 将发布的变量“currentHeight”设置为键盘的高度:

import Foundation
import SwiftUI

class KeyboardResponder: ObservableObject {

@Published var currentHeight: CGFloat = 0

var _center: NotificationCenter

init(center: NotificationCenter = .default) {
    _center = center
    _center.addObserver(self, selector: #selector(keyBoardWillShow(notification:)), name: UIResponder.keyboardWillShowNotification, object: nil)
    _center.addObserver(self, selector: #selector(keyBoardWillHide(notification:)), name: UIResponder.keyboardWillHideNotification, object: nil)
}

@objc func keyBoardWillShow(notification: Notification) {
    if let keyboardSize = (notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue {
        withAnimation {
           currentHeight = keyboardSize.height
        }
    }
    print("the KEYBOARD HEIGHT IS \(self.currentHeight)")
}

@objc func keyBoardWillHide(notification: Notification) {
    withAnimation {
       currentHeight = 0
    } …
Run Code Online (Sandbox Code Playgroud)

keyboard xcode offset swift swiftui

5
推荐指数
1
解决办法
459
查看次数

SwiftUI 颜色的十六进制代码/RGB 值?

是否有 SwiftUI 颜色的十六进制代码或 RGB 值的编译列表?我想知道 Color.purple 但似乎找不到任何好的来源。有没有办法以编程方式确定十六进制代码或 RGB 值?或者也许我可以看看颜色的一些属性?提前致谢!

rgb hex swift swiftui

3
推荐指数
1
解决办法
4232
查看次数

SwiftUI:如何仅在用户停止在 TextField 中输入时运行代码?

所以我试图制作一个搜索栏,它不会运行显示结果的代码,直到用户停止输入 2 秒(也就是当用户输入新字符时它应该重置某种计时器)。我尝试使用 .onChange() 和 AsyncAfter DispatchQueue 但它不起作用(我想我明白为什么当前的实现不起作用,但我不确定我是否以正确的方式解决了这个问题)......

struct SearchBarView: View {
    @State var text: String = ""
    @State var justUpdatedSuggestions: Bool = false
    var body: some View {
        ZStack {
            TextField("Search", text: self.$text).onChange(of: self.text, perform: { newText in
                appState.justUpdatedSuggestions = true
                DispatchQueue.main.asyncAfter(deadline: .now() + 3, execute: {
                    appState.justUpdatedSuggestions = false
                })
                if justUpdatedSuggestions == false {
                    //update suggestions
                }
            })
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

textfield swift dispatch-queue swiftui

3
推荐指数
2
解决办法
308
查看次数

如何定义在运行使用自定义初始值设定项的结构实例时应运行的自定义闭包 (SwiftUI)

因此,在 MapView 中,我需要有一个自定义初始值设定项,如下定义。

import SwiftUI
import Mapbox

struct MapView: UIViewRepresentable {
    var configure: (MGLMapView) -> () = { _ in }
    @Binding var showMoreDetails: Bool
    var VModel: ViewModel
    var token: String

    init(showMoreDetails: Binding<Bool>, VModel: ViewModel, token: String) {
        self.VModel = VModel
        self.token = token
        _showMoreDetails = showMoreDetails
    }

    //This function creates the actual view of the map
    func makeUIView(context: Context) -> MGLMapView {
        let map = MGLMapView()
        DispatchQueue.main.async {
            map.delegate = context.coordinator
            self.configure(map)
        }
        return map
    }

    //This function is …
Run Code Online (Sandbox Code Playgroud)

mkmapview mapbox swift swiftui

2
推荐指数
1
解决办法
513
查看次数

如何使用 SwiftUI 中的扩展将 Hashable 协议添加到 CLLocationCoordinate2D

所以我有一个自定义结构,其中一个属性为 String 类型,另一个为 CLLocationCoordinate2D 类型。显然,String 符合 Hashable,如果我可以扩展 CLLocationCoordinate2D 以符合 Hashable,我的自定义结构也将是 Hashable。这是我扩展 CLLocationCoordinate2D 的尝试:

extension CLLocationCoordinate2D {
    static func == (lhs: Self, rhs: Self) -> Bool {
        return lhs.latitude == rhs.latitude && lhs.longitude == rhs.longitude
    }
    
    func hash(into hasher: inout Hasher) {
        hasher.combine(self.latitude) //wasn't entirely sure what to put for the combine parameter but I saw similar things online
    }
}
Run Code Online (Sandbox Code Playgroud)

cllocation hashable swift swiftui

1
推荐指数
1
解决办法
169
查看次数