0

Reading this answer about detecting emojis in Swift 5 using unicodeScalars I'm wondering how I can access this in a Objective-C project. I know that Swift-Libs can be accessed in Objective-C, but is there an easier way to access just unicodeScalars and doing something similar to the linked answer?

Keeper
  • 920
  • 1
  • 10
  • 16

1 Answers1

0

following the answer it can be changed a little bit to make it work with Bridging back to ObjC..

@objc extension NSString {

    var containsEmoji : Bool {
        let uni = self as String
        for scalar in uni.unicodeScalars {
            switch scalar.value {
            case 0x1F600...0x1F64F, // Emoticons
                 0x1F300...0x1F5FF, // Misc Symbols and Pictographs
                 0x1F680...0x1F6FF, // Transport and Map
                 0x2600...0x26FF,   // Misc symbols
                 0x2700...0x27BF,   // Dingbats
                 0xFE00...0xFE0F,   // Variation Selectors
                 0x1F900...0x1F9FF, // Supplemental Symbols and Pictographs
                 0x1F1E6...0x1F1FF: // Flags
                return true
            default:
                continue
            }
        }
        return false
    }

}

let's test..

NSString *normal = @"hello world";
NSString *special = @" badewelt";
if ([special containsEmoji]) {
    NSLog(@"emoji inside %@",special);
} else {
    NSLog(@"no emoji inside %@",special);
}
// or with dot syntax
if (normal.containsEmoji) {
    NSLog(@"emoji inside %@",normal);
} else {
    NSLog(@"no emoji inside %@",normal);
}

output

emoji inside  badewelt
no emoji inside hello world
Ol Sen
  • 3,163
  • 2
  • 21
  • 30