在控制台上从XCode 10.2(在Swift 5.0迁移之前和之后)运行时,该应用在启动时崩溃
libswiftCore.dylib的此副本需要12.2.0之前的操作系统版本。
我了解此错误,但不确定要解决此问题需要什么。
从Xcode 10.2开始,使用我在Objective-C中定义的枚举时,但是在Swift 5 switch语句中,即使我用尽了所有可能的枚举值,我也会收到以下警告。
Switch covers known cases, but 'MyObjectiveCEnumName' may have additional
unknown values
Run Code Online (Sandbox Code Playgroud)
Xcode告诉我应该通过以下方法解决此问题
Handle unknown values using "@unknown default"
Run Code Online (Sandbox Code Playgroud)
为什么会发生这种情况,我该怎么办?
Objective-C列举
typedef NS_ENUM(NSUInteger, CardColor) {
CardColorBlack,
CardColorRed
};
Run Code Online (Sandbox Code Playgroud)
Swift 5开关声明
var cardColor: CardColor = .black
switch (cardColor) {
case .black:
print("black")
case .red:
print("red")
}
Run Code Online (Sandbox Code Playgroud) 我LoadingView使用SwiftUI 构建了一个用于在我从API提取远程数据时在应用程序中显示一些加载内容的功能。我正在使用Xcode 11.0 beta 5。
这是LoadingView:
struct LoadingView<Content>: View where Content: View {
@Binding var isShowing: Bool
var content: () -> Content
var body: some View {
GeometryReader { geometry in
ZStack(alignment: .center) {
self.content()
.disabled(self.isShowing)
.blur(radius: self.isShowing ? 3 : 0)
VStack {
Text("Loading...")
ActivityIndicator(isAnimating: .constant(true), style: .large)
}
.frame(width: geometry.size.width / 2,
height: geometry.size.height / 5)
.background(Color.white)
.foregroundColor(Color.primary)
.cornerRadius(5)
.opacity(self.isShowing ? 1 : 0)
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的数据存储。它被声明为ObservableObject具有多个@Published属性。它还从API进行一些远程获取:
class …Run Code Online (Sandbox Code Playgroud) 我有以下带有2个项目的TabBarController。显示正确。
setupItems()当something更改其值时,我正在从另一个控制器调用该函数。
该函数已正确调用,问题navFirstController.tabBarItem.image是未更新。
class TabBarController: UITabBarController {
override func viewDidLoad() {
super.viewDidLoad()
setupItems()
}
func setupItems() {
let scale: CGFloat = 0.35
let navFirstController = UINavigationController(rootViewController: FirstController())
let navSecondController = UINavigationController(rootViewController: SecondController())
navSecondController.tabBarItem.image = UIImage.scale(image: UIImage(named: "image2")!, by: scale)
navSecondController.tabBarItem.imageInsets = UIEdgeInsets(top: 8, left: 0, bottom: -8, right: 0)
if something == true {
navFirstController.tabBarItem.image = UIImage.scale(image: UIImage(named: "image1")!, by: scale)
} else {
navFirstController.tabBarItem.image = UIImage.scale(image: UIImage(named: "image3")!, by: scale)
}
navFirstController.tabBarItem.imageInsets = UIEdgeInsets(top: …Run Code Online (Sandbox Code Playgroud) 我是一个快速学习者。我使用 SwiftUI,它是一个结构体,我必须实现一个 WKWebView,并且其中的 url 正在动态更改。我必须捕捉这些不断变化的网址,但我尝试过的解决方案不起作用。
例如:https : //stackoverflow.com/a/48273950/10088243 我试过这个代码块,但它不起作用,它给了我一些编译器错误:
import SwiftUI
import WebKit
struct ContentView: UIViewRepresentable, WKNavigationDelegate {
let request = URLRequest(url: URL(string: "https://apple.com")!)
func makeUIView(context: Context) -> WKWebView {
let preferences = WKPreferences()
preferences.javaScriptEnabled = true
preferences.javaScriptCanOpenWindowsAutomatically = true
let configuration = WKWebViewConfiguration()
configuration.preferences = preferences
let webView = WKWebView(frame: .zero, configuration: configuration)
webView.allowsBackForwardNavigationGestures = true
return webView
}
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
// 'override' can only …Run Code Online (Sandbox Code Playgroud) 我想在 SwiftUI 中的 NavigationBar 的 titleView 中设置图像,就像我们在 UIKit 中所做的那样
navigationItem.titleView = UIImageView(image: UIImage(named: "logo"))
Run Code Online (Sandbox Code Playgroud)
这就是我们在 UIKit 中的做法。
有人知道怎么做吗?
使用 Realitykit,尝试将月球实体的材质更改为自定义 .jpg,然后点击屏幕以基于 hitTest 生成该对象。当我点击并在调试中收到以下错误时,没有任何显示:[Collision] Bad paramater (SphereRadius),value = 0.000000,passed to shape creation。
import UIKit
import RealityKit
class ViewController: UIViewController {
@IBOutlet var arView: ARView!
override func viewDidLoad() {
super.viewDidLoad()
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
if let touchLocation = touches.first?.location(in: arView){
let hitTest = arView.hitTest(touchLocation)
if let hitResult = hitTest.first {
addObject(at: hitResult)
}
}
}
func addObject(at hitResult: CollisionCastHit) {
let moonAnchor = try! Galaxy.loadWorld()
let moon = moonAnchor.moon! as! ModelEntity
var material …Run Code Online (Sandbox Code Playgroud) 如何使用无线连接计算两个 iOS 设备之间的距离。
我发现我们可以使用BLE,使用RSSI数字进行计算。
但是设备的范围各不相同,放置在远处房间的设备无法被发现。
我的要求是计算房间中存在的距离设备。
我已经研究了多点连接框架,但没有数字这样的东西RSSI。
提前致谢。
我正在尝试通过以下代码使用 @Observable 和 @Environment 来使用新的 Swift 宏:
import SwiftUI
import Observation
@Observable class Note {
var text = ""
}
struct ContentView: View {
@State var note = Note()
var body: some View {
FormView()
.environment(note)
}
}
struct FormView: View {
@Environment(Note.self) var note
var body: some View {
Form {
TextField("write here", text: $note.text)
}
}
}
Run Code Online (Sandbox Code Playgroud)
但是,无法构建并出现以下错误:
Cannot find '$note' in scope
删除$in 的$note结果是:
Cannot convert value of type 'String' to expected argument …
我有一个 swift UIHostingController,它呈现 SwiftUI。我在视图中调用了一个函数,它确实出现了,它构建得很好,但没有创建预期的输出。
class LoginView: UIHostingController<LoginViewComponent> {
required init?(coder: NSCoder) {
super.init(coder: coder, rootView: LoginViewComponent())
}
override func viewDidAppear(_ animated: Bool) {
sessionHandler()
}
func sessionHandler(){
let user = User()
if user.isLoggedIn(){
view.isUserInteractionEnabled = false
print("Authenticating Session")
self.rootView.loginState(state: "success")
}else{
view.isUserInteractionEnabled = true
print("Needs Logging in")
}
}
}
Run Code Online (Sandbox Code Playgroud)
函数 ("loginState(state: "success")") 在 SwiftUI 视图类中调用时有效,但是当从托管控制器调用时它不起作用。
任何帮助将不胜感激。
swift5 ×10
swift ×6
ios ×5
swiftui ×4
xcode ×2
xcode10.2 ×2
arkit ×1
combine ×1
objective-c ×1
realitykit ×1
swift-macro ×1
wifi ×1
wkwebview ×1