So, you can write some extension for your Dog like this:
Swift 2
extension Dog {
var Name: String {
mutating get {
return withUnsafePointer(&self.name) {namePtr in
let charPtr = UnsafePointer<CChar>(namePtr)
//Sorry, this code may crash here...
return String(CString: charPtr, encoding: NSUTF8StringEncoding)!
}
}
set {
withUnsafeMutablePointer(&self.name) {namePtr in
let charPtr = UnsafeMutablePointer<CChar>(namePtr)
let size = sizeofValue(self.name)
strncpy(charPtr, newValue, size - 1)
charPtr[size - 1] = 0
}
}
}
}
Swift 3(Tested with Xcode 8 beta 4)
extension Dog {
var Name: String {
mutating get {
return withUnsafePointer(&self.name) {namePtr in
let charPtr = UnsafePointer<CChar>(namePtr)
//The result may contain the Unicode replacement character ("\u{FFFD}")
return String(cString: charPtr)
}
}
set {
withUnsafeMutablePointer(&self.name) {namePtr in
let charPtr = UnsafeMutablePointer<CChar>(namePtr)
let size = sizeofValue(self.name)
strncpy(charPtr, newValue, size - 1)
charPtr[size - 1] = 0
}
}
}
}
Seeing the linked thread in Martin R's comment, there may be some room to improve...
(Especially using strlcpy reduces your code size with better safety. Please check it.)
But anyway, it works as:
dog.Name = "H"
print(dog.Name) //->H
I have found that getter of Name may crash your app...
"It works" in a restriction that you store only ASCII characters.