Skip to content

Commit df18084

Browse files
committed
implement remaining methods and NS[Mutable]Copying
1 parent 722ed72 commit df18084

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

Foundation/NSAttributedString.swift

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,17 @@ open class NSAttributedString: NSObject, NSCopying, NSMutableCopying, NSSecureCo
4848
}
4949

5050
open func copy(with zone: NSZone? = nil) -> Any {
51-
NSUnimplemented()
51+
return self
5252
}
5353

5454
open override func mutableCopy() -> Any {
5555
return mutableCopy(with: nil)
5656
}
5757

5858
open func mutableCopy(with zone: NSZone? = nil) -> Any {
59-
NSUnimplemented()
59+
let mutableAttrString = NSMutableAttributedString(string: "")
60+
mutableAttrString.setAttributedString(self)
61+
return mutableAttrString
6062
}
6163

6264
/// The character contents of the receiver as an NSString object.
@@ -112,7 +114,9 @@ open class NSAttributedString: NSObject, NSCopying, NSMutableCopying, NSSecureCo
112114
}
113115

114116
/// Returns a Boolean value that indicates whether the receiver is equal to another given attributed string.
115-
open func isEqual(to other: NSAttributedString) -> Bool { NSUnimplemented() }
117+
open func isEqual(to other: NSAttributedString) -> Bool {
118+
return CFEqual(_cfObject, other._cfObject)
119+
}
116120

117121
/// Returns an NSAttributedString object initialized with the characters of a given string and no attribute information.
118122
public init(string: String) {
@@ -134,7 +138,9 @@ open class NSAttributedString: NSObject, NSCopying, NSMutableCopying, NSSecureCo
134138

135139
/// Returns an NSAttributedString object initialized with the characters and attributes of another given attributed string.
136140
public init(attributedString: NSAttributedString) {
137-
NSUnimplemented()
141+
let mutableCopy = attributedString.mutableCopy() as! NSMutableAttributedString
142+
_string = mutableCopy._string
143+
_attributeArray = mutableCopy._attributeArray
138144
}
139145

140146
/// Executes the block for each attribute in the range.
@@ -358,7 +364,8 @@ open class NSMutableAttributedString : NSAttributedString {
358364
}
359365

360366
open func setAttributedString(_ attrString: NSAttributedString) {
361-
NSUnimplemented()
367+
let fullStringRange = NSRange(location: 0, length: length)
368+
replaceCharacters(in: fullStringRange, with: attrString)
362369
}
363370

364371
open func beginEditing() {
@@ -369,6 +376,10 @@ open class NSMutableAttributedString : NSAttributedString {
369376
CFAttributedStringEndEditing(_cfMutableObject)
370377
}
371378

379+
open override func copy(with zone: NSZone? = nil) -> Any {
380+
return NSAttributedString(attributedString: self)
381+
}
382+
372383
public override init(string str: String) {
373384
super.init(string: str)
374385
_string = NSMutableString(string: str)

0 commit comments

Comments
 (0)