Skip to content

Commit 23bac55

Browse files
author
Max Moiseev
committed
[migration] migrating to Swift 3
1 parent a1d15a1 commit 23bac55

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

77 files changed

+944
-946
lines changed

Foundation/FoundationErrors.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
88
//
99

10-
public struct NSCocoaError : RawRepresentable, ErrorType, __BridgedNSError {
10+
public struct NSCocoaError : RawRepresentable, ErrorProtocol, __BridgedNSError {
1111
public let rawValue: Int
1212
public init(rawValue: Int) {
1313
self.rawValue = rawValue

Foundation/NSArray.swift

+35-35
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ extension Array : _ObjectTypeBridgeable {
1616
})
1717
}
1818

19-
public static func _forceBridgeFromObject(x: NSArray, inout result: Array?) {
19+
public static func _forceBridgeFromObject(x: NSArray, result: inout Array?) {
2020
var array = [Element]()
2121
for value in x.allObjects {
2222
if let v = value as? Element {
@@ -28,7 +28,7 @@ extension Array : _ObjectTypeBridgeable {
2828
result = array
2929
}
3030

31-
public static func _conditionallyBridgeFromObject(x: NSArray, inout result: Array?) -> Bool {
31+
public static func _conditionallyBridgeFromObject(x: NSArray, result: inout Array?) -> Bool {
3232
_forceBridgeFromObject(x, result: &result)
3333
return true
3434
}
@@ -74,13 +74,13 @@ public class NSArray : NSObject, NSCopying, NSMutableCopying, NSSecureCoding, NS
7474
withUnsafeMutablePointer(&cnt) { (ptr: UnsafeMutablePointer<UInt32>) -> Void in
7575
aDecoder.decodeValueOfObjCType("i", at: UnsafeMutablePointer<Void>(ptr))
7676
}
77-
let objects = UnsafeMutablePointer<AnyObject?>.alloc(Int(cnt))
77+
let objects = UnsafeMutablePointer<AnyObject?>(allocatingCapacity: Int(cnt))
7878
for idx in 0..<cnt {
79-
objects.advancedBy(Int(idx)).initialize(aDecoder.decodeObject())
79+
objects.advanced(by: Int(idx)).initialize(with: aDecoder.decodeObject())
8080
}
8181
self.init(objects: UnsafePointer<AnyObject?>(objects), count: Int(cnt))
82-
objects.destroy(Int(cnt))
83-
objects.dealloc(Int(cnt))
82+
objects.deinitialize(count: Int(cnt))
83+
objects.deallocateCapacity(Int(cnt))
8484
} else if aDecoder.dynamicType == NSKeyedUnarchiver.self || aDecoder.containsValueForKey("NS.objects") {
8585
let objects = aDecoder._decodeArrayOfObjectsForKey("NS.objects")
8686
self.init(array: objects)
@@ -160,11 +160,11 @@ public class NSArray : NSObject, NSCopying, NSMutableCopying, NSSecureCoding, NS
160160
// self.init(objects: ptr.baseAddress, count: array.count)
161161
// }
162162
let cnt = array.count
163-
let buffer = UnsafeMutablePointer<AnyObject?>.alloc(cnt)
163+
let buffer = UnsafeMutablePointer<AnyObject?>(allocatingCapacity: cnt)
164164
buffer.initializeFrom(optionalArray)
165165
self.init(objects: buffer, count: cnt)
166-
buffer.destroy(cnt)
167-
buffer.dealloc(cnt)
166+
buffer.deinitialize(count: cnt)
167+
buffer.deallocateCapacity(cnt)
168168
}
169169

170170
public override func isEqual(object: AnyObject?) -> Bool {
@@ -199,7 +199,7 @@ public class NSArray : NSObject, NSCopying, NSMutableCopying, NSSecureCoding, NS
199199

200200
public func componentsJoinedByString(separator: String) -> String {
201201
// make certain to call NSObject's description rather than asking the string interpolator for the swift description
202-
return bridge().map() { ($0 as! NSObject).description }.joinWithSeparator(separator)
202+
return bridge().map() { ($0 as! NSObject).description }.joined(separator: separator)
203203
}
204204

205205
public func containsObject(anObject: AnyObject) -> Bool {
@@ -233,7 +233,7 @@ public class NSArray : NSObject, NSCopying, NSMutableCopying, NSSecureCoding, NS
233233
/// Alternative pseudo funnel method for fastpath fetches from arrays
234234
/// - Experiment: This is a draft API currently under consideration for official import into Foundation
235235
/// - Note: Since this API is under consideration it may be either removed or revised in the near future
236-
public func getObjects(inout objects: [AnyObject], range: NSRange) {
236+
public func getObjects(objects: inout [AnyObject], range: NSRange) {
237237
objects.reserveCapacity(objects.count + range.length)
238238

239239
if self.dynamicType === NSArray.self || self.dynamicType === NSMutableArray.self {
@@ -319,7 +319,7 @@ public class NSArray : NSObject, NSCopying, NSMutableCopying, NSSecureCoding, NS
319319
}
320320
}
321321

322-
public struct Generator : GeneratorType {
322+
public struct Iterator : IteratorProtocol {
323323
// TODO: Detect mutations
324324
// TODO: Use IndexingGenerator instead?
325325
let array : NSArray
@@ -342,21 +342,21 @@ public class NSArray : NSObject, NSCopying, NSMutableCopying, NSSecureCoding, NS
342342
}
343343
}
344344
public func objectEnumerator() -> NSEnumerator {
345-
return NSGeneratorEnumerator(Generator(self))
345+
return NSGeneratorEnumerator(Iterator(self))
346346
}
347347

348348
public func reverseObjectEnumerator() -> NSEnumerator {
349-
return NSGeneratorEnumerator(Generator(self, reverse: true))
349+
return NSGeneratorEnumerator(Iterator(self, reverse: true))
350350
}
351351

352352
/*@NSCopying*/ public var sortedArrayHint: NSData {
353-
let buffer = UnsafeMutablePointer<Int32>.alloc(count)
353+
let buffer = UnsafeMutablePointer<Int32>(allocatingCapacity: count)
354354
for idx in 0..<count {
355355
let item = objectAtIndex(idx) as! NSObject
356356
let hash = item.hash
357-
buffer.advancedBy(idx).memory = Int32(hash).littleEndian
357+
buffer.advanced(by: idx).pointee = Int32(hash).littleEndian
358358
}
359-
return NSData(bytesNoCopy: unsafeBitCast(buffer, UnsafeMutablePointer<Void>.self), length: count * sizeof(Int), freeWhenDone: true)
359+
return NSData(bytesNoCopy: unsafeBitCast(buffer, to: UnsafeMutablePointer<Void>.self), length: count * sizeof(Int), freeWhenDone: true)
360360
}
361361

362362
public func sortedArrayUsingFunction(comparator: @convention(c) (AnyObject, AnyObject, UnsafeMutablePointer<Void>) -> Int, context: UnsafeMutablePointer<Void>) -> [AnyObject] {
@@ -386,7 +386,7 @@ public class NSArray : NSObject, NSCopying, NSMutableCopying, NSSecureCoding, NS
386386
public func objectsAtIndexes(indexes: NSIndexSet) -> [AnyObject] {
387387
var objs = [AnyObject]()
388388
indexes.enumerateRangesUsingBlock { (range, _) in
389-
objs.appendContentsOf(self.subarrayWithRange(range))
389+
objs.append(contentsOf: self.subarrayWithRange(range))
390390
}
391391
return objs
392392
}
@@ -426,7 +426,7 @@ public class NSArray : NSObject, NSCopying, NSMutableCopying, NSSecureCoding, NS
426426
enumerateObjectsAtIndexes(s, options: opts) { (obj, idx, stop) -> Void in
427427
if predicate(obj, idx, stop) {
428428
result = idx
429-
stop.memory = true
429+
stop.pointee = true
430430
}
431431
}
432432
return result
@@ -460,7 +460,7 @@ public class NSArray : NSObject, NSCopying, NSMutableCopying, NSSecureCoding, NS
460460
}
461461

462462
let swiftRange = range.toRange()!
463-
return allObjects[swiftRange].sort { lhs, rhs in
463+
return allObjects[swiftRange].sorted { lhs, rhs in
464464
return cmptr(lhs, rhs) == .OrderedAscending
465465
}
466466
}
@@ -565,7 +565,7 @@ public class NSArray : NSObject, NSCopying, NSMutableCopying, NSSecureCoding, NS
565565
}
566566

567567
extension NSArray : _CFBridgable, _SwiftBridgable {
568-
internal var _cfObject: CFArray { return unsafeBitCast(self, CFArray.self) }
568+
internal var _cfObject: CFArray { return unsafeBitCast(self, to: CFArray.self) }
569569
internal var _swiftObject: [AnyObject] {
570570
var array: [AnyObject]?
571571
Array._forceBridgeFromObject(self, result: &array)
@@ -574,11 +574,11 @@ extension NSArray : _CFBridgable, _SwiftBridgable {
574574
}
575575

576576
extension NSMutableArray {
577-
internal var _cfMutableObject: CFMutableArray { return unsafeBitCast(self, CFMutableArray.self) }
577+
internal var _cfMutableObject: CFMutableArray { return unsafeBitCast(self, to: CFMutableArray.self) }
578578
}
579579

580580
extension CFArray : _NSBridgable, _SwiftBridgable {
581-
internal var _nsObject: NSArray { return unsafeBitCast(self, NSArray.self) }
581+
internal var _nsObject: NSArray { return unsafeBitCast(self, to: NSArray.self) }
582582
internal var _swiftObject: Array<AnyObject> { return _nsObject._swiftObject }
583583
}
584584

@@ -590,7 +590,7 @@ extension CFArray {
590590
let count = CFArrayGetCount(self)
591591
result.reserveCapacity(count)
592592
for i in 0..<count {
593-
result.append(unsafeBitCast(CFArrayGetValueAtIndex(self, i), T.self))
593+
result.append(unsafeBitCast(CFArrayGetValueAtIndex(self, i), to: T.self))
594594
}
595595
return result
596596
}
@@ -601,7 +601,7 @@ extension Array : _NSBridgable, _CFBridgable {
601601
internal var _cfObject: CFArray { return _nsObject._cfObject }
602602
}
603603

604-
public struct NSBinarySearchingOptions : OptionSetType {
604+
public struct NSBinarySearchingOptions : OptionSet {
605605
public let rawValue : UInt
606606
public init(rawValue: UInt) { self.rawValue = rawValue }
607607

@@ -618,7 +618,7 @@ public class NSMutableArray : NSArray {
618618

619619
public func insertObject(anObject: AnyObject, atIndex index: Int) {
620620
if self.dynamicType === NSMutableArray.self {
621-
_storage.insert(anObject, atIndex: index)
621+
_storage.insert(anObject, at: index)
622622
} else {
623623
NSRequiresConcreteImplementation()
624624
}
@@ -632,7 +632,7 @@ public class NSMutableArray : NSArray {
632632

633633
public func removeObjectAtIndex(index: Int) {
634634
if self.dynamicType === NSMutableArray.self {
635-
_storage.removeAtIndex(index)
635+
_storage.remove(at: index)
636636
} else {
637637
NSRequiresConcreteImplementation()
638638
}
@@ -642,7 +642,7 @@ public class NSMutableArray : NSArray {
642642
if self.dynamicType === NSMutableArray.self {
643643
let min = index
644644
let max = index + 1
645-
_storage.replaceRange(min..<max, with: [anObject])
645+
_storage.replaceSubrange(min..<max, with: [anObject])
646646
} else {
647647
NSRequiresConcreteImplementation()
648648
}
@@ -734,7 +734,7 @@ public class NSMutableArray : NSArray {
734734

735735
public func removeObjectsInArray(otherArray: [AnyObject]) {
736736
let set = NSSet(array : otherArray)
737-
for idx in (0..<count).reverse() {
737+
for idx in (0..<count).reversed() {
738738
if set.containsObject(objectAtIndex(idx)) {
739739
removeObjectAtIndex(idx)
740740
}
@@ -743,9 +743,9 @@ public class NSMutableArray : NSArray {
743743

744744
public func removeObjectsInRange(range: NSRange) {
745745
if self.dynamicType === NSMutableArray.self {
746-
_storage.removeRange(range.toRange()!)
746+
_storage.removeSubrange(range.toRange()!)
747747
} else {
748-
for idx in range.toRange()!.reverse() {
748+
for idx in range.toRange()!.reversed() {
749749
removeObjectAtIndex(idx)
750750
}
751751
}
@@ -763,7 +763,7 @@ public class NSMutableArray : NSArray {
763763
_storage[idx + range.location] = otherArray[idx]
764764
}
765765
for idx in range.length..<otherArray.count {
766-
_storage.insert(otherArray[idx], atIndex: idx + range.location)
766+
_storage.insert(otherArray[idx], at: idx + range.location)
767767
}
768768
} else {
769769
NSUnimplemented()
@@ -823,9 +823,9 @@ public class NSMutableArray : NSArray {
823823
public convenience init?(contentsOfURL url: NSURL) { NSUnimplemented() }
824824
}
825825

826-
extension NSArray : SequenceType {
827-
final public func generate() -> Generator {
828-
return Generator(self)
826+
extension NSArray : Sequence {
827+
final public func makeIterator() -> Iterator {
828+
return Iterator(self)
829829
}
830830
}
831831

Foundation/NSAttributedString.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public class NSAttributedString : NSObject, NSCopying, NSMutableCopying, NSSecur
5858
public func enumerateAttribute(attrName: String, inRange enumerationRange: NSRange, options opts: NSAttributedStringEnumerationOptions, usingBlock block: (AnyObject?, NSRange, UnsafeMutablePointer<ObjCBool>) -> Void) { NSUnimplemented() }
5959
}
6060

61-
public struct NSAttributedStringEnumerationOptions : OptionSetType {
61+
public struct NSAttributedStringEnumerationOptions : OptionSet {
6262
public let rawValue : UInt
6363
public init(rawValue: UInt) { self.rawValue = rawValue }
6464
public static let Reverse = NSAttributedStringEnumerationOptions(rawValue: 1 << 1)

Foundation/NSBundle.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public class NSBundle : NSObject {
3636
}
3737

3838
let url = NSURL(fileURLWithPath: resolvedPath)
39-
_bundle = CFBundleCreate(kCFAllocatorSystemDefault, unsafeBitCast(url, CFURL.self))
39+
_bundle = CFBundleCreate(kCFAllocatorSystemDefault, unsafeBitCast(url, to: CFURL.self))
4040
}
4141

4242
public convenience init?(URL url: NSURL) {

Foundation/NSByteCountFormatter.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
//
99

1010

11-
public struct NSByteCountFormatterUnits : OptionSetType {
11+
public struct NSByteCountFormatterUnits : OptionSet {
1212
public let rawValue : UInt
1313
public init(rawValue: UInt) { self.rawValue = rawValue }
1414

Foundation/NSCFArray.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,11 @@ internal final class _NSCFArray : NSMutableArray {
3030

3131
override func objectAtIndex(index: Int) -> AnyObject {
3232
let value = CFArrayGetValueAtIndex(_cfObject, index)
33-
return unsafeBitCast(value, AnyObject.self)
33+
return unsafeBitCast(value, to: AnyObject.self)
3434
}
3535

3636
override func insertObject(anObject: AnyObject, atIndex index: Int) {
37-
CFArrayInsertValueAtIndex(_cfMutableObject, index, unsafeBitCast(anObject, UnsafePointer<Void>.self))
37+
CFArrayInsertValueAtIndex(_cfMutableObject, index, unsafeBitCast(anObject, to: UnsafePointer<Void>.self))
3838
}
3939

4040
override func removeObjectAtIndex(index: Int) {

Foundation/NSCFDictionary.swift

+12-12
Original file line numberDiff line numberDiff line change
@@ -33,20 +33,20 @@ internal final class _NSCFDictionary : NSMutableDictionary {
3333
}
3434

3535
override var count: Int {
36-
return CFDictionaryGetCount(unsafeBitCast(self, CFDictionary.self))
36+
return CFDictionaryGetCount(unsafeBitCast(self, to: CFDictionary.self))
3737
}
3838

3939
override func objectForKey(aKey: AnyObject) -> AnyObject? {
40-
let value = CFDictionaryGetValue(_cfObject, unsafeBitCast(aKey, UnsafePointer<Void>.self))
40+
let value = CFDictionaryGetValue(_cfObject, unsafeBitCast(aKey, to: UnsafePointer<Void>.self))
4141
if value != nil {
42-
return unsafeBitCast(value, AnyObject.self)
42+
return unsafeBitCast(value, to: AnyObject.self)
4343
} else {
4444
return nil
4545
}
4646
}
4747

4848
// This doesn't feel like a particularly efficient generator of CFDictionary keys, but it works for now. We should probably put a function into CF that allows us to simply iterate the keys directly from the underlying CF storage.
49-
private struct _NSCFKeyGenerator : GeneratorType {
49+
private struct _NSCFKeyGenerator : IteratorProtocol {
5050
var keyArray : [NSObject] = []
5151
var index : Int = 0
5252
let count : Int
@@ -64,15 +64,15 @@ internal final class _NSCFDictionary : NSMutableDictionary {
6464
let cf = dict._cfObject
6565
count = CFDictionaryGetCount(cf)
6666

67-
let keys = UnsafeMutablePointer<UnsafePointer<Void>>.alloc(count)
67+
let keys = UnsafeMutablePointer<UnsafePointer<Void>>(allocatingCapacity: count)
6868
CFDictionaryGetKeysAndValues(cf, keys, nil)
6969

7070
for idx in 0..<count {
71-
let key = unsafeBitCast(keys.advancedBy(idx).memory, NSObject.self)
71+
let key = unsafeBitCast(keys.advanced(by: idx).pointee, to: NSObject.self)
7272
keyArray.append(key)
7373
}
74-
keys.destroy()
75-
keys.dealloc(count)
74+
keys.deinitialize()
75+
keys.deallocateCapacity(count)
7676
}
7777
}
7878

@@ -81,11 +81,11 @@ internal final class _NSCFDictionary : NSMutableDictionary {
8181
}
8282

8383
override func removeObjectForKey(aKey: AnyObject) {
84-
CFDictionaryRemoveValue(_cfMutableObject, unsafeBitCast(aKey, UnsafePointer<Void>.self))
84+
CFDictionaryRemoveValue(_cfMutableObject, unsafeBitCast(aKey, to: UnsafePointer<Void>.self))
8585
}
8686

8787
override func setObject(anObject: AnyObject, forKey aKey: NSObject) {
88-
CFDictionarySetValue(_cfMutableObject, unsafeBitCast(aKey, UnsafePointer<Void>.self), unsafeBitCast(anObject, UnsafePointer<Void>.self))
88+
CFDictionarySetValue(_cfMutableObject, unsafeBitCast(aKey, to: UnsafePointer<Void>.self), unsafeBitCast(anObject, to: UnsafePointer<Void>.self))
8989
}
9090

9191
override var classForCoder: AnyClass {
@@ -120,10 +120,10 @@ internal func _CFSwiftDictionaryGetValue(dictionary: AnyObject, key: AnyObject)
120120

121121
internal func _CFSwiftDictionaryGetValueIfPresent(dictionary: AnyObject, key: AnyObject, value: UnsafeMutablePointer<Unmanaged<AnyObject>?>) -> Bool {
122122
if let val = _CFSwiftDictionaryGetValue(dictionary, key: key) {
123-
value.memory = val
123+
value.pointee = val
124124
return true
125125
} else {
126-
value.memory = nil
126+
value.pointee = nil
127127
return false
128128
}
129129
}

0 commit comments

Comments
 (0)