我正在解码来自 URL 响应的 JSON(解码器)字符串,如下所示,并在解码过程中得到以下错误消息:
错误:
typeMismatch(Swift.String, Swift.DecodingError.Context(codingPath: [_JSONKey(stringValue: "Index 0", intValue: 0), CodingKeys(stringValue: "tags", intValue: nil), _JSONKey(stringValue: "Index 0") , intValue: 0)], debugDescription: "预期解码字符串,但找到了一个数字。",underlyingError: nil))
结构:
struct Wordpress: Codable {
let id: Int?
let date: String?
let date_gmt: String?
let guid: GUID?
let modified: String?
let modified_gmt: String?
let slug: String?
let status: String?
let type: String?
let link: String?
let title: Title?
let content: Content?
let excerpt: Excerpt?
let author: Int?
let featured_media: Int?
let comment_status: String?
let ping_status: …Run Code Online (Sandbox Code Playgroud) Swift 中最轻量级的数据类型是什么?我的意思是哪种类型占用的内存空间最少。是(布尔型、整数型还是字符型)?谢谢。
我在我创建的 API 中有一个 .map 命令,我很确定可以以更好的方式完成...
代码是:
.map {
var myTags = [Tag]()
for name in $0 {
myTags.append(Tag(name: name))
}
return myTags
}
Run Code Online (Sandbox Code Playgroud)
我可以在不必创建临时变量的情况下执行此操作吗??
谢谢!
我需要编写一个函数来交换任意两个变量的内容。这就是我目前所拥有的,但我收到错误“无法分配给值:‘a’是‘let’常量”。
func swap<T>(a: T, b: T) {
(a, b) = (b, a);
}
Run Code Online (Sandbox Code Playgroud)
有人可以解释为什么这不起作用并建议如何解决它吗?
谢谢。
我想今天我终于明白什么是 typealias 了。
我没有。
我们来看一个例子:
typealias Graph = [String: [String]]
let futurama: Graph = [
"you": ["bender", "hermes", "scruffy"],
"bender": ["hubert", "zoidberh"],
"hermes": ["hubert", "amy", "scruffy"],
"hubert": ["mom", "fry"],
"fry": ["leela"],
"leela": ["brannigan", "nibbler", "scruffy"],
"amy": ["kif"],
"brannigan": ["kif"],
"zoidberh": [],
"kif": [],
"mom": [],
"nibbler": [],
"scruffy": []
]
extension Graph {
// Breadth First Search
func bfs(from start: String, to finish: String) -> [String]? {
// Implementation of this graph algorithm here
}
}
print(
futurama.bfs(from: "you", to: …Run Code Online (Sandbox Code Playgroud) 我一直在玩苹果的Combine框架,在那里我找到了几个运营商map() & tryMap()或allSatisfy() & tryAllSatisfy
Combine 中的许多操作符都遵循这种模式,我想知道这意味着什么。
我经历过很多运营商,他们中的大多数都有前缀try. 如果有人能以最简单的方式让我知道,那将非常有帮助。
谢谢
我正在学习斯坦福大学的 CS193p 开发 iOS 应用程序在线课程。\n我使用的是 Xcode 11.5。(我没有更新,因为这是课程讲师(Paul Heagarty)正在使用的版本。)
\n我正在尝试做作业3(设定游戏)。目前(为了避免重复代码)我正在尝试替换此片段:
\n VStack {\n ForEach(0..<numberOfShapes) { index in\n if self.card.shape == .diamond {\n ZStack {\n Diamond().fill()\n .opacity(self.opacity)\n Diamond().stroke(lineWidth: self.shapeEdgeLineWidth)\n }\n }\n if self.card.shape == .squiggle {\n ZStack {\n Rectangle().fill()\n Rectangle().stroke(lineWidth: self.shapeEdgeLineWidth)\n }\n }\n if self.card.shape == .oval {\n ZStack {\n Ellipse().fill()\n Ellipse().stroke(lineWidth: self.shapeEdgeLineWidth)\n }\n }\n }\n }\nRun Code Online (Sandbox Code Playgroud)\n有了这个片段:
\n VStack {\n ForEach(0..<numberOfShapes) { index in\n ZStack {\n shape(self.card.shape).opacity(self.opacity)\n shape(self.card.shape).stroke(lineWidth: 2.5) // ERROR here: Value of type …Run Code Online (Sandbox Code Playgroud) 我注意到我有很多重复的代码来获取/发送 JSON 到我的应用程序的 API,但唯一不同的是正在(反)序列化的实体。所以我想出了以下设计(为简洁起见,只有 GET 方法):
HTTPClient.swift
func getEntityJSON<T: JSONSerializable>(
type: T.Type,
url: String,
completionHandler: @escaping (_ result: T?,
_ headers: [String: String]?,
_ statusCode: Int?,
_ error: Error?) -> Void) {
HttpClient.sharedInstance().getJSON(url, completionHandler: { (jsonData, headers, statusCode, error) in
if let error = error {
completionHandler(nil, headers, statusCode, error)
} else {
if let entityData = jsonData {
completionHandler(T.fromJSON(data: entityData), headers, statusCode, nil)
}
}
})
}
Run Code Online (Sandbox Code Playgroud)
打算像这样使用它:
HTTPClient.sharedInstance().getEntity(type: Translation, url: url) { (translation, _, _, _) in
// …Run Code Online (Sandbox Code Playgroud) 我正在尝试调用一个如下所示的函数
func customTextField(placeholder: String, text: Binding<String>) -> some View {
ZStack(alignment: .leading){
if text.isEmpty{
VStack {
Text(placeholder)
.font(.system(size: 14))
.fontWeight(.regular)
.foregroundColor(Color(red: 0.5803921568627451, green: 0.5803921568627451, blue: 0.5803921568627451))
.padding()
if placeholder == "Your message type here..."{
Spacer()
}
}
}
TextField("", text: text)
.font(.system(size: 14))
.foregroundColor(.black)
.padding()
}
.frame(height: placeholder == "Your message type here..." ? 170 : 54)
.background(RoundedRectangle(cornerRadius: 16).stroke(text.isEmpty ? Color.clear : Color("orange")))
.background(Color.white)
.cornerRadius(16)
}
Run Code Online (Sandbox Code Playgroud)
但因为text是Binding<String>我不能使用.isEmpty()它。我不想为它创建另一个结构视图。对此最好的方法是什么?
let data = [10.0, 20.0, 30.0, 40.0, 50.0]
var bookQty = [String : Double]()
enum activityTypeEnum : String {
case Physics = "Physics"
case Math = "Math"
case Science = "Science"
case Others = "Others"
}
for i in 0..<data.count {
print("Data[\(i)]:\(data[i])")
bookQty["Science"] = bookQty["Science"] ?? 0 + data[i]
}
print("bookQty:\(bookQty)") // Results in bookQty:["Science": 10.0]
Run Code Online (Sandbox Code Playgroud)
正在做
bookQty["Ride"] = bookQty["Science"] + data[i]
Run Code Online (Sandbox Code Playgroud)
导致此错误消息“可选类型‘Double’的值?” 必须解包为“Double”类型的值”
所以我做了
bookQty["Science"] = bookQty["Science"] ?? 0 + data[i]
Run Code Online (Sandbox Code Playgroud)
但这会导致错误的输出。(我希望它是所有数量的总和data = []
注意:我不想强行打开它。
swift ×10
ios ×2
swiftui ×2
codable ×1
combine ×1
cs193p ×1
generics ×1
option-type ×1
type-alias ×1
types ×1