Skip to content

Commit 7c713e4

Browse files
committed
Replace deprecated flatMap() with compactMap()
1 parent 3206f9d commit 7c713e4

6 files changed

+8
-8
lines changed

Foundation/HTTPCookie.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ open class HTTPCookie : NSObject {
268268

269269
if let portString = properties[.port] as? String, _version == 1 {
270270
_portList = portString.split(separator: ",")
271-
.flatMap { Int(String($0)) }
271+
.compactMap { Int(String($0)) }
272272
.map { NSNumber(value: $0) }
273273
} else {
274274
_portList = nil

Foundation/JSONEncoder.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -1043,7 +1043,7 @@ fileprivate struct _JSONKeyedDecodingContainer<K : CodingKey> : KeyedDecodingCon
10431043
// MARK: - KeyedDecodingContainerProtocol Methods
10441044

10451045
public var allKeys: [Key] {
1046-
return self.container.keys.flatMap { Key(stringValue: $0) }
1046+
return self.container.keys.compactMap { Key(stringValue: $0) }
10471047
}
10481048

10491049
public func contains(_ key: Key) -> Bool {

Foundation/ProcessInfo.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ open class ProcessInfo: NSObject {
9292
return OperatingSystemVersion(majorVersion: fallbackMajor, minorVersion: fallbackMinor, patchVersion: fallbackPatch)
9393
}
9494

95-
let versionComponents = productVersion._swiftObject.split(separator: ".").map(String.init).flatMap({ Int($0) })
95+
let versionComponents = productVersion._swiftObject.split(separator: ".").map(String.init).compactMap({ Int($0) })
9696
let majorVersion = versionComponents.dropFirst(0).first ?? fallbackMajor
9797
let minorVersion = versionComponents.dropFirst(1).first ?? fallbackMinor
9898
let patchVersion = versionComponents.dropFirst(2).first ?? fallbackPatch

Foundation/XMLElement.swift

+3-3
Original file line numberDiff line numberDiff line change
@@ -69,15 +69,15 @@ open class XMLElement: XMLNode {
6969
@abstract Returns all of the child elements that match this name.
7070
*/
7171
open func elements(forName name: String) -> [XMLElement] {
72-
return self.filter({ _CFXMLNodeGetType($0._xmlNode) == _kCFXMLTypeElement }).filter({ $0.name == name }).flatMap({ $0 as? XMLElement })
72+
return self.filter({ _CFXMLNodeGetType($0._xmlNode) == _kCFXMLTypeElement }).filter({ $0.name == name }).compactMap({ $0 as? XMLElement })
7373
}
7474

7575
/*!
7676
@method elementsForLocalName:URI
7777
@abstract Returns all of the child elements that match this localname URI pair.
7878
*/
7979
open func elements(forLocalName localName: String, uri URI: String?) -> [XMLElement] {
80-
return self.filter({ _CFXMLNodeGetType($0._xmlNode) == _kCFXMLTypeElement }).filter({ $0.localName == localName && $0.uri == uri }).flatMap({ $0 as? XMLElement })
80+
return self.filter({ _CFXMLNodeGetType($0._xmlNode) == _kCFXMLTypeElement }).filter({ $0.localName == localName && $0.uri == uri }).compactMap({ $0 as? XMLElement })
8181
}
8282

8383
/*!
@@ -189,7 +189,7 @@ open class XMLElement: XMLNode {
189189
@abstract Adds a namespace. Namespaces with duplicate names are not added.
190190
*/
191191
open func addNamespace(_ aNamespace: XMLNode) {
192-
if ((namespaces ?? []).flatMap({ $0.name }).contains(aNamespace.name ?? "")) {
192+
if ((namespaces ?? []).compactMap({ $0.name }).contains(aNamespace.name ?? "")) {
193193
return
194194
}
195195
_CFXMLAddNamespace(_xmlNode, aNamespace._xmlNode)

TestFoundation/TestJSONSerialization.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -552,7 +552,7 @@ extension TestJSONSerialization {
552552
return
553553
}
554554
let res = try getjsonObjectResult(data, objectType) as? [Any]
555-
let result = res?.flatMap { $0 as? String }
555+
let result = res?.compactMap { $0 as? String }
556556
XCTAssertEqual(result?[0], "\"")
557557
XCTAssertEqual(result?[1], "\\")
558558
XCTAssertEqual(result?[2], "/")

TestFoundation/TestXMLDocument.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,7 @@ class TestXMLDocument : XCTestCase {
356356
otherDoc.rootElement()?.namespaces = [XMLNode.namespace(withName: "R", stringValue: "http://example.com/rnamespace") as! XMLNode, XMLNode.namespace(withName: "F", stringValue: "http://example.com/fakenamespace") as! XMLNode]
357357
XCTAssert(otherDoc.rootElement()?.namespaces?.count == 2)
358358
let namespaces: [XMLNode]? = otherDoc.rootElement()?.namespaces
359-
let names: [String]? = namespaces?.flatMap { $0.name }
359+
let names: [String]? = namespaces?.compactMap { $0.name }
360360
XCTAssertNotNil(names)
361361
XCTAssert(names![0] == "R" && names![1] == "F")
362362
otherDoc.rootElement()?.namespaces = nil

0 commit comments

Comments
 (0)