Skip to content

Commit a07e590

Browse files
committed
First version.
1 parent 52327f9 commit a07e590

File tree

7 files changed

+112
-25
lines changed

7 files changed

+112
-25
lines changed

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright © 2020-2021 BB9z
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

Package.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44
import PackageDescription
55

66
let package = Package(
7-
name: "AssociatedObject",
7+
name: "B9AssociatedObject",
88
products: [
99
// Products define the executables and libraries a package produces, and make them visible to other packages.
1010
.library(
11-
name: "AssociatedObject",
12-
targets: ["AssociatedObject"]),
11+
name: "B9AssociatedObject",
12+
targets: ["B9AssociatedObject"]),
1313
],
1414
dependencies: [
1515
// Dependencies declare other packages that this package depends on.
@@ -19,10 +19,10 @@ let package = Package(
1919
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
2020
// Targets can depend on other targets in this package, and on products in packages this package depends on.
2121
.target(
22-
name: "AssociatedObject",
22+
name: "B9AssociatedObject",
2323
dependencies: []),
2424
.testTarget(
25-
name: "AssociatedObjectTests",
26-
dependencies: ["AssociatedObject"]),
25+
name: "MainTests",
26+
dependencies: ["B9AssociatedObject"]),
2727
]
2828
)

README.md

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,37 @@
1-
# AssociatedObject
1+
# B9AssociatedObject
22

3-
A description of this package.
3+
[![Swift Version](https://img.shields.io/badge/Swift-5.3+-F05138.svg?style=flat-square)](https://swift.org)
4+
[![Swift Package Manager](https://img.shields.io/badge/spm-compatible-F05138.svg?style=flat-square)](https://swift.org/package-manager)
5+
[![Build Status](https://img.shields.io/github/workflow/status/b9swift/Action/Swift?style=flat-square&colorA=555555&colorB=F05138)](https://github.com/b9swift/AssociatedObject/actions)
6+
[![gitee 镜像](https://img.shields.io/badge/%E9%95%9C%E5%83%8F-gitee-C61E22.svg?style=flat-square)](https://gitee.com/b9swift/AssociatedObject)
7+
[![GitHub Source](https://img.shields.io/badge/Source-GitHub-24292F.svg?style=flat-square)](https://github.com/b9swift/AssociatedObject)
8+
9+
Objective-C associated object 的 Swift 封装。
10+
11+
Objective-C associated value wrapper.
12+
13+
## 集成
14+
15+
使用 Swift Package Manager 或手工导入。
16+
17+
You can also use [GitHub source](https://github.com/b9swift/AssociatedObject).
18+
19+
## Installation
20+
21+
Using Swift Package Manager or import manually.
22+
23+
你也可以使用 [gitee 镜像](https://gitee.com/b9swift/AssociatedObject)
24+
25+
## 使用
26+
27+
## Usage
28+
29+
```swift
30+
private let fooAssociation = AssociatedObject<String>()
31+
extension SomeObject {
32+
var foo: String? {
33+
get { fooAssociation[self] }
34+
set { fooAssociation[self] = newValue }
35+
}
36+
}
37+
```

Sources/AssociatedObject/AssociatedObject.swift

Lines changed: 0 additions & 6 deletions
This file was deleted.
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
AssociatedObject.swift
3+
4+
Copyright © 2020-2021 RFUI.
5+
https://github.com/b9swift/AssociatedObject
6+
7+
The MIT License
8+
https://opensource.org/licenses/MIT
9+
*/
10+
11+
import Foundation
12+
13+
/**
14+
Objective-C associated value wrapper.
15+
16+
Usage
17+
18+
```
19+
private let fooAssociation = AssociatedObject<String>()
20+
extension SomeObject {
21+
var foo: String? {
22+
get { fooAssociation[self] }
23+
set { fooAssociation[self] = newValue }
24+
}
25+
}
26+
```
27+
*/
28+
public final class AssociatedObject<T> {
29+
private let policy: objc_AssociationPolicy
30+
31+
/// Creates an associated value wrapper.
32+
/// - Parameter policy: The policy for the association.
33+
public init(policy: objc_AssociationPolicy = .OBJC_ASSOCIATION_RETAIN_NONATOMIC) {
34+
self.policy = policy
35+
}
36+
37+
/// Accesses the associated value.
38+
/// - Parameter index: The source object for the association.
39+
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) }
42+
}
43+
}

Tests/AssociatedObjectTests/AssociatedObjectTests.swift

Lines changed: 0 additions & 11 deletions
This file was deleted.

Tests/MainTests/MainTests.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import XCTest
2+
@testable import B9AssociatedObject
3+
4+
final class AssociatedObjectTests: XCTestCase {
5+
6+
}

0 commit comments

Comments
 (0)