3

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?

9
  • The question you linked to doesn't have the [c] tag... Do you wonder why? Commented Aug 16, 2015 at 8:35
  • @Freenode-newbostonSebivor To be honest, I don't. :-/ Obviously I am blind for the obvious answer... Commented Aug 16, 2015 at 8:43
  • 1
    @MartinR This question is about interoperability between whichever ABI supports Swift, C, C++ and potentially a whole host of other programming languages... The question and answer for all of those languages is the same, so they should all be tagged too, right? Commented Aug 16, 2015 at 9:01
  • 2
    @Freenode-newbostonSebivor,MartinR I don't think we have to start this discussion again here. I removed the [c] tag as the issue targets Swift and not C. Thank you both for helping! Commented Aug 16, 2015 at 9:08
  • 1
    @emKay Perhaps [interop] might be a good tag to add? Commented Aug 16, 2015 at 9:09

1 Answer 1

6

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.

Sign up to request clarification or add additional context in comments.

1 Comment

That did the trick, thank you! To my defense, this is not very obvious, is it? ;-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.