GithubHelp home page GithubHelp logo

chinhbui / extendedattributes Goto Github PK

View Code? Open in Web Editor NEW

This project forked from amosavian/extendedattributes

1.0 2.0 0.0 14 KB

Working with file's extended attributes easily

License: MIT License

Ruby 40.30% Swift 59.70%

extendedattributes's Introduction

ExtendedAttributes

This library handles file extended attributes by extending URL struct.

Swift Version Platform License Release version

CocoaPods version Carthage compatible Cocoapods Downloads Cocoapods Apps

Requirements

  • Swift 4.0 or higher
  • macOS, iOS, tvOS or Linux
  • XCode 9.0

Installation

First you must clone this project from github:

git clone https://github.com/amosavian/ExtendedAttributes

Then you can either install manually by adding Sources/ExtendedAttributes directory to your project or create a xcodeproj file and add it as a dynamic framework:

swift package generate-xcodeproj

Usage

Extended attributes only work with urls that begins with file:///.

Listing

To get which extended attributes are set for file:

do {
    print(try url.listExtendedAttributes())
} catch {
    print(error.localizedDescription)
}

Retrieving

To check either a specific extended attribute exists or not:

if url.hasExtendedAttribute(forName: "eaName") {
    // Do something
}

To retrieve raw data for an extended attribute, simply use this code as template, Please note if extended attribute doesn't exist, it will throw an error.

do {
    let data = try url.extendedAttribute(forName: "eaName")
    print(data as NSData)
} catch {
    print(error.localizedDescription)
}

You can retrieve values of extended attributes if they are set with standard plist binary format. This can be String, Int/NSNumber, Double, Bool, URL, Date, Array or Dictionary. Arrays should not contain nil value.

To retrieve raw data for an extended attribute, simply use this code as template:

do {
    let notes: String = try url.extendedAttributeValue(forName: "notes")
    print("Notes:", notes)
    let isDownloeded: Bool = try url.extendedAttributeValue(forName: "isdownloaded")
    print("isDownloaded:", isDownloeded)
    let originURL: URL = try url.extendedAttributeValue(forName: "originurl")
    print("Original url:", originurl)
} catch {
    print(error.localizedDescription)
}

or to list all values of a file:

do {
    for name in try url.listExtendedAttributes() {
        let value = try url.extendedAttributeValue(forName: name)
        print(name, ":" , value)
    }
} catch {
    print(error.localizedDescription)
}

Setting attributes

To set raw data for an extended attribute:

do {
    try url.setExtendedAttribute(data: Data(bytes: [0xFF, 0x20]), forName: "data")
} catch {
    print(error.localizedDescription)
}

To set a value for an extended attribute:

do {
    let dictionary: [String: Any] = ["name": "Amir", "age": 30]
    try url.setExtendedAttribute(value: dictionary, forName: "identity")
} catch {
    print(error.localizedDescription)
}

Removing

To remove an extended attribute:

do {
    try url.removeExtendedAttribute(forName: "identity")
} catch {
    print(error.localizedDescription)
}

Known issues

Check Issues page.

Contribute

We would love for you to contribute to ExtendedAttributes, check the LICENSE file for more info.

extendedattributes's People

Contributors

amosavian avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.