Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Strings and characters in swift

1,567 views

Published on

FUKUOKA Engineers Day 2018
https://engineers-day.connpass.com/event/74240/

Published in: Software
  • Be the first to comment

  • Be the first to like this

Strings and characters in swift

  1. 1. String String String
  2. 2. String String String
  3. 3. String Substring StringProtocol String Unicode
  4. 4. String String String
  5. 5. String NSString
  6. 6. String String Character Character 
 ==
  7. 7. String UnicodeScalarView UTF16View UTF8View
  8. 8. let 

  9. 9. NSString 

  10. 10. NSString NSString NSString String StringProtocol Substring
  11. 11. String String String
  12. 12. String String Character String.characters String.CharacterView
  13. 13. String Unicode.Scalar Character String
  14. 14. Unicode.UTF8 CodeUnit UInt8 Unicode.UTF16 CodeUnit UInt16 Unicode.UTF32 CodeUnit UInt32
  15. 15. String
  16. 16. 🐶 "Dog‼🐶".count = 5 Character D U+0044 o U+006F g U+0067 ‼ U+203C 🐶 U+1F436 Unicode Scalar (UTF-32) D U+0044 o U+006F g U+0067 ‼ U+203C 🐶 U+1F436 0 1 2 3 4 UTF-16 0x44 0x6F 0x67 0x203C 0xD83D 0xDC36 0 1 2 3 4 5 UTF-8 0x44 0x6F 0x67 0xE2 0x80 0xBC 0xF0 0x9F 0x90 0xB6 0 1 2 3 4 5 6 7 8 9
  17. 17. ⃣ " ⃣ !⁇".count = 3 Character ⃣ U+72AC U+20E3 U+0021 ⁇ U+2047 Unicode Scalar (UTF-32) U+72AC ⃣ U+20E3 ! U+0021 ⁇ U+2047 0 1 2 3 UTF-16 0x72AC 0x20E3 0x21 0x2047 0 1 2 3 UTF-8 0xE7 0x8A 0xAC 0xE2 0x83 0xA3 0x21 0xE2 0x81 0x87 0 1 2 3 4 5 6 7 8 9
  18. 18. String String String
  19. 19. String NSString
  20. 20. String NSString
  21. 21. NSString NSString NSMutableString NSString StringProtocol NSUInteger NSRange String.Index Range
  22. 22. StringProtocol NSString import Foundation // StringProtocol (NSString API) let str = "Dog‼🐶" let range = str.range(of: "🐶")! print(range) // Index(_compoundOffset: 16, _cache: Swift.String.Index._Cache.utf16)..<Index(_compoundOffset: 24, _cache: Swift.String.Index._Cache.utf16) print(str[..<range.lowerBound]) // Dog‼ // NSString let nsStr = str as NSString let nsRange = nsStr.range(of: "🐶") print(nsRange) // {4, 2} print(nsStr.substring(to: nsRange.location)) // Dog‼ // Range <-> NSRange print(range == Range(nsRange, in: str)) // true print(NSRange(range, in: str) == nsRange) // true
  23. 23. String NSString
  24. 24. " "👨" + [ZWJ] + "👩" + [ZWJ] + "👧" + [ZWJ] + "👦" "👨" + [ZWJ] + "👩" + [ZWJ] + "👧" = "'"
  25. 25. String NSString
  26. 26. == != String Character
  27. 27. "u{00E9}" == "u{0065}u{0301}" // "é" == "e" + "́" "u{2460}" == "u{0031}" // " " == "1"
  28. 28. ==
  29. 29. "e" == "e" lhs rhs Character e U+0065 e U+0065 UnicodeScalar (UTF-32) e U+0065 e U+0065 D
 (NFD) U+0065 U+0065 Swift == true NSString
 isEqual: YES
  30. 30. "é" == "e" + "́" lhs rhs Character é U+00E9 é U+0065 U+0301 UnicodeScalar (UTF-32) é U+00E9 e U+0065 ́ U+0301 D
 (NFD) e U+0065 ́ U+0301 e U+0065 ́ U+0301 Swift == true NSString
 isEqual: NO
  31. 31. " " == " " + " " lhs rhs Character U+304C U+304B U+0399 UnicodeScalar (UTF-32) U+304C U+304B U+0399 D
 (NFD) U+304B U+0399 U+304B U+0399 Swift == true NSString
 isEqual: NO
  32. 32. "é̲" == "e" + "́" + "_" lhs rhs Character é̲ U+00E9 U+0332 é̲ U+0065 U+0301 U+0332 UnicodeScalar (UTF-32) é U+00E9 _ U+0332 e U+0065 ́ U+0301 _ U+0332 D
 (NFD) e U+0065 _ U+0332 ́ U+0301 e U+0065 _ U+0332 ́ U+0301 Swift == true NSString
 isEqual: NO
  33. 33. " " == " " lhs rhs Character CJK U+FA10 CJK U+585A UnicodeScalar (UTF-32) U+FA10 U+585A D
 (NFD) U+585A U+585A Swift == true NSString
 isEqual: NO
  34. 34. " " == " " lhs rhs Character CJK U+FA11 CJK U+5D0E UnicodeScalar (UTF-32) U+FA11 U+5D0E D
 (NFD) U+FA11 U+5D0E Swift == false NSString
 isEqual: NO
  35. 35. " " == " " lhs rhs Character CJK U+585A U+585A U+E0100 UnicodeScalar (UTF-32) U+585A U+585A U+E0100 D
 (NFD) U+585A U+585A U+E0100 Swift == false NSString
 isEqual: NO
  36. 36. String NSString
  37. 37. String String NSString 
 


×