Skip to content

Migrating Foundation to Swift 3 #271

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 8, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Foundation/FoundationErrors.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//

public struct NSCocoaError : RawRepresentable, ErrorType, __BridgedNSError {
public struct NSCocoaError : RawRepresentable, ErrorProtocol, __BridgedNSError {
public let rawValue: Int
public init(rawValue: Int) {
self.rawValue = rawValue
Expand Down
70 changes: 35 additions & 35 deletions Foundation/NSArray.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ extension Array : _ObjectTypeBridgeable {
})
}

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

public static func _conditionallyBridgeFromObject(x: NSArray, inout result: Array?) -> Bool {
public static func _conditionallyBridgeFromObject(x: NSArray, result: inout Array?) -> Bool {
_forceBridgeFromObject(x, result: &result)
return true
}
Expand Down Expand Up @@ -74,13 +74,13 @@ public class NSArray : NSObject, NSCopying, NSMutableCopying, NSSecureCoding, NS
withUnsafeMutablePointer(&cnt) { (ptr: UnsafeMutablePointer<UInt32>) -> Void in
aDecoder.decodeValueOfObjCType("i", at: UnsafeMutablePointer<Void>(ptr))
}
let objects = UnsafeMutablePointer<AnyObject?>.alloc(Int(cnt))
let objects = UnsafeMutablePointer<AnyObject?>(allocatingCapacity: Int(cnt))
for idx in 0..<cnt {
objects.advancedBy(Int(idx)).initialize(aDecoder.decodeObject())
objects.advanced(by: Int(idx)).initialize(with: aDecoder.decodeObject())
}
self.init(objects: UnsafePointer<AnyObject?>(objects), count: Int(cnt))
objects.destroy(Int(cnt))
objects.dealloc(Int(cnt))
objects.deinitialize(count: Int(cnt))
objects.deallocateCapacity(Int(cnt))
} else if aDecoder.dynamicType == NSKeyedUnarchiver.self || aDecoder.containsValueForKey("NS.objects") {
let objects = aDecoder._decodeArrayOfObjectsForKey("NS.objects")
self.init(array: objects)
Expand Down Expand Up @@ -160,11 +160,11 @@ public class NSArray : NSObject, NSCopying, NSMutableCopying, NSSecureCoding, NS
// self.init(objects: ptr.baseAddress, count: array.count)
// }
let cnt = array.count
let buffer = UnsafeMutablePointer<AnyObject?>.alloc(cnt)
let buffer = UnsafeMutablePointer<AnyObject?>(allocatingCapacity: cnt)
buffer.initializeFrom(optionalArray)
self.init(objects: buffer, count: cnt)
buffer.destroy(cnt)
buffer.dealloc(cnt)
buffer.deinitialize(count: cnt)
buffer.deallocateCapacity(cnt)
}

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

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

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

if self.dynamicType === NSArray.self || self.dynamicType === NSMutableArray.self {
Expand Down Expand Up @@ -319,7 +319,7 @@ public class NSArray : NSObject, NSCopying, NSMutableCopying, NSSecureCoding, NS
}
}

public struct Generator : GeneratorType {
public struct Iterator : IteratorProtocol {
// TODO: Detect mutations
// TODO: Use IndexingGenerator instead?
let array : NSArray
Expand All @@ -342,21 +342,21 @@ public class NSArray : NSObject, NSCopying, NSMutableCopying, NSSecureCoding, NS
}
}
public func objectEnumerator() -> NSEnumerator {
return NSGeneratorEnumerator(Generator(self))
return NSGeneratorEnumerator(Iterator(self))
}

public func reverseObjectEnumerator() -> NSEnumerator {
return NSGeneratorEnumerator(Generator(self, reverse: true))
return NSGeneratorEnumerator(Iterator(self, reverse: true))
}

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

public func sortedArrayUsingFunction(comparator: @convention(c) (AnyObject, AnyObject, UnsafeMutablePointer<Void>) -> Int, context: UnsafeMutablePointer<Void>) -> [AnyObject] {
Expand Down Expand Up @@ -386,7 +386,7 @@ public class NSArray : NSObject, NSCopying, NSMutableCopying, NSSecureCoding, NS
public func objectsAtIndexes(indexes: NSIndexSet) -> [AnyObject] {
var objs = [AnyObject]()
indexes.enumerateRangesUsingBlock { (range, _) in
objs.appendContentsOf(self.subarrayWithRange(range))
objs.append(contentsOf: self.subarrayWithRange(range))
}
return objs
}
Expand Down Expand Up @@ -426,7 +426,7 @@ public class NSArray : NSObject, NSCopying, NSMutableCopying, NSSecureCoding, NS
enumerateObjectsAtIndexes(s, options: opts) { (obj, idx, stop) -> Void in
if predicate(obj, idx, stop) {
result = idx
stop.memory = true
stop.pointee = true
}
}
return result
Expand Down Expand Up @@ -460,7 +460,7 @@ public class NSArray : NSObject, NSCopying, NSMutableCopying, NSSecureCoding, NS
}

let swiftRange = range.toRange()!
return allObjects[swiftRange].sort { lhs, rhs in
return allObjects[swiftRange].sorted { lhs, rhs in
return cmptr(lhs, rhs) == .OrderedAscending
}
}
Expand Down Expand Up @@ -565,7 +565,7 @@ public class NSArray : NSObject, NSCopying, NSMutableCopying, NSSecureCoding, NS
}

extension NSArray : _CFBridgable, _SwiftBridgable {
internal var _cfObject: CFArray { return unsafeBitCast(self, CFArray.self) }
internal var _cfObject: CFArray { return unsafeBitCast(self, to: CFArray.self) }
internal var _swiftObject: [AnyObject] {
var array: [AnyObject]?
Array._forceBridgeFromObject(self, result: &array)
Expand All @@ -574,11 +574,11 @@ extension NSArray : _CFBridgable, _SwiftBridgable {
}

extension NSMutableArray {
internal var _cfMutableObject: CFMutableArray { return unsafeBitCast(self, CFMutableArray.self) }
internal var _cfMutableObject: CFMutableArray { return unsafeBitCast(self, to: CFMutableArray.self) }
}

extension CFArray : _NSBridgable, _SwiftBridgable {
internal var _nsObject: NSArray { return unsafeBitCast(self, NSArray.self) }
internal var _nsObject: NSArray { return unsafeBitCast(self, to: NSArray.self) }
internal var _swiftObject: Array<AnyObject> { return _nsObject._swiftObject }
}

Expand All @@ -590,7 +590,7 @@ extension CFArray {
let count = CFArrayGetCount(self)
result.reserveCapacity(count)
for i in 0..<count {
result.append(unsafeBitCast(CFArrayGetValueAtIndex(self, i), T.self))
result.append(unsafeBitCast(CFArrayGetValueAtIndex(self, i), to: T.self))
}
return result
}
Expand All @@ -601,7 +601,7 @@ extension Array : _NSBridgable, _CFBridgable {
internal var _cfObject: CFArray { return _nsObject._cfObject }
}

public struct NSBinarySearchingOptions : OptionSetType {
public struct NSBinarySearchingOptions : OptionSet {
public let rawValue : UInt
public init(rawValue: UInt) { self.rawValue = rawValue }

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

public func insertObject(anObject: AnyObject, atIndex index: Int) {
if self.dynamicType === NSMutableArray.self {
_storage.insert(anObject, atIndex: index)
_storage.insert(anObject, at: index)
} else {
NSRequiresConcreteImplementation()
}
Expand All @@ -632,7 +632,7 @@ public class NSMutableArray : NSArray {

public func removeObjectAtIndex(index: Int) {
if self.dynamicType === NSMutableArray.self {
_storage.removeAtIndex(index)
_storage.remove(at: index)
} else {
NSRequiresConcreteImplementation()
}
Expand All @@ -642,7 +642,7 @@ public class NSMutableArray : NSArray {
if self.dynamicType === NSMutableArray.self {
let min = index
let max = index + 1
_storage.replaceRange(min..<max, with: [anObject])
_storage.replaceSubrange(min..<max, with: [anObject])
} else {
NSRequiresConcreteImplementation()
}
Expand Down Expand Up @@ -734,7 +734,7 @@ public class NSMutableArray : NSArray {

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

public func removeObjectsInRange(range: NSRange) {
if self.dynamicType === NSMutableArray.self {
_storage.removeRange(range.toRange()!)
_storage.removeSubrange(range.toRange()!)
} else {
for idx in range.toRange()!.reverse() {
for idx in range.toRange()!.reversed() {
removeObjectAtIndex(idx)
}
}
Expand All @@ -763,7 +763,7 @@ public class NSMutableArray : NSArray {
_storage[idx + range.location] = otherArray[idx]
}
for idx in range.length..<otherArray.count {
_storage.insert(otherArray[idx], atIndex: idx + range.location)
_storage.insert(otherArray[idx], at: idx + range.location)
}
} else {
NSUnimplemented()
Expand Down Expand Up @@ -823,9 +823,9 @@ public class NSMutableArray : NSArray {
public convenience init?(contentsOfURL url: NSURL) { NSUnimplemented() }
}

extension NSArray : SequenceType {
final public func generate() -> Generator {
return Generator(self)
extension NSArray : Sequence {
final public func makeIterator() -> Iterator {
return Iterator(self)
}
}

Expand Down
2 changes: 1 addition & 1 deletion Foundation/NSAttributedString.swift
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public class NSAttributedString : NSObject, NSCopying, NSMutableCopying, NSSecur
public func enumerateAttribute(attrName: String, inRange enumerationRange: NSRange, options opts: NSAttributedStringEnumerationOptions, usingBlock block: (AnyObject?, NSRange, UnsafeMutablePointer<ObjCBool>) -> Void) { NSUnimplemented() }
}

public struct NSAttributedStringEnumerationOptions : OptionSetType {
public struct NSAttributedStringEnumerationOptions : OptionSet {
public let rawValue : UInt
public init(rawValue: UInt) { self.rawValue = rawValue }
public static let Reverse = NSAttributedStringEnumerationOptions(rawValue: 1 << 1)
Expand Down
2 changes: 1 addition & 1 deletion Foundation/NSBundle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public class NSBundle : NSObject {
}

let url = NSURL(fileURLWithPath: resolvedPath)
_bundle = CFBundleCreate(kCFAllocatorSystemDefault, unsafeBitCast(url, CFURL.self))
_bundle = CFBundleCreate(kCFAllocatorSystemDefault, unsafeBitCast(url, to: CFURL.self))
}

public convenience init?(URL url: NSURL) {
Expand Down
2 changes: 1 addition & 1 deletion Foundation/NSByteCountFormatter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
//


public struct NSByteCountFormatterUnits : OptionSetType {
public struct NSByteCountFormatterUnits : OptionSet {
public let rawValue : UInt
public init(rawValue: UInt) { self.rawValue = rawValue }

Expand Down
4 changes: 2 additions & 2 deletions Foundation/NSCFArray.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ internal final class _NSCFArray : NSMutableArray {

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

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

override func removeObjectAtIndex(index: Int) {
Expand Down
24 changes: 12 additions & 12 deletions Foundation/NSCFDictionary.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,20 @@ internal final class _NSCFDictionary : NSMutableDictionary {
}

override var count: Int {
return CFDictionaryGetCount(unsafeBitCast(self, CFDictionary.self))
return CFDictionaryGetCount(unsafeBitCast(self, to: CFDictionary.self))
}

override func objectForKey(aKey: AnyObject) -> AnyObject? {
let value = CFDictionaryGetValue(_cfObject, unsafeBitCast(aKey, UnsafePointer<Void>.self))
let value = CFDictionaryGetValue(_cfObject, unsafeBitCast(aKey, to: UnsafePointer<Void>.self))
if value != nil {
return unsafeBitCast(value, AnyObject.self)
return unsafeBitCast(value, to: AnyObject.self)
} else {
return nil
}
}

// 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.
private struct _NSCFKeyGenerator : GeneratorType {
private struct _NSCFKeyGenerator : IteratorProtocol {
var keyArray : [NSObject] = []
var index : Int = 0
let count : Int
Expand All @@ -64,15 +64,15 @@ internal final class _NSCFDictionary : NSMutableDictionary {
let cf = dict._cfObject
count = CFDictionaryGetCount(cf)

let keys = UnsafeMutablePointer<UnsafePointer<Void>>.alloc(count)
let keys = UnsafeMutablePointer<UnsafePointer<Void>>(allocatingCapacity: count)
CFDictionaryGetKeysAndValues(cf, keys, nil)

for idx in 0..<count {
let key = unsafeBitCast(keys.advancedBy(idx).memory, NSObject.self)
let key = unsafeBitCast(keys.advanced(by: idx).pointee, to: NSObject.self)
keyArray.append(key)
}
keys.destroy()
keys.dealloc(count)
keys.deinitialize()
keys.deallocateCapacity(count)
}
}

Expand All @@ -81,11 +81,11 @@ internal final class _NSCFDictionary : NSMutableDictionary {
}

override func removeObjectForKey(aKey: AnyObject) {
CFDictionaryRemoveValue(_cfMutableObject, unsafeBitCast(aKey, UnsafePointer<Void>.self))
CFDictionaryRemoveValue(_cfMutableObject, unsafeBitCast(aKey, to: UnsafePointer<Void>.self))
}

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

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

internal func _CFSwiftDictionaryGetValueIfPresent(dictionary: AnyObject, key: AnyObject, value: UnsafeMutablePointer<Unmanaged<AnyObject>?>) -> Bool {
if let val = _CFSwiftDictionaryGetValue(dictionary, key: key) {
value.memory = val
value.pointee = val
return true
} else {
value.memory = nil
value.pointee = nil
return false
}
}
Expand Down
Loading