查看: 925|回复: 0

[手机开发] Swift3.0基本运算符中的变化

发表于 2018-1-19 08:00:03
  1. Swift3.0中的基础运算符并无太大改动,只是移除了取余运算符的浮点数取余功能,取余运算符可以进行浮点运算本是Swift独有的一个特点,3.0版本的改变后,Swift中的"%"运算符功能将与Objective-C与C语言中的取余运算符保持一致。
  2. 除了Swift版的Cocoa框架中的API有了大范围的修改外,Swift的一些核心库也有了很大的改动 。 Swift3.0中的字符串类型String在方法API上更加简洁,其中变动较大的是与下标相关的方法,列举如下:
复制代码
  1. var string = "Hello-Swift"
  2. //获取某个下标后一个下标对应的字符 char="e"
  3. //swift2.2
  4. //var char = string[startIndex.successor()]
  5. //swift3.0
  6. var char = string[string.index(after: startIndex)]
  7. //获取某个下标前一个下标对应的字符 char2 = "t"
  8. //swift2.2
  9. //var char2 = string[endIndex.predecessor()]
  10. //swift3.0
  11. var char2 = string[string.index(before: string.endIndex)]
  12. //通过范围获取字符串中的一个子串 Hello
  13. //swift2.2
  14. //var subString = string[startIndex...startIndex.advancedBy(4)]
  15. //swift3.0
  16. var subString = string[startIndex...string.index(startIndex, offsetBy: 4)]
  17. //swift2.2
  18. //var subString2 = string[endIndex.advancedBy(-5)...endIndex.predecessor()]
  19. //swift3.0
  20. var subString2 = string[string.index(endIndex, offsetBy: -5)..<endIndex]
  21. //获取某个子串在父串中的范围
  22. //swift2.2
  23. //var range = string.rangeOfString("Hello")
  24. //swift3.0
  25. var range = string.range(of: "Hello")
  26. //追加字符串操作 此时string = "Hello-Swift! Hello-World"
  27. //swift2.2
  28. //string.appendContentsOf(" Hello-World")
  29. //swift3.0
  30. string.append(" Hello-World")
  31. //在指定位置插入一个字符 此时string = "Hello-Swift!~ Hello-World"
  32. //swift2.2
  33. //string.insert("~", atIndex: string.startIndex.advancedBy(12))
  34. //swift3.0
  35. string.insert("~", at: string.index(string.startIndex, offsetBy: 12))
  36. //在指定位置插入一组字符 此时string = "Hello-Swift!~~~~ Hello-World"
  37. //swift2.2
  38. //string.insertContentsOf(["~","~","~"], at: string.startIndex.advancedBy(12))
  39. //swift3.0
  40. string.insert(contentsOf: ["~","~","~"], at: string.index(string.startIndex, offsetBy: 12))
  41. //在指定范围替换一个字符串 此时string = "Hi-Swift!~~~~ Hello-World"
  42. //swift2.2
  43. //string.replaceRange(string.startIndex...string.startIndex.advancedBy(4), with: "Hi")
  44. //swift3.0
  45. string.replaceSubrange(string.startIndex...string.index(string.startIndex, offsetBy: 4), with: "Hi")
  46. //在指定位置删除一个字符 此时string = "Hi-Swift!~~~~ Hello-Worl"
  47. //swift2.2
  48. //string.removeAtIndex(string.endIndex.predecessor())
  49. //swift3.0
  50. string.remove(at: string.index(before:string.endIndex))
  51. //删除指定范围的字符 此时string = "Swift!~~~~ Hello-Worl"
  52. //swift2.2
  53. //string.removeRange(string.startIndex...string.startIndex.advancedBy(2))
  54. //swift3.0
  55. string.removeSubrange(string.startIndex...string.index(string.startIndex, offsetBy: 2))
  56. var string2 = "My name is Jaki"
  57. //全部转换为大写
  58. //swift2.2
  59. //string2 = string2.uppercaseString
  60. //swift3.0
  61. string2 = string2.uppercased()
  62. //全部转换为小写
  63. //swift2.2
  64. //string2 = string2.lowercaseString
  65. //swift3.0
  66. string2 = string2.lowercased()
复制代码


回复

使用道具 举报