dasmaximum
dasmaximum

Reputation: 93

convert a single c char to string in swift

I am trying to user MailCore2 (Objective C) in my Swift project.

At the moment I try to retrieve all IMAP Folders from a server (so far that works) and to split the paths by the delimiter.

INBOX.Sent
INBOX.Drafts

to

INBOX
 > Sent
 > Drafts

The Class MCOIMAPFolder has the property delimiter which is a char. If I try to print this or use it to split the paths:

print("\(folder.delimiter)\n")
var components = folder.path.componentsSeparatedByString(String.init(folder.delimiter))
for component in components {
    print("\(component)\n")
}

it will print

46
INBOX.Sent

The closest I could find here was Converting a C char array to a String but this only seems to apply to char[] and not a single char.

So what am I missing?

Upvotes: 3

Views: 1047

Answers (1)

Martin R
Martin R

Reputation: 539685

The C type char is mapped to Swift as CChar, which is an alias for Int8. You can create a Swift string from a single C character with

let delim = String(UnicodeScalar(UInt8(bitPattern: folder.delimiter)))

This interprets the given char as a Unicode value in the range 0 ... 255 and converts it to a String.

Upvotes: 6

Related Questions