@@ -27,18 +27,33 @@ open class NSArray : NSObject, NSCopying, NSMutableCopying, NSSecureCoding, NSCo
27
27
return _SwiftValue. fetch ( nonOptional: _storage [ index] )
28
28
}
29
29
30
- public convenience override init ( ) {
31
- self . init ( objects : [ ] , count : 0 )
30
+ public override init ( ) {
31
+ _storage . reserveCapacity ( 0 )
32
32
}
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 {
37
48
_storage. append ( objects [ idx] )
38
49
}
39
50
}
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 ... ) {
42
57
self . init ( array: elements)
43
58
}
44
59
@@ -168,7 +183,7 @@ open class NSArray : NSObject, NSCopying, NSMutableCopying, NSSecureCoding, NSCo
168
183
open func addingObjects( from otherArray: [ Any ] ) -> [ Any ] {
169
184
return allObjects + otherArray
170
185
}
171
-
186
+
172
187
open func componentsJoined( by separator: String ) -> String {
173
188
// make certain to call NSObject's description rather than asking the string interpolator for the swift description
174
189
return allObjects. map { " \( $0) " } . joined ( separator: separator)
@@ -188,8 +203,15 @@ open class NSArray : NSObject, NSCopying, NSMutableCopying, NSSecureCoding, NSCo
188
203
}
189
204
return false
190
205
}
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
+
193
215
open func description( withLocale locale: Locale ? , indent level: Int ) -> String {
194
216
var descriptions = [ String] ( )
195
217
let cnt = count
@@ -406,12 +428,19 @@ open class NSArray : NSObject, NSCopying, NSMutableCopying, NSSecureCoding, NSCo
406
428
getObjects ( & objects, range: range)
407
429
return objects
408
430
}
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)
410
438
open func write( toFile path: String , atomically useAuxiliaryFile: Bool ) -> Bool {
411
439
return write ( to: URL ( fileURLWithPath: path) , atomically: useAuxiliaryFile)
412
440
}
413
-
441
+
414
442
// the atomically flag is ignored if url of a type that cannot be written atomically.
443
+ @available ( * , deprecated)
415
444
open func write( to url: URL , atomically: Bool ) -> Bool {
416
445
do {
417
446
let pListData = try PropertyListSerialization . data ( fromPropertyList: self , format: . xml, options: 0 )
@@ -421,7 +450,7 @@ open class NSArray : NSObject, NSCopying, NSMutableCopying, NSSecureCoding, NSCo
421
450
return false
422
451
}
423
452
}
424
-
453
+
425
454
open func objects( at indexes: IndexSet ) -> [ Any ] {
426
455
var objs = [ Any] ( )
427
456
indexes. rangeView. forEach {
@@ -430,33 +459,37 @@ open class NSArray : NSObject, NSCopying, NSMutableCopying, NSSecureCoding, NSCo
430
459
431
460
return objs
432
461
}
433
-
462
+
434
463
open subscript ( idx: Int ) -> Any {
435
464
guard idx < count && idx >= 0 else {
436
465
fatalError ( " \( self ) : Index out of bounds " )
437
466
}
438
467
439
468
return object ( at: idx)
440
469
}
441
-
470
+
442
471
open func enumerateObjects( _ block: ( Any , Int , UnsafeMutablePointer < ObjCBool > ) -> Swift . Void ) {
443
472
self . enumerateObjects ( options: [ ] , using: block)
444
473
}
474
+
445
475
open func enumerateObjects( options opts: NSEnumerationOptions = [ ] , using block: ( Any , Int , UnsafeMutablePointer < ObjCBool > ) -> Swift . Void ) {
446
476
self . enumerateObjects ( at: IndexSet ( integersIn: 0 ..< count) , options: opts, using: block)
447
477
}
478
+
448
479
open func enumerateObjects( at s: IndexSet , options opts: NSEnumerationOptions = [ ] , using block: ( Any , Int , UnsafeMutablePointer < ObjCBool > ) -> Swift . Void ) {
449
480
s. _bridgeToObjectiveC ( ) . enumerate ( options: opts) { ( idx, stop) in
450
481
block ( self . object ( at: idx) , idx, stop)
451
482
}
452
483
}
453
-
484
+
454
485
open func indexOfObject( passingTest predicate: ( Any , Int , UnsafeMutablePointer < ObjCBool > ) -> Bool ) -> Int {
455
- return indexOfObject ( [ ] , passingTest: predicate)
486
+ return indexOfObject ( options : [ ] , passingTest: predicate)
456
487
}
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 {
458
490
return indexOfObject ( at: IndexSet ( integersIn: 0 ..< count) , options: opts, passingTest: predicate)
459
491
}
492
+
460
493
open func indexOfObject( at s: IndexSet , options opts: NSEnumerationOptions = [ ] , passingTest predicate: ( Any , Int , UnsafeMutablePointer < ObjCBool > ) -> Bool ) -> Int {
461
494
var result = NSNotFound
462
495
enumerateObjects ( at: s, options: opts) { ( obj, idx, stop) -> Void in
@@ -467,13 +500,15 @@ open class NSArray : NSObject, NSCopying, NSMutableCopying, NSSecureCoding, NSCo
467
500
}
468
501
return result
469
502
}
470
-
503
+
471
504
open func indexesOfObjects( passingTest predicate: ( Any , Int , UnsafeMutablePointer < ObjCBool > ) -> Bool ) -> IndexSet {
472
505
return indexesOfObjects ( options: [ ] , passingTest: predicate)
473
506
}
507
+
474
508
open func indexesOfObjects( options opts: NSEnumerationOptions = [ ] , passingTest predicate: ( Any , Int , UnsafeMutablePointer < ObjCBool > ) -> Bool ) -> IndexSet {
475
509
return indexesOfObjects ( at: IndexSet ( integersIn: 0 ..< count) , options: opts, passingTest: predicate)
476
510
}
511
+
477
512
open func indexesOfObjects( at s: IndexSet , options opts: NSEnumerationOptions = [ ] , passingTest predicate: ( Any , Int , UnsafeMutablePointer < ObjCBool > ) -> Bool ) -> IndexSet {
478
513
var result = IndexSet ( )
479
514
enumerateObjects ( at: s, options: opts) { ( obj, idx, stop) in
@@ -589,22 +624,38 @@ open class NSArray : NSObject, NSCopying, NSMutableCopying, NSSecureCoding, NSCo
589
624
590
625
return lastEqual ? result + 1 : result
591
626
}
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)
593
638
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
+ }
595
644
}
596
-
597
- public convenience init ? ( contentsOfURL url: URL ) {
645
+
646
+ @available ( * , deprecated)
647
+ public convenience init ? ( contentsOf url: URL ) {
598
648
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: ( ) )
603
650
} catch {
604
651
return nil
605
652
}
606
653
}
607
-
654
+
655
+ open func pathsMatchingExtensions( _ filterTypes: [ String ] ) -> [ String ] {
656
+ NSUnimplemented ( )
657
+ }
658
+
608
659
override open var _cfTypeID : CFTypeID {
609
660
return CFArrayGetTypeID ( )
610
661
}
@@ -664,7 +715,11 @@ open class NSMutableArray : NSArray {
664
715
}
665
716
_storage. insert ( _SwiftValue. store ( anObject) , at: index)
666
717
}
667
-
718
+
719
+ open func insert( _ objects: [ Any ] , at indexes: IndexSet ) {
720
+ NSUnimplemented ( )
721
+ }
722
+
668
723
open func removeLastObject( ) {
669
724
if count > 0 {
670
725
removeObject ( at: count - 1 )
@@ -687,22 +742,26 @@ open class NSMutableArray : NSArray {
687
742
_storage. replaceSubrange ( min..< max, with: [ _SwiftValue. store ( anObject) as AnyObject ] )
688
743
}
689
744
690
- public convenience init ( ) {
691
- self . init ( capacity : 0 )
745
+ public override init ( ) {
746
+ super . init ( )
692
747
}
693
748
694
749
public init ( capacity numItems: Int ) {
695
- super. init ( objects: [ ] , count: 0 )
750
+ super. init ( objects: nil , count: 0 )
696
751
697
752
if type ( of: self ) === NSMutableArray . self {
698
753
_storage. reserveCapacity ( numItems)
699
754
}
700
755
}
701
756
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] )
706
765
}
707
766
}
708
767
@@ -715,7 +774,7 @@ open class NSMutableArray : NSArray {
715
774
}
716
775
}
717
776
718
- open func addObjectsFromArray ( _ otherArray: [ Any ] ) {
777
+ open func addObjects ( from otherArray: [ Any ] ) {
719
778
if type ( of: self ) === NSMutableArray . self {
720
779
_storage += otherArray. map { _SwiftValue. store ( $0) as AnyObject }
721
780
} else {
@@ -743,7 +802,7 @@ open class NSMutableArray : NSArray {
743
802
}
744
803
}
745
804
746
- open func remove( _ anObject: Any , inRange range: NSRange ) {
805
+ open func remove( _ anObject: Any , in range: NSRange ) {
747
806
let idx = index ( of: anObject, in: range)
748
807
if idx != NSNotFound {
749
808
removeObject ( at: idx)
@@ -757,7 +816,7 @@ open class NSMutableArray : NSArray {
757
816
}
758
817
}
759
818
760
- open func removeObject( identicalTo anObject: Any , inRange range: NSRange ) {
819
+ open func removeObject( identicalTo anObject: Any , in range: NSRange ) {
761
820
let idx = indexOfObjectIdentical ( to: anObject, in: range)
762
821
if idx != NSNotFound {
763
822
removeObject ( at: idx)
@@ -794,10 +853,10 @@ open class NSMutableArray : NSArray {
794
853
open func replaceObjects( in range: NSRange , withObjectsFrom otherArray: [ Any ] , range otherRange: NSRange ) {
795
854
var list = [ Any] ( )
796
855
otherArray. _bridgeToObjectiveC ( ) . getObjects ( & list, range: otherRange)
797
- replaceObjects ( in: range, withObjectsFromArray : list)
856
+ replaceObjects ( in: range, withObjectsFrom : list)
798
857
}
799
858
800
- open func replaceObjects( in range: NSRange , withObjectsFromArray otherArray: [ Any ] ) {
859
+ open func replaceObjects( in range: NSRange , withObjectsFrom otherArray: [ Any ] ) {
801
860
if type ( of: self ) === NSMutableArray . self {
802
861
_storage. reserveCapacity ( count - range. length + otherArray. count)
803
862
for idx in 0 ..< range. length {
@@ -815,7 +874,7 @@ open class NSMutableArray : NSArray {
815
874
if type ( of: self ) === NSMutableArray . self {
816
875
_storage = otherArray. map { _SwiftValue. store ( $0) }
817
876
} else {
818
- replaceObjects ( in: NSMakeRange ( 0 , count) , withObjectsFromArray : otherArray)
877
+ replaceObjects ( in: NSMakeRange ( 0 , count) , withObjectsFrom : otherArray)
819
878
}
820
879
}
821
880
@@ -844,7 +903,7 @@ open class NSMutableArray : NSArray {
844
903
for countedRange in indexes. rangeView {
845
904
let range = NSMakeRange ( countedRange. lowerBound, countedRange. upperBound - countedRange. lowerBound)
846
905
let subObjects = objects [ objectIndex..< objectIndex + range. length]
847
- self . replaceObjects ( in: range, withObjectsFromArray : Array ( subObjects) )
906
+ self . replaceObjects ( in: range, withObjectsFrom : Array ( subObjects) )
848
907
objectIndex += range. length
849
908
}
850
909
}
@@ -853,11 +912,11 @@ open class NSMutableArray : NSArray {
853
912
self . setArray ( self . sortedArray ( compare, context: context) )
854
913
}
855
914
856
- open func sort( _ cmptr : Comparator ) {
857
- self . sort ( options: [ ] , usingComparator: cmptr )
915
+ open func sort( comparator : Comparator ) {
916
+ self . sort ( options: [ ] , usingComparator: comparator )
858
917
}
859
918
860
- open func sort( options opts: NSSortOptions , usingComparator cmptr: Comparator ) {
919
+ open func sort( options opts: NSSortOptions = [ ] , usingComparator cmptr: Comparator ) {
861
920
self . setArray ( self . sortedArray ( options: opts, usingComparator: cmptr) )
862
921
}
863
922
0 commit comments