WebView 的一些坑需要记录一下
enum
- JSONDecoder.KeyDecodingStrategy.custom(_:)
A key decoding strategy defined by the closure you supply.
官方文档举的例子很好
developer.apple.com/documentati…
class
- JSONDecoder
an object that decodes instances of a data type from JSON objects.
struct GroceryProduct: Codable {
var name: String
var points: Int
var description: String?
}
let json = """
{
"name": "Durian",
"points": 600,
"description": "A fruit with a distinctive scent."
}
""".data(using: .utf8)!
let decoder = JSONDecoder()
let product = try decoder.decode(GroceryProduct.self, from: json)
print(product.name) // Prints "Durian"
- NSTask
利用NSTask,我们可以在应用中调用外部程序或脚本并获得它的<执行状态和结果 NSTask最为常用的一个场景是为命令行操作提供图形化的界面。
function
- fatalError
在遇到确实因为输入的错误无法使程序继续运行的时候,我们一般考虑以产生致命错误 (fatalError) 的方式来终止程序。
- subscript(dynamicMember keyPath: ReferenceWritableKeyPath<WindowAttributes, Value>) -> Value
extension ImageCache {
subscript(url: URL) -> UIImage? {
get {
imageStore[url]
}
set {
imageStore[url] = newValue
}
}
}
property
- CALayer: strokeStart、strokeEnd
strokeStart它表示描线开始的地方占总路径的百分比,默认值是0 取值范围[0,1]
- isNaN
A Boolean value indicating whether the instance is NaN (“not a number”).
Because NaN is not equal to any value, including NaN, use this property instead of the equal-to operator (==) or not-equal-to operator (!=) to test whether a value is or is not NaN. For example:
let x = 0.0
let y = x * .infinity
// y is a NaN
// Comparing with the equal-to operator never returns 'true'
print(x == Double.nan)
// Prints "false"
print(y == Double.nan)
// Prints "false"
// Test with the 'isNaN' property instead
print(x.isNaN)
// Prints "false"
print(y.isNaN)
// Prints "true"
NaN 不等于任何值,包括 NaN,用任何值 == 或者 != NaN 永远返回 false
- CGFloat isZero
A Boolean value indicating whether the instance is equal to zero.
let x = -0.0
x.isZero // true
x == 0.0 // true
- round, floor, ceil
- inputAccessoryView, inputView
inputView 的作用就是让开发者提供自定义的 view 来获取用户的输入,当 UITextField 对象成为第一响应者的时候,系统会尝试唤出 inputView,如果 inputView 存在,就唤出开发者提供的 inputView;如果 inputView 不存在,也就是说这个属性的值是 nil(inputView 默认就是 nil),系统会唤出系统键盘。
inputAccessoryView 的作用,这个属性就是在 inputView 或者系统键盘上添加一个辅助的 view。
- keyDecodingStrategy
与这种情况关联的值是一个闭包,您可以使用该闭包将已解码的 JSON 对象中的键名映射到类型的编码键名。在解码过程中,对要解码的“可解码”值中的每个键调用一次闭包。该闭包与 CodingKey 实例数组一起调用,该数组代表达到要解码的值所需的键序列。
详见 Apple 官方文档:developer.apple.com/documentati…
protocol
-
CVarArg
-
Decodable, Decoder
Encoding and Decoding Custom Types
- ExpressibleByIntegerLiteral
A type that can be initialized with an integer literal.
// Type inferred as 'Int'
let cookieCount = 12
// An array of 'Int'
let chipsPerCookie = [21, 22, 25, 23, 24, 19]
// A floating-point value initialized using an integer literal
let redPercentage: Double = 1
// redPercentage == 1.0
keywords
@dynamicMemberLookup
这个特性中文可以叫动态查找成员。在使用@dynamicMemberLookup标记了对象后(对象、结构体、枚举、protocol),实现了subscript(dynamicMember member: String)方法后我们就可以访问到对象不存在的属性。如果访问到的属性不存在,就会调用到实现的 subscript(dynamicMember member: String)方法,key 作为 member 传入这个方法。
作者:没故事的卓同学 链接:juejin.cn/post/684490… 来源:掘金
tips
swift 除以 0
除零溢出
一个数除以0,或者对0求余数,就会产生一个错误;使用它们对应的可溢出的版本的运算符&/和&%进行除0操作时就会得到0值。
let x = 1
let y = x &/ 0
// y 等于 0
link
Custom subscripts in Swift explained with code examples
Better to be roughly right than precisely wrong: Rounding numbers with Swift
UIResponder 拾遗:inputView 和 inputAccessoryView
JSONDecoder.KeyDecodingStrategy.custom(_:)