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

后端 未结 7 2584
天命终不由人
天命终不由人 2021-02-20 12:54

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:33

    Swift 5 Update:

    extension String {
        func osType() -> OSType {
            return OSType(
                data(using: .macOSRoman)?
                    .withUnsafeBytes {
                        $0.reduce(into: UInt(0)) { $0 = $0 << 8 + UInt($1) }
                    } ?? 0
            )
        }
    }
    

提交回复
热议问题