Skip to content

Commit ec1b067

Browse files
committed
move dummy attr str creating from mutableCopy to init(attributedString:)
1 parent f43e647 commit ec1b067

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

Foundation/NSAttributedString.swift

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,7 @@ open class NSAttributedString: NSObject, NSCopying, NSMutableCopying, NSSecureCo
5656
}
5757

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

6462
/// The character contents of the receiver as an NSString object.
@@ -146,9 +144,13 @@ open class NSAttributedString: NSObject, NSCopying, NSMutableCopying, NSSecureCo
146144

147145
/// Returns an NSAttributedString object initialized with the characters and attributes of another given attributed string.
148146
public init(attributedString: NSAttributedString) {
149-
let mutableCopy = attributedString.mutableCopy() as! NSMutableAttributedString
150-
_string = mutableCopy._string
151-
_attributeArray = mutableCopy._attributeArray
147+
// create an empty mutable attr string then immediately replace all of its contents
148+
let mutableAttributedString = NSMutableAttributedString(string: "")
149+
mutableAttributedString.setAttributedString(attributedString)
150+
151+
// use the resulting _string and _attributeArray to initialize a new instance
152+
_string = mutableAttributedString._string
153+
_attributeArray = mutableAttributedString._attributeArray
152154
}
153155

154156
/// Executes the block for each attribute in the range.

0 commit comments

Comments
 (0)