我目前正在使用 JSONDecoder 将 json 解析为 Swift 5 的对象。我现在才意识到 JSON 的一部分是错误的 JSON。它有一个带空格的字段名称:“帖子标题”。我不确定为什么会这样,我知道这样设置 JSON 是不好的做法,但在 JSON 方面我无能为力。有没有办法使用 JSON 解码器按原样获取该字段?
我已经对此进行了大量研究,但是由于这是一个错误的 json 问题,除了创建自定义解码器/解串器(我试图避免)之外,我在网上找不到太多东西。
JSON:
{
"Post Title":"Hello World"
}
Run Code Online (Sandbox Code Playgroud)
结构:
struct Post: Decodable {
var PostTitle: String
}
Run Code Online (Sandbox Code Playgroud)
解码器:
let jsonObject = try jsonDecoder.decode(Post.self, from: responseData)
Run Code Online (Sandbox Code Playgroud)
先感谢您!
我想在 Swift 5 中使用 Firebase,但出现错误消息。
无法构建 Objective-C 模块“Firebase”
我尝试了以下页面上的所有解决方案,但无法解决:
错误:无法构建 Objective-C 模块“Firebase”
https://github.com/firebase/quickstart-ios/issues/672
import UIKit
import Firebase
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
FirebaseApp.configure()
return true
}
}
Run Code Online (Sandbox Code Playgroud)
我期望构建成功,但实际上构建失败。
构建错误消息是:
找不到“FirebaseCore/FirebaseCore.h”文件无法构建Objective-C模块“Firebase”
并且,xcode 在以下位置显示错误消息
import Firebase
Run Code Online (Sandbox Code Playgroud)
在 AppDelegate.swift 中
免责声明:发布了很多类似的问题,但似乎没有一个与我自己的问题重复
假设我的应用程序从设计非常糟糕的 API 接收到一个 JSON,并且该 JSON 看起来像这样:
{
"matches": {
"page1": [{
"name": "John",
"surname": "Doe",
"interests": [{
"id": 13,
"text": "basketball"
},
{
"id": 37,
"text": "competitive knitting"
},
{
"id": 127,
"text": "romcoms"
}
]
}],
"page2": [{
"name": "Dwayne",
"surname": "Johnson",
"interests": [{
"id": 42,
"text": "sci-fi"
},
{
"id": 255,
"text": "round numbers"
}
]
}]
}
}
Run Code Online (Sandbox Code Playgroud)
如果我想从所有比赛中获得所有兴趣,在原生 Swift 功能中,我首先必须这样做:
struct MatchesData: Codable {
let matches: Matches
}
struct Matches: Codable {
let page1: Page1 …Run Code Online (Sandbox Code Playgroud) 我必须在 else 正文中编写什么才能从 firebase 存储中检索图像并将其显示在 UI 中?
public func downloadFile(url: String){
var img: Image?
let storage = Storage.storage()
let storageRef = storage.reference()
let imageRef = storageRef.child("Wallpapers/Cars/Wallpaper1.jpg")
imageRef.downloadURL { (url, err) in
if let err = err{
print("Error! Unable to download")
}
else{
}
}
}
Run Code Online (Sandbox Code Playgroud) 当应用程序需要注意并在 Dock 上弹跳时,如何实现类似于 macOS Dock 上的弹跳动画。SwiftUI 似乎只有缓动曲线和弹簧,它们并没有真正强调反弹的方式。我尝试了各种弹簧动画以及缓动曲线和时序曲线的组合来尝试使弹跳动画正常工作,但没有真正做到这一点。
最接近反弹动画的是插值弹簧,但这些动画的主要问题是它们在动画过程中过冲,而反弹动画则不会。
struct ContentView: View {
@State var bounce = false
@State private var initialVelocity:Double = 1
@State private var damping:Double = 1
@State private var stiffness:Double = 1
var body: some View {
VStack {
Circle().fill(Color.red).frame(width:50,height:50)
.offset(y: bounce ? 0 : -80)
.animation(.interpolatingSpring(stiffness: self.stiffness, damping: self.damping, initialVelocity: self.initialVelocity))
HStack(){
Text("stiffness")
Slider(value: $stiffness, in: 0...100)
}
HStack(){
Text("damping")
Slider(value: $damping, in: 0...100)
}
HStack(){
Text("initialVelocity")
Slider(value: $initialVelocity, in: 0...100)
}
Button("Animate" ){
self.bounce.toggle()
} …Run Code Online (Sandbox Code Playgroud) 在我的 iOS 应用程序中,我使用 SPM 来管理依赖项。其中之一是一个加载资源的库Bundle.module(Swift 5.3 中提供的一项新功能)。但现在我需要在我的应用程序中使用 CocoaPods。因此,我应该添加 CocoaPods 对这种依赖关系的支持。我知道我可以将Bundle.module的声明从resources_bundle_accessor.swift复制到库中。但它也应该支持SPM。我想知道如何检查是否应该使用在resource_bundle_accessor.swiftBundle.module中定义的函数,或者如果未生成resource_bundle_accessor.swift,则我的函数会执行相同的操作。
我们如何快速打印变量名?它类似于这个问题所讨论的Get a Swift Variable's Actual Name as String 我无法从上面的 QA 中弄清楚如何快速打印它以满足我的需要,如下所述
struct API {
static let apiEndPoint = "example.com/profile?"
static let apiEndPoint1 = "example.com/user?"
}
doSomething(endPoint: API.apiEndPoint)
doSomething(endPoint: API.apiEndPoint1)
func doSomething(endPoint: String) {
// print the variable name which should be apiEndPoint or endPoint1
}
Run Code Online (Sandbox Code Playgroud) Swift 5.x 中有什么变化吗?不确定我在这里做错了什么。
extension String {
var dateValue: Date? {
let dateAsString = "13:15"
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "HH:mm"
let date = dateFormatter.date(from: dateAsString) //date is being nil here
let Date24 = dateFormatter.string(from: date!)
print("24 hour formatted Date:",Date24)
return date
}
}
Run Code Online (Sandbox Code Playgroud) 在 iOS 中,我如何解析这个 JSON?
{
"status": 1,
"data": [
{
"month": "8-2019",
"jobs": [
{
"jobId": 4,
"jobTitle": "",
"jobDesc": "",
"jobDate": "26 Sep 2019",
"jobVenue": "Singapore",
"jobAccept": "N"
}
]
}
],
"message": "Success"
}
Run Code Online (Sandbox Code Playgroud) 
我想制作一个简单的表单并更改文本字段的背景颜色,但 Xcode 为我提供了.background(background: View)等选项,但没有.background(Color())。