파인더에 표시된 컬러 라벨을 설정하려고합니다. 내가 아는 유일한 함수는 setResourceValue입니다. 그러나 이것은 현지화 된 이름이 필요합니다!스위프트 3 : 파인더 라벨 색상을 설정하십시오.
나는 모국어와 영어도 이미지 할 수 있지만 다른 모든 것들은 잘 모릅니다. 나는 이것이 길이어야한다는 것을 믿을 수 없다.
enum이나 int와 같은 표준 매개 변수를 사용하고 지역화 된 색상 이름을 제공하는 번역 기능입니까? 새로운 URLResourceValues()
구조체에 대한 감사와 태그 번호를
let colorNamesEN = [ "None", "Gray", "Green", "Purple", "Blue", "Yellow", "Red", "Orange" ]
let colorNamesDE = [ "", "Grau", "Grün", "Lila", "Blau", "Gelb", "Rot", "Orange" ]
public enum TagColors : Int8 {
case None = -1, Gray, Green, Purple, Blue, Yellow, Red, Orange, Max
}
//let theURL : NSURL = NSURL.fileURLWithPath("/Users/dirk/Documents/MyLOG.txt")
extension NSURL {
// e.g. theURL.setColors(0b01010101)
func tagColorValue(tagcolor : TagColors) -> UInt16 {
return 1 << UInt16(tagcolor.rawValue)
}
func addTagColor(tagcolor : TagColors) -> Bool {
let bits : UInt16 = tagColorValue(tagcolor) | self.getTagColors()
return setTagColors(bits)
}
func remTagColor(tagcolor : TagColors) -> Bool {
let bits : UInt16 = ~tagColorValue(tagcolor) & self.getTagColors()
return setTagColors(bits)
}
func setColors(tagcolor : TagColors) -> Bool {
let bits : UInt16 = tagColorValue(tagcolor)
return setTagColors(bits)
}
func setTagColors(colorMask : UInt16) -> Bool {
// get string for all available and requested bits
let arr = colorBitsToStrings(colorMask & (tagColorValue(TagColors.Max)-1))
do {
try self.setResourceValue(arr, forKey: NSURLTagNamesKey)
return true
}
catch {
print("Could not write to file \(self.absoluteURL)")
return false
}
}
func getTagColors() -> UInt16 {
return getAllTagColors(self.absoluteURL)
}
}
// let initialBits: UInt8 = 0b00001111
func colorBitsToStrings(colorMask : UInt16) -> NSArray {
// translate bits to (localized!) color names
let countryCode = NSLocale.currentLocale().objectForKey(NSLocaleLanguageCode)!
// I don't know how to automate it for all languages possible!!!!
let colorNames = countryCode as! String == "de" ? colorNamesDE : colorNamesEN
var tagArray = [String]()
var bitNumber : Int = -1 // ignore first loop
for colorName in colorNames {
if bitNumber >= 0 {
if colorMask & UInt16(1<<bitNumber) > 0 {
tagArray.append(colorName)
}
}
bitNumber += 1
}
return tagArray
}
func getAllTagColors(file : NSURL) -> UInt16 {
var colorMask : UInt16 = 0
// translate (localized!) color names to bits
let countryCode = NSLocale.currentLocale().objectForKey(NSLocaleLanguageCode)!
// I don't know how to automate it for all languages possible!!!!
let colorNames = countryCode as! String == "de" ? colorNamesDE : colorNamesEN
var bitNumber : Int = -1 // ignore first loop
var tags : AnyObject?
do {
try file.getResourceValue(&tags, forKey: NSURLTagNamesKey)
if tags != nil {
let tagArray = tags as! [String]
for colorName in colorNames {
if bitNumber >= 0 {
// color name listed?
if tagArray.filter({ $0 == colorName }).count > 0 {
colorMask |= UInt16(1<<bitNumber)
}
}
bitNumber += 1
}
}
} catch {
// process the error here
}
return colorMask
}
이것을 구현하려 할 때 https://github.com/billgarrison/SOExtendedAttributes의 일부로 'setExtendedAttributeData'도 설치해야했습니다. 그러나 이것은 스위프트 모듈이 아니며이 언어로 시작합니다. 이 C 모듈을 사용하려면 무엇을해야합니까? 스위프트 전용 솔루션이 있습니까? –
Xcode 프로젝트에 SOExtendedAttributes를 쉽게 통합 할 수 있습니다. (그리고 Swift에 대해 걱정할 필요가 없습니다.) zip을 다운로드 한 다음 "NSURL + SOExtendedAttributes.h"및 "NSURL + SOExtendedAttributes.m" 프로젝트 네비게이터에서 자신의 파일 옆에 있습니다. 나타나는 패널에서 "복사"상자를 선택해야합니다. 그런 다음 Xcode는 브리징 헤더를 만들 것인지 묻습니다. 네 라고 말하다. 그런 다음이 헤더에'#import "NSURL + SOExtendedAttributes.h"': 행을 추가하십시오. 이제'setExtendedAttributeData'를 사용할 수 있습니다. :) – Moritz