Thank you very much. :-) I had been trying to figure out how to use 
NSCharacterSet, but I didn’t know the bit about converting to UTF-16 string 
first.    

— 

Charles

On April 1, 2015 at 9:52:47 PM, Charles Srstka ([email protected]) wrote:

On Apr 1, 2015, at 8:14 PM, Charles Jenkins <[email protected]> wrote:  
>  
> Given this code:  
>  
> let someCharacter = str[str.endIndex.predecessor()]  
>  
> How can I determine if someCharacter is whitespace?  

import Foundation  

func isChar(char: Character, inSet set: NSCharacterSet) -> Bool {  
// this function is from an answer on StackOverflow:  
// 
http://stackoverflow.com/questions/27697508/nscharacterset-characterismember-with-swifts-character-type
  
var found = true  
for ch in String(char).utf16 {  
if !set.characterIsMember(ch) { found = false }  
}  
return found  
}  

let str = "foo "  
let chr = str[str.endIndex.predecessor()]  

let isWhitespace = isChar(chr, inSet: 
NSCharacterSet.whitespaceAndNewlineCharacterSet()) // true  

Charles
_______________________________________________

Cocoa-dev mailing list ([email protected])

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [email protected]

Reply via email to