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

后端 未结 7 2331
青春惊慌失措
青春惊慌失措 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:18

    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
    

提交回复
热议问题