1. Note writing:
1 init
1.1 Identify the characteristics of the objects generated by initialization
12 Creates a/an XXXX from XXXX
/// Creates an empty instance.
public init(a) {}
/// Create an instance from an array of `HTTPHeader`s. Duplicate case-insensitive names are collapsed into the last.
/// name and value encountered.
public init(_ headers: [HTTPHeader]) {
self.init()
headers.forEach { update($0)}}/// Create an instance form a `[String: String]`. Duplicate case-insensitive names are collapsed into the last name.
/// and value encounterd.
public init(_ dictionary: [String: String]) {
self.init()
dictionary.forEach{ update(HTTPHeader(name: $0.key, value: $0.value))}
}
Copy the code
2 Common Methods
2.1 Indicate the function of the method
2.2 Indicate the influence of this method on instance or class
2.3 Labeling the method will be generatedSide effects
andPay attention to the point
2.4 If there are parameters, press Enter between the parameter description and the parameter description
2.5 Indicate the meaning of the return value
2.6 If the returned value is optional, the value if it exists must be specified.
2.7 Return value enter a line
3 Calculating attributes
3.1 Indicate the meaning of the calculated attribute
3.2 Indicate the variable on which the calculated attribute depends, i.e. how it is calculated
4 other
4.1 All parameters and arguments need wrapping
4.2 End plus.
4.3 Pay attention to the triple order form
4.4 For assumptions: It is assumed, per Swift naming Conventions, that the first character of the key is lowercase.
The second agreement
1 ExpressibleByDictionaryLiteral
The implementation of this protocol can be easily initialized as a dictionary [:]
extension HTTPHeaders: ExpressibleByDictionaryLiteral {
public init(dictionaryLiteral elements: (String.String).) {
self.init()
elements.forEach{ update(name: $0.0, value: $0.1)}}}Copy the code
2 ExpressibleByArrayLiteral
Implementation of this protocol can be initialized like an array []
extension HTTPHeaders: ExpressibleByArrayLiteral {
public init(arrayLiteral elements: HTTPHeader...). {
self.init(elements)
}
}
Copy the code
3 Sequence & Collection
The implementation of these two protocols can be treated as arrays
extension HTTPHeaders: Sequence {
public func makeIterator(a) -> IndexingIterator"[HTTPHeader]> {
headers.makeIterator()
}
}
extension HTTPHeaders: Collection {
public var startIndex: Int {
headers.startIndex
}
public var endIndex: Int {
headers.endIndex
}
public subscript(position: Int) -> HTTPHeader {
headers[position]
}
public func index(after i: Int) -> Int {
headers.index(after: i)
}
}
Copy the code
headers.map
headers.isEmpty
for header in sessionHeaders
Copy the code
4 CustomStringConvertible
Implementation of the protocol can control the printout, as OC’s description method
Three Special Functions
subscript
public subscript(_ name: String) -> String?
Copy the code
This function can be implemented using subscripts
public subscript(_ name: String) -> String? {
get { value(for: name) }
set {
if let value = newValue {
update(name: name, value: value)
} else {
remove(name: name)
}
}
}
Copy the code
headers[header.name] = header.value
Copy the code
Write the read-only attribute
1 Calculate attributes
public var dictionary: [String: String] {
let namesAndValues = headers.map { ($0.name, $0.value) }
return Dictionary(namesAndValues, uniquingKeysWith:{ _, last in last })
}
Copy the code
2 Storage Properties
private(set) var name: String
Copy the code
Five simple factory writing method
public static var sortedKeys: JSONParameterEncoder {
let encoder = JSONEncoder()
encoder.outputFormatting = .sortedKeys
return JSONParameterEncoder(encoder: encoder)
}
Copy the code