Skip to content

Commit

Permalink
解决内存泄漏
Browse files Browse the repository at this point in the history
  • Loading branch information
lixiang1994 committed Sep 19, 2024
1 parent f3a2b4d commit d1d1ff3
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 18 deletions.
2 changes: 1 addition & 1 deletion AttributedString.podspec
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Pod::Spec.new do |s|

s.name = "AttributedString"
s.version = "3.4.1"
s.version = "3.4.2"
s.summary = "基于Swift字符串插值快速构建你想要的富文本, 支持点击按住等事件获取, 支持多种类型过滤"

s.homepage = "https://github.com/lixiang1994/AttributedString"
Expand Down
2 changes: 1 addition & 1 deletion Sources/Action.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ extension ASAttributedString {

/// 内部使用
internal var isExternal: Bool = true
internal var handle: (() -> Void)?
internal var result: Action.Result?

public init(_ trigger: Trigger = .click, highlights: [Highlight] = .defalut, with callback: @escaping (Result) -> Void) {
self.trigger = trigger
Expand Down
7 changes: 3 additions & 4 deletions Sources/Extension/AppKit/NSTextFieldExtension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -174,9 +174,7 @@ extension ASAttributedStringWrapper where Base: NSTextField {
let result: Action.Result = string.value.get($1.key)
let actions: [Action] = $1.value.reduce(into: []) {
var temp = $1
temp.handle = {
temp.callback(result)
}
temp.result = result
$0.append(temp)
}
$0[$1.key] = actions
Expand Down Expand Up @@ -292,7 +290,8 @@ fileprivate extension NSTextField {
guard let touched = self.touched else { return }
let actions = touched.1.flatMap({ $0.value })
for action in actions where action.trigger.matching(sender) {
action.handle?()
guard let result = action.result else { return }
action.callback(result)
}
}

Expand Down
13 changes: 7 additions & 6 deletions Sources/Extension/UIKit/UILabel/UILabelExtension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -181,9 +181,7 @@ extension ASAttributedStringWrapper where Base: UILabel {
let result: Action.Result = string.value.get($1.key)
let actions: [Action] = $1.value.reduce(into: []) {
var temp = $1
temp.handle = {
temp.callback(result)
}
temp.result = result
$0.append(temp)
}
$0[$1.key] = actions
Expand Down Expand Up @@ -286,7 +284,8 @@ extension UILabel {
return
}
// 保证 touchesBegan -> Action -> touchesEnded 的调用顺序
ActionQueue.main.ended {
ActionQueue.main.ended { [weak self] in
guard let self = self else { return }
self.touched = nil
self.attributedText = touched.0.value
}
Expand All @@ -300,7 +299,8 @@ extension UILabel {
return
}
// 保证 touchesBegan -> Action -> touchesEnded 的调用顺序
ActionQueue.main.cancelled {
ActionQueue.main.cancelled { [weak self] in
guard let self = self else { return }
self.touched = nil
self.attributedText = touched.0.value
}
Expand All @@ -319,7 +319,8 @@ fileprivate extension UILabel {
guard let touched = self.touched else { return }
let actions = touched.1.flatMap({ $0.value })
for action in actions where action.trigger.matching(sender) {
action.handle?()
guard let result = action.result else { return }
action.callback(result)
}
}
}
Expand Down
13 changes: 7 additions & 6 deletions Sources/Extension/UIKit/UITextViewExtension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -184,9 +184,7 @@ extension ASAttributedStringWrapper where Base: UITextView {
let result: Action.Result = string.value.get($1.key)
let actions: [Action] = $1.value.reduce(into: []) {
var temp = $1
temp.handle = {
temp.callback(result)
}
temp.result = result
$0.append(temp)
}
$0[$1.key] = actions
Expand Down Expand Up @@ -327,7 +325,8 @@ extension UITextView {
return
}
// 保证 touchesBegan -> Action -> touchesEnded 的调用顺序
ActionQueue.main.ended {
ActionQueue.main.ended { [weak self] in
guard let self = self else { return }
self.touched = nil
self.attributedText = touched.0.value
self.layout()
Expand All @@ -342,7 +341,8 @@ extension UITextView {
return
}
// 保证 touchesBegan -> Action -> touchesEnded 的调用顺序
ActionQueue.main.cancelled {
ActionQueue.main.cancelled { [weak self] in
guard let self = self else { return }
self.touched = nil
self.attributedText = touched.0.value
self.layout()
Expand All @@ -362,7 +362,8 @@ fileprivate extension UITextView {
guard let touched = self.touched else { return }
let actions = touched.1.flatMap({ $0.value })
for action in actions where action.trigger.matching(sender) {
action.handle?()
guard let result = action.result else { return }
action.callback(result)
}
}
}
Expand Down

0 comments on commit d1d1ff3

Please sign in to comment.