先前的属性在后续调用中传播
Previous attributes propagated on subsequent calls
我遇到了一个奇怪的场景,我的扩展方法生成的先前属性(这是属性字符串的自定义属性)也被分配给后续调用。它的行为就像一个静态值被缓存并在我调用它的任何地方分配它。
extension Model {
func attributedString() -> NSAttributedString {
// Generate an icon, represented by the type of model
let icon = ...
// Merge both icon and the value of the ziptag
let preferredAttributedString = NSMutableAttributedString(attributedString: icon.attributedString())
let attributedValue = NSAttributedString(string: self.value)
preferredAttributedString.append(attributedValue)
// Mention Attribute
let mentionAttributeKey = NSAttributedStringKey(HKWMentionAttributeName)
guard let mentionAttribute = HKWMentionsAttribute.mention(withText: self.value, identifier: self.id) else {
fatalError("mentionAttribute can't be nil")
}
mentionAttribute.metadata = self.entityMetadata()
// Color Attribute
let foregroundColor = UIColor.blue
// Set the attributes
let attributes: [NSAttributedStringKey : Any] = [mentionAttributeKey : mentionAttribute,
NSAttributedStringKey.foregroundColor : foregroundColor]
preferredAttributedString.addAttributes(attributes, range: preferredAttributedString.range)
return preferredAttributedString
}
}
这就是我复制它的方式。可以说我确实有两个模型类型的对象,它们具有上面声明和实现的扩展方法。
let modelA = Model()
let modelB = Model()
let attributedStringA = modelA.attributedString()
let attributedStringB = modelB.attributedString()
我已经记录了上面的两个属性字符串,我希望它会分别显示 modelA
和 modelB
属性,但它只在两个属性字符串上生成 modelA
要添加有关该问题的更多数据。上面生成的图标是一种自定义字体 FontAwesome,它还会生成一个属性字符串并添加到 Model
的属性字符串之前(使用系统字体)。
我 运行 Xcode 的 lldb 在生成所需属性的语句上,它的报告是正确的;但是一旦我在属性字符串上分配了属性,上述问题就开始出现
我通过在我的问题的代码段上添加一个未使用的自定义属性来修复它
// Mention Attribute
let mentionAttributeKey = NSAttributedKey(HKWMentionAttributeName)
let mentionAttribute = ....
// Foreground Color
let foregroundColor = UIColor.blue
// Trash Attribute
let trashAttributeKey = NSAttributedKey("TrashAttribute")
let trashAttribute = Trash()
添加垃圾桶属性后,完美运行;因为此扩展方法以前专门针对 Attributed String
的 Icon 部分制作的挥之不去的属性
我遇到了一个奇怪的场景,我的扩展方法生成的先前属性(这是属性字符串的自定义属性)也被分配给后续调用。它的行为就像一个静态值被缓存并在我调用它的任何地方分配它。
extension Model {
func attributedString() -> NSAttributedString {
// Generate an icon, represented by the type of model
let icon = ...
// Merge both icon and the value of the ziptag
let preferredAttributedString = NSMutableAttributedString(attributedString: icon.attributedString())
let attributedValue = NSAttributedString(string: self.value)
preferredAttributedString.append(attributedValue)
// Mention Attribute
let mentionAttributeKey = NSAttributedStringKey(HKWMentionAttributeName)
guard let mentionAttribute = HKWMentionsAttribute.mention(withText: self.value, identifier: self.id) else {
fatalError("mentionAttribute can't be nil")
}
mentionAttribute.metadata = self.entityMetadata()
// Color Attribute
let foregroundColor = UIColor.blue
// Set the attributes
let attributes: [NSAttributedStringKey : Any] = [mentionAttributeKey : mentionAttribute,
NSAttributedStringKey.foregroundColor : foregroundColor]
preferredAttributedString.addAttributes(attributes, range: preferredAttributedString.range)
return preferredAttributedString
}
}
这就是我复制它的方式。可以说我确实有两个模型类型的对象,它们具有上面声明和实现的扩展方法。
let modelA = Model()
let modelB = Model()
let attributedStringA = modelA.attributedString()
let attributedStringB = modelB.attributedString()
我已经记录了上面的两个属性字符串,我希望它会分别显示 modelA
和 modelB
属性,但它只在两个属性字符串上生成 modelA
要添加有关该问题的更多数据。上面生成的图标是一种自定义字体 FontAwesome,它还会生成一个属性字符串并添加到 Model
的属性字符串之前(使用系统字体)。
我 运行 Xcode 的 lldb 在生成所需属性的语句上,它的报告是正确的;但是一旦我在属性字符串上分配了属性,上述问题就开始出现
我通过在我的问题的代码段上添加一个未使用的自定义属性来修复它
// Mention Attribute
let mentionAttributeKey = NSAttributedKey(HKWMentionAttributeName)
let mentionAttribute = ....
// Foreground Color
let foregroundColor = UIColor.blue
// Trash Attribute
let trashAttributeKey = NSAttributedKey("TrashAttribute")
let trashAttribute = Trash()
添加垃圾桶属性后,完美运行;因为此扩展方法以前专门针对 Attributed String
的 Icon 部分制作的挥之不去的属性