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

后端 未结 7 2583
天命终不由人
天命终不由人 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:45

    In Swift 4 or later, I use this code - if the string is not 4 characters in size, it will return an OSType(0):

    extension String {
        public func osType() -> OSType {
           var result:UInt = 0
    
           if let data = self.data(using: .macOSRoman), data.count == 4
           {
                data.withUnsafeBytes { (ptr:UnsafePointer) in
                    for i in 0.. "APPL" in ASCII
    

提交回复
热议问题