@@ -15,47 +15,50 @@ import Darwin
15
15
import Glibc
16
16
#endif
17
17
18
- public struct NSDataReadingOptions : OptionSet {
19
- public let rawValue : UInt
20
- public init ( rawValue: UInt ) { self . rawValue = rawValue }
21
-
22
- public static let dataReadingMappedIfSafe = NSDataReadingOptions ( rawValue: UInt ( 1 << 0 ) )
23
- public static let dataReadingUncached = NSDataReadingOptions ( rawValue: UInt ( 1 << 1 ) )
24
- public static let dataReadingMappedAlways = NSDataReadingOptions ( rawValue: UInt ( 1 << 2 ) )
25
- }
18
+ extension NSData {
26
19
27
- public struct NSDataWritingOptions : OptionSet {
28
- public let rawValue : UInt
29
- public init ( rawValue: UInt ) { self . rawValue = rawValue }
30
-
31
- public static let dataWritingAtomic = NSDataWritingOptions ( rawValue: UInt ( 1 << 0 ) )
32
- public static let dataWritingWithoutOverwriting = NSDataWritingOptions ( rawValue: UInt ( 1 << 1 ) )
33
- }
20
+ public struct ReadingOptions : OptionSet {
21
+ public let rawValue : UInt
22
+ public init ( rawValue: UInt ) { self . rawValue = rawValue }
23
+
24
+ public static let dataReadingMappedIfSafe = ReadingOptions ( rawValue: UInt ( 1 << 0 ) )
25
+ public static let dataReadingUncached = ReadingOptions ( rawValue: UInt ( 1 << 1 ) )
26
+ public static let dataReadingMappedAlways = ReadingOptions ( rawValue: UInt ( 1 << 2 ) )
27
+ }
34
28
35
- public struct NSDataSearchOptions : OptionSet {
36
- public let rawValue : UInt
37
- public init ( rawValue: UInt ) { self . rawValue = rawValue }
38
-
39
- public static let backwards = NSDataSearchOptions ( rawValue: UInt ( 1 << 0 ) )
40
- public static let anchored = NSDataSearchOptions ( rawValue: UInt ( 1 << 1 ) )
41
- }
29
+ public struct WritingOptions : OptionSet {
30
+ public let rawValue : UInt
31
+ public init ( rawValue: UInt ) { self . rawValue = rawValue }
32
+
33
+ public static let dataWritingAtomic = WritingOptions ( rawValue: UInt ( 1 << 0 ) )
34
+ public static let dataWritingWithoutOverwriting = WritingOptions ( rawValue: UInt ( 1 << 1 ) )
35
+ }
42
36
43
- public struct NSDataBase64EncodingOptions : OptionSet {
44
- public let rawValue : UInt
45
- public init ( rawValue: UInt ) { self . rawValue = rawValue }
46
-
47
- public static let encoding64CharacterLineLength = NSDataBase64EncodingOptions ( rawValue: UInt ( 1 << 0 ) )
48
- public static let encoding76CharacterLineLength = NSDataBase64EncodingOptions ( rawValue: UInt ( 1 << 1 ) )
49
- public static let encodingEndLineWithCarriageReturn = NSDataBase64EncodingOptions ( rawValue: UInt ( 1 << 4 ) )
50
- public static let encodingEndLineWithLineFeed = NSDataBase64EncodingOptions ( rawValue: UInt ( 1 << 5 ) )
51
- }
37
+ public struct SearchOptions : OptionSet {
38
+ public let rawValue : UInt
39
+ public init ( rawValue: UInt ) { self . rawValue = rawValue }
40
+
41
+ public static let backwards = SearchOptions ( rawValue: UInt ( 1 << 0 ) )
42
+ public static let anchored = SearchOptions ( rawValue: UInt ( 1 << 1 ) )
43
+ }
52
44
53
- public struct NSDataBase64DecodingOptions : OptionSet {
54
- public let rawValue : UInt
55
- public init ( rawValue: UInt ) { self . rawValue = rawValue }
56
-
57
- public static let ignoreUnknownCharacters = NSDataBase64DecodingOptions ( rawValue: UInt ( 1 << 0 ) )
58
- public static let anchored = NSDataSearchOptions ( rawValue: UInt ( 1 << 1 ) )
45
+ public struct Base64EncodingOptions : OptionSet {
46
+ public let rawValue : UInt
47
+ public init ( rawValue: UInt ) { self . rawValue = rawValue }
48
+
49
+ public static let encoding64CharacterLineLength = Base64EncodingOptions ( rawValue: UInt ( 1 << 0 ) )
50
+ public static let encoding76CharacterLineLength = Base64EncodingOptions ( rawValue: UInt ( 1 << 1 ) )
51
+ public static let encodingEndLineWithCarriageReturn = Base64EncodingOptions ( rawValue: UInt ( 1 << 4 ) )
52
+ public static let encodingEndLineWithLineFeed = Base64EncodingOptions ( rawValue: UInt ( 1 << 5 ) )
53
+ }
54
+
55
+ public struct Base64DecodingOptions : OptionSet {
56
+ public let rawValue : UInt
57
+ public init ( rawValue: UInt ) { self . rawValue = rawValue }
58
+
59
+ public static let ignoreUnknownCharacters = Base64DecodingOptions ( rawValue: UInt ( 1 << 0 ) )
60
+ public static let anchored = Base64DecodingOptions ( rawValue: UInt ( 1 << 1 ) )
61
+ }
59
62
}
60
63
61
64
private final class _NSDataDeallocator {
@@ -252,7 +255,7 @@ extension NSData {
252
255
var deallocator : ( ( buffer: UnsafeMutablePointer < Void > , length: Int ) -> Void ) ?
253
256
}
254
257
255
- internal static func readBytesFromFileWithExtendedAttributes( _ path: String , options: NSDataReadingOptions ) throws -> NSDataReadResult {
258
+ internal static func readBytesFromFileWithExtendedAttributes( _ path: String , options: ReadingOptions ) throws -> NSDataReadResult {
256
259
let fd = _CFOpenFile ( path, O_RDONLY)
257
260
if fd < 0 {
258
261
throw NSError ( domain: NSPOSIXErrorDomain, code: Int ( errno) , userInfo: nil )
@@ -308,7 +311,7 @@ extension NSData {
308
311
}
309
312
}
310
313
311
- public convenience init ( contentsOfFile path: String , options readOptionsMask: NSDataReadingOptions ) throws {
314
+ public convenience init ( contentsOfFile path: String , options readOptionsMask: ReadingOptions ) throws {
312
315
let readResult = try NSData . readBytesFromFileWithExtendedAttributes ( path, options: readOptionsMask)
313
316
self . init ( bytes: readResult. bytes, length: readResult. length, copy: false , deallocator: readResult. deallocator)
314
317
}
@@ -326,15 +329,15 @@ extension NSData {
326
329
self . init ( bytes: data. bytes, length: data. length)
327
330
}
328
331
329
- public convenience init ( contentsOfURL url: URL , options readOptionsMask: NSDataReadingOptions ) throws {
332
+ public convenience init ( contentsOfURL url: URL , options readOptionsMask: ReadingOptions ) throws {
330
333
if url. isFileURL {
331
334
try self . init ( contentsOfFile: url. path!, options: readOptionsMask)
332
335
} else {
333
336
let session = URLSession ( configuration: URLSessionConfiguration . defaultSessionConfiguration ( ) )
334
337
let cond = NSCondition ( )
335
338
var resError : NSError ?
336
339
var resData : NSData ?
337
- let task = session. dataTaskWithURL ( url, completionHandler: { ( data: NSData ? , response: URLResponse ? , error: NSError ? ) -> Void in
340
+ let task = session. dataTaskWithURL ( url, completionHandler: { ( data: Data ? , response: URLResponse ? , error: NSError ? ) -> Void in
338
341
resData = data
339
342
resError = error
340
343
cond. broadcast ( )
@@ -383,14 +386,15 @@ extension NSData {
383
386
384
387
return memcmp ( bytes1, bytes2, length) == 0
385
388
}
386
- public func subdata( with range: NSRange ) -> NSData {
389
+
390
+ public func subdata( with range: NSRange ) -> Data {
387
391
if range. length == 0 {
388
- return NSData ( )
392
+ return Data ( )
389
393
}
390
394
if range. location == 0 && range. length == self . length {
391
- return copy ( with : nil ) as! NSData
395
+ return Data ( _bridged : self )
392
396
}
393
- return NSData ( bytes: bytes. advanced ( by: range. location) , length : range. length)
397
+ return Data ( bytes: bytes. advanced ( by: range. location) , count : range. length)
394
398
}
395
399
396
400
internal func makeTemporaryFileInDirectory( _ dirPath: String ) throws -> ( Int32 , String ) {
@@ -425,7 +429,7 @@ extension NSData {
425
429
}
426
430
}
427
431
428
- public func write( toFile path: String , options writeOptionsMask: NSDataWritingOptions = [ ] ) throws {
432
+ public func write( toFile path: String , options writeOptionsMask: WritingOptions = [ ] ) throws {
429
433
var fd : Int32
430
434
var mode : mode_t ? = nil
431
435
let useAuxiliaryFile = writeOptionsMask. contains ( . dataWritingAtomic)
@@ -512,7 +516,7 @@ extension NSData {
512
516
/// - throws: This method returns Void and is marked with the `throws` keyword to indicate that it throws an error in the event of failure.
513
517
///
514
518
/// This method is invoked in a `try` expression and the caller is responsible for handling any errors in the `catch` clauses of a `do` statement, as described in [Error Handling](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/ErrorHandling.html#//apple_ref/doc/uid/TP40014097-CH42) in [The Swift Programming Language](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/index.html#//apple_ref/doc/uid/TP40014097) and [Error Handling](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/BuildingCocoaApps/AdoptingCocoaDesignPatterns.html#//apple_ref/doc/uid/TP40014216-CH7-ID10) in [Using Swift with Cocoa and Objective-C](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/BuildingCocoaApps/index.html#//apple_ref/doc/uid/TP40014216).
515
- public func write( to url: URL , options writeOptionsMask: NSDataWritingOptions = [ ] ) throws {
519
+ public func write( to url: URL , options writeOptionsMask: WritingOptions = [ ] ) throws {
516
520
guard let path = url. path where url. isFileURL == true else {
517
521
let userInfo = [ NSLocalizedDescriptionKey : " The folder at “ \( url) ” does not exist or is not a file URL. " , // NSLocalizedString() not yet available
518
522
NSURLErrorKey : url. absoluteString ?? " " ] as Dictionary < String , Any >
@@ -521,7 +525,7 @@ extension NSData {
521
525
try write ( toFile: path, options: writeOptionsMask)
522
526
}
523
527
524
- public func range( of dataToFind: NSData , options mask: NSDataSearchOptions = [ ] , in searchRange: NSRange ) -> NSRange {
528
+ public func range( of dataToFind: NSData , options mask: SearchOptions = [ ] , in searchRange: NSRange ) -> NSRange {
525
529
guard dataToFind. length > 0 else { return NSRange ( location: NSNotFound, length: 0 ) }
526
530
guard let searchRange = searchRange. toRange ( ) else { fatalError ( " invalid range " ) }
527
531
@@ -549,7 +553,7 @@ extension NSData {
549
553
return nil
550
554
}
551
555
552
- internal func enumerateByteRangesUsingBlockRethrows( _ block: ( UnsafePointer < Void > , NSRange , UnsafeMutablePointer < Bool > ) throws -> Void ) throws {
556
+ internal func enumerateByteRangesUsingBlockRethrows( _ block: @ noescape ( UnsafePointer < Void > , NSRange , UnsafeMutablePointer < Bool > ) throws -> Void ) throws {
553
557
var err : ErrorProtocol ? = nil
554
558
self . enumerateBytes ( ) { ( buf, range, stop) -> Void in
555
559
do {
@@ -563,19 +567,31 @@ extension NSData {
563
567
}
564
568
}
565
569
566
- public func enumerateBytes( _ block: ( UnsafePointer < Void > , NSRange , UnsafeMutablePointer < Bool > ) -> Void ) {
570
+ public func enumerateBytes( _ block: @ noescape ( UnsafePointer < Void > , NSRange , UnsafeMutablePointer < Bool > ) -> Void ) {
567
571
var stop = false
568
572
withUnsafeMutablePointer ( & stop) { stopPointer in
569
573
block ( bytes, NSMakeRange ( 0 , length) , stopPointer)
570
574
}
571
575
}
572
576
}
573
577
574
- extension NSData : _CFBridgable { }
578
+ extension NSData : _CFBridgable , _SwiftBridgable {
579
+ typealias SwiftType = Data
580
+ internal var _swiftObject : SwiftType { return Data ( _bridged: self ) }
581
+ }
582
+
583
+ extension Data : _NSBridgable , _CFBridgable {
584
+ typealias CFType = CFData
585
+ typealias NSType = NSData
586
+ internal var _cfObject : CFType { return _nsObject. _cfObject }
587
+ internal var _nsObject : NSType { return _bridgeToObjectiveC ( ) }
588
+ }
575
589
576
- extension CFData : _NSBridgable {
590
+ extension CFData : _NSBridgable , _SwiftBridgable {
577
591
typealias NSType = NSData
592
+ typealias SwiftType = Data
578
593
internal var _nsObject : NSType { return unsafeBitCast ( self , to: NSType . self) }
594
+ internal var _swiftObject : SwiftType { return Data ( _bridged: self . _nsObject) }
579
595
}
580
596
581
597
extension NSMutableData {
@@ -614,7 +630,7 @@ extension NSData {
614
630
615
631
/* Create an NSData from a Base-64 encoded NSString using the given options. By default, returns nil when the input is not recognized as valid Base-64.
616
632
*/
617
- public convenience init ? ( base64Encoded base64String: String , options: NSDataBase64DecodingOptions ) {
633
+ public convenience init ? ( base64Encoded base64String: String , options: Base64DecodingOptions ) {
618
634
let encodedBytes = Array ( base64String. utf8)
619
635
guard let decodedBytes = NSData . base64DecodeBytes ( encodedBytes, options: options) else {
620
636
return nil
@@ -624,7 +640,7 @@ extension NSData {
624
640
625
641
/* Create a Base-64 encoded NSString from the receiver's contents using the given options.
626
642
*/
627
- public func base64EncodedString( _ options: NSDataBase64EncodingOptions = [ ] ) -> String {
643
+ public func base64EncodedString( _ options: Base64EncodingOptions = [ ] ) -> String {
628
644
var decodedBytes = [ UInt8] ( repeating: 0 , count: self . length)
629
645
getBytes ( & decodedBytes, length: decodedBytes. count)
630
646
let encodedBytes = NSData . base64EncodeBytes ( decodedBytes, options: options)
@@ -634,9 +650,9 @@ extension NSData {
634
650
635
651
/* Create an NSData from a Base-64, UTF-8 encoded NSData. By default, returns nil when the input is not recognized as valid Base-64.
636
652
*/
637
- public convenience init ? ( base64Encoded base64Data: NSData , options: NSDataBase64DecodingOptions ) {
638
- var encodedBytes = [ UInt8] ( repeating: 0 , count: base64Data. length )
639
- base64Data. getBytes ( & encodedBytes, length: encodedBytes. count)
653
+ public convenience init ? ( base64Encoded base64Data: Data , options: Base64DecodingOptions ) {
654
+ var encodedBytes = [ UInt8] ( repeating: 0 , count: base64Data. count )
655
+ base64Data. _nsObject . getBytes ( & encodedBytes, length: encodedBytes. count)
640
656
guard let decodedBytes = NSData . base64DecodeBytes ( encodedBytes, options: options) else {
641
657
return nil
642
658
}
@@ -645,11 +661,11 @@ extension NSData {
645
661
646
662
/* Create a Base-64, UTF-8 encoded NSData from the receiver's contents using the given options.
647
663
*/
648
- public func base64EncodedData( _ options: NSDataBase64EncodingOptions = [ ] ) -> NSData {
664
+ public func base64EncodedData( _ options: Base64EncodingOptions = [ ] ) -> Data {
649
665
var decodedBytes = [ UInt8] ( repeating: 0 , count: self . length)
650
666
getBytes ( & decodedBytes, length: decodedBytes. count)
651
667
let encodedBytes = NSData . base64EncodeBytes ( decodedBytes, options: options)
652
- return NSData ( bytes: encodedBytes, length : encodedBytes. count)
668
+ return Data ( bytes: encodedBytes, count : encodedBytes. count)
653
669
}
654
670
655
671
/**
@@ -726,7 +742,7 @@ extension NSData {
726
742
- parameter options: Options for handling invalid input
727
743
- returns: The decoded bytes.
728
744
*/
729
- private static func base64DecodeBytes( _ bytes: [ UInt8 ] , options: NSDataBase64DecodingOptions = [ ] ) -> [ UInt8 ] ? {
745
+ private static func base64DecodeBytes( _ bytes: [ UInt8 ] , options: Base64DecodingOptions = [ ] ) -> [ UInt8 ] ? {
730
746
var decodedBytes = [ UInt8] ( )
731
747
decodedBytes. reserveCapacity ( ( bytes. count/ 3 ) * 2 )
732
748
@@ -796,7 +812,7 @@ extension NSData {
796
812
- parameter options: Options for formatting the result
797
813
- returns: The Base64-encoding for those bytes.
798
814
*/
799
- private static func base64EncodeBytes( _ bytes: [ UInt8 ] , options: NSDataBase64EncodingOptions = [ ] ) -> [ UInt8 ] {
815
+ private static func base64EncodeBytes( _ bytes: [ UInt8 ] , options: Base64EncodingOptions = [ ] ) -> [ UInt8 ] {
800
816
var result = [ UInt8] ( )
801
817
result. reserveCapacity ( ( bytes. count/ 3 ) * 4 )
802
818
0 commit comments