I know this question has been asked for ObjectiveC, but my Swift isn't strong enough yet to transliterate the char *bytes stuff.
So given
let string = "600DBEEF"
How do I create an NSData which represents those 4 bytes: 60 0D BE EF?
I know this question has been asked for ObjectiveC, but my Swift isn't strong enough yet to transliterate the char *bytes stuff.
So given
let string = "600DBEEF"
How do I create an NSData which represents those 4 bytes: 60 0D BE EF?
 
    
    With the arrival of Swift3 and the new Foundation Data type, I finally circled back to this:
extension UnicodeScalar {
    var hexNibble:UInt8 {
        let value = self.value
        if 48 <= value && value <= 57 {
            return UInt8(value - 48)
        }
        else if 65 <= value && value <= 70 {
            return UInt8(value - 55)
        }
        else if 97 <= value && value <= 102 {
            return UInt8(value - 87)
        }
        fatalError("\(self) not a legal hex nibble")
    }
}
extension Data {
    init(hex:String) {
        let scalars = hex.unicodeScalars
        var bytes = Array<UInt8>(repeating: 0, count: (scalars.count + 1) >> 1)
        for (index, scalar) in scalars.enumerated() {
            var nibble = scalar.hexNibble
            if index & 1 == 0 {
                nibble <<= 4
            }
            bytes[index >> 1] |= nibble
        }
        self = Data(bytes: bytes)
    }
}
Now I can construct Data objects in a fashion similar to their printed form:
Data(hex: "600dBeef")
 
    
    Answer in swift, XCode beta 6
    let string = "600DBEEF"
    let length = string.characters.count
    let rawData = UnsafeMutablePointer<CUnsignedChar>.alloc(length/2)
    var rawIndex = 0
    for var index = 0; index < length; index+=2{
      let single = NSMutableString()
      single.appendString(string.substringWithRange(Range(start:string.startIndex.advancedBy(index), end:string.startIndex.advancedBy(index+2))))
      rawData[rawIndex] = UInt8(single as String, radix:16)!
      rawIndex++
    }
    let data:NSData = NSData(bytes: rawData, length: length/2)
    rawData.dealloc(length/2)
