Skip to content

Commit bc4bbb1

Browse files
authored
Merge pull request #1248 from spevans/pr_nsmutablearray_renames
2 parents 4264eff + c7bc4ca commit bc4bbb1

File tree

3 files changed

+181
-62
lines changed

3 files changed

+181
-62
lines changed

Foundation/NSArray.swift

Lines changed: 106 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,33 @@ open class NSArray : NSObject, NSCopying, NSMutableCopying, NSSecureCoding, NSCo
2727
return _SwiftValue.fetch(nonOptional: _storage[index])
2828
}
2929

30-
public convenience override init() {
31-
self.init(objects: [], count:0)
30+
public override init() {
31+
_storage.reserveCapacity(0)
3232
}
33-
34-
public required init(objects: UnsafePointer<AnyObject>!, count cnt: Int) {
35-
_storage.reserveCapacity(cnt)
36-
for idx in 0..<cnt {
33+
34+
public required init(objects: UnsafePointer<AnyObject>?, count: Int) {
35+
precondition(count >= 0)
36+
precondition(count == 0 || objects != nil)
37+
38+
_storage.reserveCapacity(count)
39+
for idx in 0..<count {
40+
_storage.append(objects![idx])
41+
}
42+
}
43+
44+
public convenience init(objects: UnsafePointer<AnyObject>, count: Int) {
45+
self.init()
46+
_storage.reserveCapacity(count)
47+
for idx in 0..<count {
3748
_storage.append(objects[idx])
3849
}
3950
}
40-
41-
required public convenience init(arrayLiteral elements: Any...) {
51+
52+
public convenience init(objects elements: AnyObject...) {
53+
self.init(objects: elements, count: elements.count)
54+
}
55+
56+
public required convenience init(arrayLiteral elements: Any...) {
4257
self.init(array: elements)
4358
}
4459

@@ -168,7 +183,7 @@ open class NSArray : NSObject, NSCopying, NSMutableCopying, NSSecureCoding, NSCo
168183
open func addingObjects(from otherArray: [Any]) -> [Any] {
169184
return allObjects + otherArray
170185
}
171-
186+
172187
open func componentsJoined(by separator: String) -> String {
173188
// make certain to call NSObject's description rather than asking the string interpolator for the swift description
174189
return allObjects.map { "\($0)" }.joined(separator: separator)
@@ -188,8 +203,15 @@ open class NSArray : NSObject, NSCopying, NSMutableCopying, NSSecureCoding, NSCo
188203
}
189204
return false
190205
}
191-
192-
open func description(withLocale locale: Locale?) -> String { return description(withLocale: locale, indent: 0) }
206+
207+
override open var description: String {
208+
return description(withLocale: nil)
209+
}
210+
211+
open func description(withLocale locale: Locale?) -> String {
212+
return description(withLocale: locale, indent: 0)
213+
}
214+
193215
open func description(withLocale locale: Locale?, indent level: Int) -> String {
194216
var descriptions = [String]()
195217
let cnt = count
@@ -406,12 +428,19 @@ open class NSArray : NSObject, NSCopying, NSMutableCopying, NSSecureCoding, NSCo
406428
getObjects(&objects, range: range)
407429
return objects
408430
}
409-
431+
432+
open func write(to url: URL) throws {
433+
let pListData = try PropertyListSerialization.data(fromPropertyList: self, format: .xml, options: 0)
434+
try pListData.write(to: url, options: .atomic)
435+
}
436+
437+
@available(*, deprecated)
410438
open func write(toFile path: String, atomically useAuxiliaryFile: Bool) -> Bool {
411439
return write(to: URL(fileURLWithPath: path), atomically: useAuxiliaryFile)
412440
}
413-
441+
414442
// the atomically flag is ignored if url of a type that cannot be written atomically.
443+
@available(*, deprecated)
415444
open func write(to url: URL, atomically: Bool) -> Bool {
416445
do {
417446
let pListData = try PropertyListSerialization.data(fromPropertyList: self, format: .xml, options: 0)
@@ -421,7 +450,7 @@ open class NSArray : NSObject, NSCopying, NSMutableCopying, NSSecureCoding, NSCo
421450
return false
422451
}
423452
}
424-
453+
425454
open func objects(at indexes: IndexSet) -> [Any] {
426455
var objs = [Any]()
427456
indexes.rangeView.forEach {
@@ -430,33 +459,37 @@ open class NSArray : NSObject, NSCopying, NSMutableCopying, NSSecureCoding, NSCo
430459

431460
return objs
432461
}
433-
462+
434463
open subscript (idx: Int) -> Any {
435464
guard idx < count && idx >= 0 else {
436465
fatalError("\(self): Index out of bounds")
437466
}
438467

439468
return object(at: idx)
440469
}
441-
470+
442471
open func enumerateObjects(_ block: (Any, Int, UnsafeMutablePointer<ObjCBool>) -> Swift.Void) {
443472
self.enumerateObjects(options: [], using: block)
444473
}
474+
445475
open func enumerateObjects(options opts: NSEnumerationOptions = [], using block: (Any, Int, UnsafeMutablePointer<ObjCBool>) -> Swift.Void) {
446476
self.enumerateObjects(at: IndexSet(integersIn: 0..<count), options: opts, using: block)
447477
}
478+
448479
open func enumerateObjects(at s: IndexSet, options opts: NSEnumerationOptions = [], using block: (Any, Int, UnsafeMutablePointer<ObjCBool>) -> Swift.Void) {
449480
s._bridgeToObjectiveC().enumerate(options: opts) { (idx, stop) in
450481
block(self.object(at: idx), idx, stop)
451482
}
452483
}
453-
484+
454485
open func indexOfObject(passingTest predicate: (Any, Int, UnsafeMutablePointer<ObjCBool>) -> Bool) -> Int {
455-
return indexOfObject([], passingTest: predicate)
486+
return indexOfObject(options: [], passingTest: predicate)
456487
}
457-
open func indexOfObject(_ opts: NSEnumerationOptions = [], passingTest predicate: (Any, Int, UnsafeMutablePointer<ObjCBool>) -> Bool) -> Int {
488+
489+
open func indexOfObject(options opts: NSEnumerationOptions = [], passingTest predicate: (Any, Int, UnsafeMutablePointer<ObjCBool>) -> Bool) -> Int {
458490
return indexOfObject(at: IndexSet(integersIn: 0..<count), options: opts, passingTest: predicate)
459491
}
492+
460493
open func indexOfObject(at s: IndexSet, options opts: NSEnumerationOptions = [], passingTest predicate: (Any, Int, UnsafeMutablePointer<ObjCBool>) -> Bool) -> Int {
461494
var result = NSNotFound
462495
enumerateObjects(at: s, options: opts) { (obj, idx, stop) -> Void in
@@ -467,13 +500,15 @@ open class NSArray : NSObject, NSCopying, NSMutableCopying, NSSecureCoding, NSCo
467500
}
468501
return result
469502
}
470-
503+
471504
open func indexesOfObjects(passingTest predicate: (Any, Int, UnsafeMutablePointer<ObjCBool>) -> Bool) -> IndexSet {
472505
return indexesOfObjects(options: [], passingTest: predicate)
473506
}
507+
474508
open func indexesOfObjects(options opts: NSEnumerationOptions = [], passingTest predicate: (Any, Int, UnsafeMutablePointer<ObjCBool>) -> Bool) -> IndexSet {
475509
return indexesOfObjects(at: IndexSet(integersIn: 0..<count), options: opts, passingTest: predicate)
476510
}
511+
477512
open func indexesOfObjects(at s: IndexSet, options opts: NSEnumerationOptions = [], passingTest predicate: (Any, Int, UnsafeMutablePointer<ObjCBool>) -> Bool) -> IndexSet {
478513
var result = IndexSet()
479514
enumerateObjects(at: s, options: opts) { (obj, idx, stop) in
@@ -589,22 +624,38 @@ open class NSArray : NSObject, NSCopying, NSMutableCopying, NSSecureCoding, NSCo
589624

590625
return lastEqual ? result + 1 : result
591626
}
592-
627+
628+
public convenience init(contentsOf url: URL, error: ()) throws {
629+
let plistDoc = try Data(contentsOf: url)
630+
guard let plistArray = try PropertyListSerialization.propertyList(from: plistDoc, options: [], format: nil) as? Array<Any>
631+
else {
632+
throw NSError(domain: NSCocoaErrorDomain, code: CocoaError.propertyListReadCorrupt.rawValue, userInfo: [NSURLErrorKey : url])
633+
}
634+
self.init(array: plistArray)
635+
}
636+
637+
@available(*, deprecated)
593638
public convenience init?(contentsOfFile path: String) {
594-
self.init(contentsOfURL: URL(fileURLWithPath: path))
639+
do {
640+
try self.init(contentsOf: URL(fileURLWithPath: path), error: ())
641+
} catch {
642+
return nil
643+
}
595644
}
596-
597-
public convenience init?(contentsOfURL url: URL) {
645+
646+
@available(*, deprecated)
647+
public convenience init?(contentsOf url: URL) {
598648
do {
599-
guard let plistDoc = try? Data(contentsOf: url),
600-
let plistArray = try PropertyListSerialization.propertyList(from: plistDoc, options: [], format: nil) as? Array<Any>
601-
else { return nil }
602-
self.init(array: plistArray)
649+
try self.init(contentsOf: url, error: ())
603650
} catch {
604651
return nil
605652
}
606653
}
607-
654+
655+
open func pathsMatchingExtensions(_ filterTypes: [String]) -> [String] {
656+
NSUnimplemented()
657+
}
658+
608659
override open var _cfTypeID: CFTypeID {
609660
return CFArrayGetTypeID()
610661
}
@@ -664,7 +715,11 @@ open class NSMutableArray : NSArray {
664715
}
665716
_storage.insert(_SwiftValue.store(anObject), at: index)
666717
}
667-
718+
719+
open func insert(_ objects: [Any], at indexes: IndexSet) {
720+
NSUnimplemented()
721+
}
722+
668723
open func removeLastObject() {
669724
if count > 0 {
670725
removeObject(at: count - 1)
@@ -687,22 +742,26 @@ open class NSMutableArray : NSArray {
687742
_storage.replaceSubrange(min..<max, with: [_SwiftValue.store(anObject) as AnyObject])
688743
}
689744

690-
public convenience init() {
691-
self.init(capacity: 0)
745+
public override init() {
746+
super.init()
692747
}
693748

694749
public init(capacity numItems: Int) {
695-
super.init(objects: [], count: 0)
750+
super.init(objects: nil, count: 0)
696751

697752
if type(of: self) === NSMutableArray.self {
698753
_storage.reserveCapacity(numItems)
699754
}
700755
}
701756

702-
public required convenience init(objects: UnsafePointer<AnyObject>!, count cnt: Int) {
703-
self.init(capacity: cnt)
704-
for idx in 0..<cnt {
705-
_storage.append(objects[idx])
757+
public required init(objects: UnsafePointer<AnyObject>?, count: Int) {
758+
precondition(count >= 0)
759+
precondition(count == 0 || objects != nil)
760+
761+
super.init()
762+
_storage.reserveCapacity(count)
763+
for idx in 0..<count {
764+
_storage.append(objects![idx])
706765
}
707766
}
708767

@@ -715,7 +774,7 @@ open class NSMutableArray : NSArray {
715774
}
716775
}
717776

718-
open func addObjectsFromArray(_ otherArray: [Any]) {
777+
open func addObjects(from otherArray: [Any]) {
719778
if type(of: self) === NSMutableArray.self {
720779
_storage += otherArray.map { _SwiftValue.store($0) as AnyObject }
721780
} else {
@@ -743,7 +802,7 @@ open class NSMutableArray : NSArray {
743802
}
744803
}
745804

746-
open func remove(_ anObject: Any, inRange range: NSRange) {
805+
open func remove(_ anObject: Any, in range: NSRange) {
747806
let idx = index(of: anObject, in: range)
748807
if idx != NSNotFound {
749808
removeObject(at: idx)
@@ -757,7 +816,7 @@ open class NSMutableArray : NSArray {
757816
}
758817
}
759818

760-
open func removeObject(identicalTo anObject: Any, inRange range: NSRange) {
819+
open func removeObject(identicalTo anObject: Any, in range: NSRange) {
761820
let idx = indexOfObjectIdentical(to: anObject, in: range)
762821
if idx != NSNotFound {
763822
removeObject(at: idx)
@@ -794,10 +853,10 @@ open class NSMutableArray : NSArray {
794853
open func replaceObjects(in range: NSRange, withObjectsFrom otherArray: [Any], range otherRange: NSRange) {
795854
var list = [Any]()
796855
otherArray._bridgeToObjectiveC().getObjects(&list, range:otherRange)
797-
replaceObjects(in: range, withObjectsFromArray:list)
856+
replaceObjects(in: range, withObjectsFrom: list)
798857
}
799858

800-
open func replaceObjects(in range: NSRange, withObjectsFromArray otherArray: [Any]) {
859+
open func replaceObjects(in range: NSRange, withObjectsFrom otherArray: [Any]) {
801860
if type(of: self) === NSMutableArray.self {
802861
_storage.reserveCapacity(count - range.length + otherArray.count)
803862
for idx in 0..<range.length {
@@ -815,7 +874,7 @@ open class NSMutableArray : NSArray {
815874
if type(of: self) === NSMutableArray.self {
816875
_storage = otherArray.map { _SwiftValue.store($0) }
817876
} else {
818-
replaceObjects(in: NSMakeRange(0, count), withObjectsFromArray: otherArray)
877+
replaceObjects(in: NSMakeRange(0, count), withObjectsFrom: otherArray)
819878
}
820879
}
821880

@@ -844,7 +903,7 @@ open class NSMutableArray : NSArray {
844903
for countedRange in indexes.rangeView {
845904
let range = NSMakeRange(countedRange.lowerBound, countedRange.upperBound - countedRange.lowerBound)
846905
let subObjects = objects[objectIndex..<objectIndex + range.length]
847-
self.replaceObjects(in: range, withObjectsFromArray: Array(subObjects))
906+
self.replaceObjects(in: range, withObjectsFrom: Array(subObjects))
848907
objectIndex += range.length
849908
}
850909
}
@@ -853,11 +912,11 @@ open class NSMutableArray : NSArray {
853912
self.setArray(self.sortedArray(compare, context: context))
854913
}
855914

856-
open func sort(_ cmptr: Comparator) {
857-
self.sort(options: [], usingComparator: cmptr)
915+
open func sort(comparator: Comparator) {
916+
self.sort(options: [], usingComparator: comparator)
858917
}
859918

860-
open func sort(options opts: NSSortOptions, usingComparator cmptr: Comparator) {
919+
open func sort(options opts: NSSortOptions = [], usingComparator cmptr: Comparator) {
861920
self.setArray(self.sortedArray(options: opts, usingComparator: cmptr))
862921
}
863922

Foundation/NSCFArray.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ internal final class _NSCFArray : NSMutableArray {
2020
fatalError()
2121
}
2222

23-
required init(objects: UnsafePointer<AnyObject>!, count cnt: Int) {
23+
required init(objects: UnsafePointer<AnyObject>?, count cnt: Int) {
2424
fatalError()
2525
}
2626

@@ -126,5 +126,5 @@ internal func _CFSwiftArrayRemoveAllValues(_ array: AnyObject) {
126126

127127
internal func _CFSwiftArrayReplaceValues(_ array: AnyObject, _ range: CFRange, _ newValues: UnsafeMutablePointer<Unmanaged<AnyObject>>, _ newCount: CFIndex) {
128128
NSUnimplemented()
129-
// (array as! NSMutableArray).replaceObjectsInRange(NSMakeRange(range.location, range.length), withObjectsFromArray: newValues.array(newCount))
129+
// (array as! NSMutableArray).replaceObjectsInRange(NSMakeRange(range.location, range.length), withObjectsFrom: newValues.array(newCount))
130130
}

0 commit comments

Comments
 (0)