Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/danieltmbr/bencode
Swift bencode decoder & encoder
https://github.com/danieltmbr/bencode
bencode ios macos swift torrent
Last synced: about 9 hours ago
JSON representation
Swift bencode decoder & encoder
- Host: GitHub
- URL: https://github.com/danieltmbr/bencode
- Owner: danieltmbr
- License: mit
- Created: 2017-09-13T14:57:23.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2023-12-17T17:45:18.000Z (9 months ago)
- Last Synced: 2024-09-24T20:38:56.139Z (about 14 hours ago)
- Topics: bencode, ios, macos, swift, torrent
- Language: Swift
- Size: 216 KB
- Stars: 15
- Watchers: 5
- Forks: 10
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Bencode
Bencode is a general purpose bencode encoder & decoder written in Swift 5.
Inspired by [VFK SwiftyBencode][vfk] & [arvidsigvardsson/Bencode][abc]
## Installation
**⚠️ Xcode 9 is required ⚠️**
### Swift Package Manager
Just add a new package on your project, and point to this repository.
### CocoaPods
Bencode is available through [CocoaPods](http://cocoapods.org).
To install it, simply add the following line to your Podfile:```ruby
pod 'Bencode'
```## Usage
**Initialize with URL:**
```swift
let bencode: Bencode? = Bencode(file: torrentUrl)
```**Initialize with bencoded string:**
```swift
let bencode: Bencode? = Bencode(bencodedString: content)
```**Would you like to know more of the parsing failures, use the decoder:**
```swift
do {
let bencode = Bencoder().decode(from: fileURL)
} catch let error {
print(error)
}
```**Accessing properties:**
Accessing properties is very handy with subscripts & accessors.
Value accessors `.int`, `.string`, `.list` & `.dict` are optional.
Subscripts produces **BencodeOptional** enums, by doing so you can chain optional subscripts without having to write `?` or `!` behind every one of them.
Bencode also comfort to Sequence protocol, so you can use `map`, `filter`, `foreach` etc. on them. :)```swift
let bencode = Bencode(file: torrentUrl)let info = bencode["info"]
let files = info["files"]// You can use them as a sequence
// .values is a shorthand for .map{ $0.value }
files.values.forEach {
print($0["path"][0].string!)
}// Easy optional chaning without unwapping every stage:
let filePath: String? = info["files"][0]["path"][0].string
let fileLength: Int? = bencode["info"]["files"]["length"].int
```## Help
* Post any issues you find
* Post new feature requests
* Pull requests are welcome## Author
danieltmbr, [email protected]
## License
Bencode is available under the MIT license. See the LICENSE file for more info.
[vfk]: https://github.com/VFK/SwiftyBencode
[abc]: https://github.com/arvidsigvardsson/Bencode