Skip to content

Commit ded2650

Browse files
committed
Tests.
1 parent a07e590 commit ded2650

File tree

3 files changed

+54
-6
lines changed

3 files changed

+54
-6
lines changed

README.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
[![gitee 镜像](https://img.shields.io/badge/%E9%95%9C%E5%83%8F-gitee-C61E22.svg?style=flat-square)](https://gitee.com/b9swift/AssociatedObject)
77
[![GitHub Source](https://img.shields.io/badge/Source-GitHub-24292F.svg?style=flat-square)](https://github.com/b9swift/AssociatedObject)
88

9-
Objective-C associated object 的 Swift 封装
9+
方便地在 Swift 中利用 Objective-C 关联对象
1010

11-
Objective-C associated value wrapper.
11+
Objective-C associated value wrapper in Swift.
1212

1313
## 集成
1414

@@ -24,9 +24,15 @@ Using Swift Package Manager or import manually.
2424

2525
## 使用
2626

27+
定义扩展属性可以像下面这样,不仅支持 Objective-C 对象,各种 Swift 类型也支持。
28+
2729
## Usage
2830

31+
You can define extended properties like below. All kinds of Swift types are also supported, not only Objective-C objects.
32+
2933
```swift
34+
import B9AssociatedObject
35+
3036
private let fooAssociation = AssociatedObject<String>()
3137
extension SomeObject {
3238
var foo: String? {

Sources/B9AssociatedObject/B9AssociatedObject.swift

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
AssociatedObject.swift
2+
B9AssociatedObject.swift
33

44
Copyright © 2020-2021 RFUI.
55
https://github.com/b9swift/AssociatedObject
@@ -37,7 +37,11 @@ public final class AssociatedObject<T> {
3737
/// Accesses the associated value.
3838
/// - Parameter index: The source object for the association.
3939
public subscript(index: AnyObject) -> T? {
40-
get { objc_getAssociatedObject(index, Unmanaged.passUnretained(self).toOpaque()) as? T }
41-
set { objc_setAssociatedObject(index, Unmanaged.passUnretained(self).toOpaque(), newValue, policy) }
40+
get {
41+
objc_getAssociatedObject(index, Unmanaged.passUnretained(self).toOpaque()) as? T
42+
}
43+
set {
44+
objc_setAssociatedObject(index, Unmanaged.passUnretained(self).toOpaque(), newValue, policy)
45+
}
4246
}
4347
}

Tests/MainTests/MainTests.swift

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,44 @@
11
import XCTest
22
@testable import B9AssociatedObject
33

4-
final class AssociatedObjectTests: XCTestCase {
4+
enum EnumType: Equatable, CustomDebugStringConvertible {
5+
case one
6+
case two
7+
8+
var debugDescription: String {
9+
switch self {
10+
case .one:
11+
return "<EnumType.one>"
12+
case .two:
13+
return "<EnumType.two>"
14+
}
15+
}
16+
}
17+
18+
let enumAssociation = AssociatedObject<EnumType>()
519

20+
class A {}
21+
22+
extension A {
23+
var enumValue: EnumType? {
24+
get { enumAssociation[self] }
25+
set { enumAssociation[self] = newValue }
26+
}
27+
}
28+
29+
final class AssociatedObjectTests: XCTestCase {
30+
func testSwiftEnum() {
31+
let a1 = A()
32+
let a2 = A()
33+
assert(a1.enumValue == nil)
34+
a1.enumValue = .one
35+
a2.enumValue = .two
36+
debugPrint(a1.enumValue as Any, a2.enumValue as Any)
37+
assert(a1.enumValue == .one)
38+
assert(a2.enumValue == .two)
39+
assert(a1.enumValue != a2.enumValue)
40+
a1.enumValue = .two
41+
assert(a1.enumValue == .two)
42+
assert(a1.enumValue == a2.enumValue)
43+
}
644
}

0 commit comments

Comments
 (0)