Skip to content

Commit e37a2e6

Browse files
authored
Merge pull request swiftlang#1257 from ianpartridge/api-fixup-4.0
[4.0] Darwin API compatibility fixes
2 parents 47cf14b + ba1f2a5 commit e37a2e6

8 files changed

+41
-38
lines changed

Foundation/ByteCountFormatter.swift

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ extension ByteCountFormatter {
1313
public let rawValue : UInt
1414
public init(rawValue: UInt) { self.rawValue = rawValue }
1515

16-
// This causes default units appropriate for the platform to be used. Specifying any units explicitly causes just those units to be used in showing the number.
17-
public static let useDefault = Units(rawValue: 0)
1816
// Specifying any of the following causes the specified units to be used in showing the number.
1917
public static let useBytes = Units(rawValue: 1 << 0)
2018
public static let useKB = Units(rawValue: 1 << 1)
@@ -50,9 +48,9 @@ open class ByteCountFormatter : Formatter {
5048
NSUnimplemented()
5149
}
5250

53-
/* Specify the units that can be used in the output. If ByteCountFormatter.Units.useDefault, uses platform-appropriate settings; otherwise will only use the specified units. This is the default value. Note that ZB and YB cannot be covered by the range of possible values, but you can still choose to use these units to get fractional display ("0.0035 ZB" for instance).
51+
/* Specify the units that can be used in the output. If ByteCountFormatter.Units is empty, uses platform-appropriate settings; otherwise will only use the specified units. This is the default value. Note that ZB and YB cannot be covered by the range of possible values, but you can still choose to use these units to get fractional display ("0.0035 ZB" for instance).
5452
*/
55-
open var allowedUnits: Units = .useDefault
53+
open var allowedUnits: Units = []
5654

5755
/* Specify how the count is displayed by indicating the number of bytes to be used for kilobyte. The default setting is ByteCountFormatter.CountStyle.fileCount, which is the system specific value for file and storage sizes.
5856
*/
@@ -127,52 +125,52 @@ open class ByteCountFormatter : Formatter {
127125
*/
128126
private func convertValue(fromByteCount byteCount: Int64, for byteSize: [Unit: Double]) -> String {
129127
let byte = Double(byteCount)
130-
if byte == 0, allowsNonnumericFormatting, allowedUnits == .useDefault, includesUnit, includesCount {
128+
if byte == 0, allowsNonnumericFormatting, allowedUnits == [], includesUnit, includesCount {
131129
return partsToIncludeFor(value: "Zero", unit: Unit.KB)
132130
} else if byte == 1 || byte == -1 {
133-
if allowedUnits.contains(.useAll) || allowedUnits == .useDefault {
131+
if allowedUnits.contains(.useAll) || allowedUnits == [] {
134132
return formatNumberFor(bytes: byte, unit: Unit.byte)
135133
} else {
136134
return valueToUseFor(byteCount: byte, unit: allowedUnits)
137135
}
138136
} else if byte < byteSize[Unit.KB]! && byte > -byteSize[Unit.KB]!{
139-
if allowedUnits.contains(.useAll) || allowedUnits == .useDefault {
137+
if allowedUnits.contains(.useAll) || allowedUnits == [] {
140138
return formatNumberFor(bytes: byte, unit: Unit.bytes)
141139
} else {
142140
return valueToUseFor(byteCount: byte, unit: allowedUnits)
143141
}
144142
} else if byte < byteSize[Unit.MB]! && byte > -byteSize[Unit.MB]! {
145-
if allowedUnits.contains(.useAll) || allowedUnits == .useDefault {
143+
if allowedUnits.contains(.useAll) || allowedUnits == [] {
146144
return divide(byte, by: byteSize, for: .KB)
147145
}
148146
return valueToUseFor(byteCount: byte, unit: allowedUnits)
149147

150148
} else if byte < byteSize[Unit.GB]! && byte > -byteSize[Unit.GB]! {
151-
if allowedUnits.contains(.useAll) || allowedUnits == .useDefault {
149+
if allowedUnits.contains(.useAll) || allowedUnits == [] {
152150
return divide(byte, by: byteSize, for: .MB)
153151
}
154152
return valueToUseFor(byteCount: byte, unit: allowedUnits)
155153

156154
} else if byte < byteSize[Unit.TB]! && byte > -byteSize[Unit.TB]! {
157-
if allowedUnits.contains(.useAll) || allowedUnits == .useDefault {
155+
if allowedUnits.contains(.useAll) || allowedUnits == [] {
158156
return divide(byte, by: byteSize, for: .GB)
159157
}
160158
return valueToUseFor(byteCount: byte, unit: allowedUnits)
161159

162160
} else if byte < byteSize[Unit.PB]! && byte > -byteSize[Unit.PB]! {
163-
if allowedUnits.contains(.useAll) || allowedUnits == .useDefault {
161+
if allowedUnits.contains(.useAll) || allowedUnits == [] {
164162
return divide(byte, by: byteSize, for: .TB)
165163
}
166164
return valueToUseFor(byteCount: byte, unit: allowedUnits)
167165

168166
} else if byte < byteSize[Unit.EB]! && byte > -byteSize[Unit.EB]! {
169-
if allowedUnits.contains(.useAll) || allowedUnits == .useDefault {
167+
if allowedUnits.contains(.useAll) || allowedUnits == [] {
170168
return divide(byte, by: byteSize, for: .PB)
171169
}
172170
return valueToUseFor(byteCount: byte, unit: allowedUnits)
173171

174172
} else {
175-
if allowedUnits.contains(.useAll) || allowedUnits == .useDefault {
173+
if allowedUnits.contains(.useAll) || allowedUnits == [] {
176174
return divide(byte, by: byteSize, for: .EB)
177175
}
178176
return valueToUseFor(byteCount: byte, unit: allowedUnits)
@@ -297,7 +295,7 @@ open class ByteCountFormatter : Formatter {
297295
} else {
298296
if lengthOfInt(number: Int(bytes)) == 3 {
299297
numberFormatter.usesSignificantDigits = false
300-
numberFormatter.maximumFractionDigits = 1
298+
numberFormatter.maximumFractionDigits = 0
301299
} else {
302300
numberFormatter.maximumSignificantDigits = 3
303301
numberFormatter.minimumSignificantDigits = 3
@@ -376,7 +374,7 @@ open class ByteCountFormatter : Formatter {
376374
} else if includesCount, includesUnit {
377375
return "\(value) \(unit)"
378376
} else if includesCount, !includesUnit {
379-
if value == "Zero", allowedUnits == .useDefault {
377+
if value == "Zero", allowedUnits == [] {
380378
return "0"
381379
} else {
382380
return value

Foundation/JSONEncoder.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ open class JSONEncoder {
6565
/// The strategy to use for encoding `Data` values.
6666
public enum DataEncodingStrategy {
6767
/// Encoded the `Data` as a Base64-encoded string. This is the default strategy.
68-
case base64Encode
68+
case base64
6969

7070
/// Encode the `Data` as a custom value encoded by the given closure.
7171
///
@@ -88,8 +88,8 @@ open class JSONEncoder {
8888
/// The strategy to use in encoding dates. Defaults to `.deferredToDate`.
8989
open var dateEncodingStrategy: DateEncodingStrategy = .deferredToDate
9090

91-
/// The strategy to use in encoding binary data. Defaults to `.base64Encode`.
92-
open var dataEncodingStrategy: DataEncodingStrategy = .base64Encode
91+
/// The strategy to use in encoding binary data. Defaults to `.base64`.
92+
open var dataEncodingStrategy: DataEncodingStrategy = .base64
9393

9494
/// The strategy to use in encoding non-conforming numbers. Defaults to `.throw`.
9595
open var nonConformingFloatEncodingStrategy: NonConformingFloatEncodingStrategy = .throw
@@ -672,7 +672,7 @@ extension _JSONEncoder {
672672

673673
fileprivate func box(_ data: Data) throws -> NSObject {
674674
switch self.options.dataEncodingStrategy {
675-
case .base64Encode:
675+
case .base64:
676676
return NSString(string: data.base64EncodedString())
677677

678678
case .custom(let closure):
@@ -827,7 +827,7 @@ open class JSONDecoder {
827827
/// The strategy to use for decoding `Data` values.
828828
public enum DataDecodingStrategy {
829829
/// Decode the `Data` from a Base64-encoded string. This is the default strategy.
830-
case base64Decode
830+
case base64
831831

832832
/// Decode the `Data` as a custom value decoded by the given closure.
833833
case custom((_ decoder: Decoder) throws -> Data)
@@ -845,8 +845,8 @@ open class JSONDecoder {
845845
/// The strategy to use in decoding dates. Defaults to `.deferredToDate`.
846846
open var dateDecodingStrategy: DateDecodingStrategy = .deferredToDate
847847

848-
/// The strategy to use in decoding binary data. Defaults to `.base64Decode`.
849-
open var dataDecodingStrategy: DataDecodingStrategy = .base64Decode
848+
/// The strategy to use in decoding binary data. Defaults to `.base64`.
849+
open var dataDecodingStrategy: DataDecodingStrategy = .base64
850850

851851
/// The strategy to use in decoding non-conforming numbers. Defaults to `.throw`.
852852
open var nonConformingFloatDecodingStrategy: NonConformingFloatDecodingStrategy = .throw
@@ -2042,7 +2042,7 @@ extension _JSONDecoder {
20422042
guard !(value is NSNull) else { return nil }
20432043

20442044
switch self.options.dataDecodingStrategy {
2045-
case .base64Decode:
2045+
case .base64:
20462046
guard let string = value as? String else {
20472047
throw DecodingError._typeMismatch(at: self.codingPath, expectation: type, reality: value)
20482048
}

Foundation/NSNotification.swift

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ open class NotificationCenter: NSObject {
169169
}
170170
}
171171

172-
open func post(name aName: Notification.Name, object anObject: Any?, userInfo aUserInfo: [AnyHashable : Any]? = nil) {
172+
open func post(name aName: NSNotification.Name, object anObject: Any?, userInfo aUserInfo: [AnyHashable : Any]? = nil) {
173173
let notification = Notification(name: aName, object: anObject, userInfo: aUserInfo)
174174
post(notification)
175175
}
@@ -187,8 +187,13 @@ open class NotificationCenter: NSObject {
187187
self._observers = _observers.filterOutObserver(observer, name: aName, object: object)
188188
})
189189
}
190-
191-
open func addObserver(forName name: Notification.Name?, object obj: Any?, queue: OperationQueue?, usingBlock block: @escaping (Notification) -> Void) -> NSObjectProtocol {
190+
191+
@available(*,obsoleted:4.0,renamed:"addObserver(forName:object:queue:using:)")
192+
open func addObserver(forName name: NSNotification.Name?, object obj: Any?, queue: OperationQueue?, usingBlock block: @escaping (Notification) -> Void) -> NSObjectProtocol {
193+
return addObserver(forName: name, object: obj, queue: queue, using: block)
194+
}
195+
196+
open func addObserver(forName name: NSNotification.Name?, object obj: Any?, queue: OperationQueue?, using block: @escaping (Notification) -> Void) -> NSObjectProtocol {
192197
let object = NSObject()
193198

194199
let newObserver = NSNotificationReceiver()

Foundation/NSTextCheckingResult.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ internal class _NSRegularExpressionNSTextCheckingResultResult : NSTextCheckingRe
8787

8888
extension NSTextCheckingResult {
8989

90-
public func resultByAdjustingRangesWithOffset(_ offset: Int) -> NSTextCheckingResult {
90+
public func adjustingRanges(offset: Int) -> NSTextCheckingResult {
9191
let count = self.numberOfRanges
9292
var newRanges = [NSRange]()
9393
for idx in 0..<count {

Foundation/Stream.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -241,11 +241,11 @@ extension Stream {
241241
#endif
242242

243243
extension StreamDelegate {
244-
func stream(_ aStream: Stream, handleEvent eventCode: Stream.Event) { }
244+
func stream(_ aStream: Stream, handle eventCode: Stream.Event) { }
245245
}
246246

247247
public protocol StreamDelegate : class {
248-
func stream(_ aStream: Stream, handleEvent eventCode: Stream.Event)
248+
func stream(_ aStream: Stream, handle eventCode: Stream.Event)
249249
}
250250

251251
// MARK: -

TestFoundation/TestByteCountFormatter.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ class TestByteCountFormatter : XCTestCase {
4949

5050
func test_DefaultValues() {
5151
let formatter = ByteCountFormatter()
52-
XCTAssertEqual(formatter.allowedUnits, ByteCountFormatter.Units.useDefault)
52+
XCTAssertEqual(formatter.allowedUnits, [])
5353
XCTAssertEqual(formatter.countStyle, ByteCountFormatter.CountStyle.file)
5454
XCTAssertEqual(formatter.allowsNonnumericFormatting, true)
5555
XCTAssertEqual(formatter.includesUnit, true)
@@ -68,7 +68,7 @@ class TestByteCountFormatter : XCTestCase {
6868
formatter.allowedUnits = .useGB
6969
XCTAssertEqual(formatter.string(fromByteCount: 0), "Zero KB")
7070

71-
formatter.allowedUnits = .useDefault
71+
formatter.allowedUnits = []
7272
formatter.allowsNonnumericFormatting = false
7373
XCTAssertEqual(formatter.string(fromByteCount: 0), "0 bytes")
7474

@@ -91,7 +91,7 @@ class TestByteCountFormatter : XCTestCase {
9191
formatter.allowedUnits = .useGB
9292
XCTAssertEqual(formatter.string(fromByteCount: 1), "0 GB")
9393

94-
formatter.allowedUnits = .useDefault
94+
formatter.allowedUnits = []
9595
formatter.isAdaptive = false
9696
XCTAssertEqual(formatter.string(fromByteCount: 1), "1 byte")
9797

@@ -365,7 +365,7 @@ class TestByteCountFormatter : XCTestCase {
365365
XCTAssertEqual(formatter.string(fromByteCount: 11999), "12.0 KB")
366366
XCTAssertEqual(formatter.string(fromByteCount: 900000), "900 KB")
367367
XCTAssertEqual(formatter.string(fromByteCount: 12345678), "12.3 MB")
368-
XCTAssertEqual(formatter.string(fromByteCount: 123456789), "123.5 MB")
368+
XCTAssertEqual(formatter.string(fromByteCount: 123456789), "123 MB")
369369
XCTAssertEqual(formatter.string(fromByteCount: 1234567898), "1.23 GB")
370370
XCTAssertEqual(formatter.string(fromByteCount: 12345678987), "12.3 GB")
371371
}

TestFoundation/TestJSONEncoder.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -446,8 +446,8 @@ class TestJSONEncoder : XCTestCase {
446446
outputFormatting: JSONEncoder.OutputFormatting = [],
447447
dateEncodingStrategy: JSONEncoder.DateEncodingStrategy = .deferredToDate,
448448
dateDecodingStrategy: JSONDecoder.DateDecodingStrategy = .deferredToDate,
449-
dataEncodingStrategy: JSONEncoder.DataEncodingStrategy = .base64Encode,
450-
dataDecodingStrategy: JSONDecoder.DataDecodingStrategy = .base64Decode,
449+
dataEncodingStrategy: JSONEncoder.DataEncodingStrategy = .base64,
450+
dataDecodingStrategy: JSONDecoder.DataDecodingStrategy = .base64,
451451
nonConformingFloatEncodingStrategy: JSONEncoder.NonConformingFloatEncodingStrategy = .throw,
452452
nonConformingFloatDecodingStrategy: JSONDecoder.NonConformingFloatDecodingStrategy = .throw) where T : Codable, T : Equatable {
453453
var payload: Data! = nil

TestFoundation/TestNSTextCheckingResult.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,17 +34,17 @@ class TestNSTextCheckingResult: XCTestCase {
3434
let searchRange = NSMakeRange(0,7)
3535
let match: NSTextCheckingResult = regex.firstMatch(in: searchString, options: searchOptions, range: searchRange)!
3636
//Positive offset
37-
var result = match.resultByAdjustingRangesWithOffset(1)
37+
var result = match.adjustingRanges(offset: 1)
3838
XCTAssertEqual(result.range(at: 0).location, 6)
3939
XCTAssertEqual(result.range(at: 1).location, NSNotFound)
4040
XCTAssertEqual(result.range(at: 2).location, 6)
4141
//Negative offset
42-
result = match.resultByAdjustingRangesWithOffset(-2)
42+
result = match.adjustingRanges(offset: -2)
4343
XCTAssertEqual(result.range(at: 0).location, 3)
4444
XCTAssertEqual(result.range(at: 1).location, NSNotFound)
4545
XCTAssertEqual(result.range(at: 2).location, 3)
4646
//ZeroOffset
47-
result = match.resultByAdjustingRangesWithOffset(0)
47+
result = match.adjustingRanges(offset: 0)
4848
XCTAssertEqual(result.range(at: 0).location, 5)
4949
XCTAssertEqual(result.range(at: 1).location, NSNotFound)
5050
XCTAssertEqual(result.range(at: 2).location, 5)

0 commit comments

Comments
 (0)