Swift equivalent to Objective-C FourCharCode single quote literals (e.g. 'TEXT')

后端 未结 7 2335
青春惊慌失措
青春惊慌失措 2021-02-20 13:05

I am trying replicate some Objective C cocoa in Swift. All is good until I come across the following:

// Set a new type and creator:
unsigned long type = \'TEXT\         


        
7条回答
  •  南方客
    南方客 (楼主)
    2021-02-20 13:45

    Using NSHFSTypeCodeFromFileType does work, but only for 4-character strings wrapped with single quotes, aka 6-character strings. It returns 0 for unquoted 4-character strings.

    So wrap your 4-character string in ' ' before passing it to the function:

    extension FourCharCode: ExpressibleByStringLiteral {
    
        public init(stringLiteral value: StringLiteralType) {
            if value.count == 4 {
                self = NSHFSTypeCodeFromFileType("'\(value)'")
            } else {
                self = 0
            }
        }
    
    }
    

提交回复
热议问题