Swift

相关术语:

  • UIKit is the iOS user interface toolkit.
  • IB is Interface Builder.
  • CA is Core Animation. Like CALayer is a Core Animation data type responsible for managing the way your view looks. Core Animation handles animation.
  • CG is Apple’s Core Graphics framework. Like CGColor. Core Graphics handles drawing.
  • KVO is key-value observing.
  • NS is Next Step. Steve Jobs did lots.
  • VFL is a technique called Auto Layout Visual Format Language.
  • GCD is Grand Central Dispatch.
  • FIFO is First In, First Out.
  • MK is Mapkit.
  • NS came from the NeXTSTEP libraries Foundation and AppKit(those names are still used by Apple’s Cocoa frameworks).
  • UN is UserNotification.
  • CL is CoreLocation.
  • MC is Multipeer Connectivity.
  • CM is Core Motion.
  • LA is the Local Authentication framework.
  • HSB is Hue, Saturation and Brightness. Using this method of creating colors you specify values between 0 and 1 to control how saturated a color is (from 0 = gray to 1 = pure color) and brightness (from 0 = black to 1 = maximum brightness), and 0 to 1 for hue. “Hue” is a value from 0 to 1 also, but it represents a position on a color wheel, like using a color picker on your Mac. Hues 0 and 1 both represent red, with all other colors lying in between.
  • CK is CloudKit.
  • XCTest may be XCode Test.

String

A string is a collection of character.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
let pizzaJoint = "café pesto"

// 获取首字母的String.index索引
let firstCharacterIndex = pizzaJoint.startIndex // 得到的是String.index类型
// 获取首字母偏移3的String.index索引
let fourthCharacterIndex = pizzaJoint.index(firstCharacterIndex, offsetBy: 3)
// 获取首字母偏移3的String.index索引所在位置的Character
let fourthCharacter = pizzaJoint[fourthCharacterIndex] // "é"

if let firstSpace = pizzaJoint.index(of: " ") {
let secondWordIndex = pizzaJoint.index(firstSpace, offsetBy: 1)
// 使用 Range of String.Index 来取得字符串的片段
let secondWord = pizzaJoint[secondWordIndex ..< pizzaJoint.endIndex] // "pesto"
// 没有secondWordIndex 直接..< pizzaJoint.endIndex 也行,一般默认都是从第一个开始
}

// 使用Array(String)将字符串Array化
let characterArray = Array(pizzaJoint) // ["c", "a", "f", "é", " ", "p", "e", "s", "t", "o"]

var s = pizzaJoint
// 使用String的insert(contentsOf: String)方法插入字符串
s.insert(contentsOf: "foo", at: s.firstIndex(of: " ")!) // "caféfoo pesto"


// func hasPrefix(String) -> Bool
// 查询前缀的方法,此处省略代码

// func hasSuffix(String) -> Bool
// 查询后缀的方法,此处省略代码

// func replaceSubrange(Range<String.Index>, with: Collection of Character)
s.replaceSubrange(..<s.endIndex, with: "new contents") // "new contents"

NSAttributedString

Attributed strings are made up of two parts: a plain Swift string, plus a dictionary containing a series of attributes that describe how various segments of the string are formatted.

NSAttributedString可以针对一串字符串,片段化地设置其的属性,而Label却只能整个设置。
我们在使用UILabel, UITextField, UITextView, UIButton, UINavigationBar等等支持text属性的情况下,更建议使用attributedText。

1
2
NSAttributedString(string: String, attributes: [NSAttributedString.Key: Any]?)

The examples are in codes below.

Read More

SwiftUI

String

components(seperatedBy:)

1
2
3
4
let input = "a b c"
let letters = input.components(separatedBy: " ")
print(letters)
// ["a", "b", "c"]

trimmingCharacters(in:)

we can ask Swift to trim all whitespace at the start and end of a string like this:

1
let trimmed = someString.trimmingCharacters(in: .whitespacesAndNewlines)

读取txt文件中的内容,并转化成String

1
2
3
4
5
6
7
8
9
10
11
// 例如读取项目中的start.txt文本文件中的内容,
// 此外,该文本每行一个单词,随机读取一个单词:
if let contentOfTxt = Bundle.main.url(forResource: "start", withExtension: "txt") {
if let contentOfString = try? String(contentsOf: contentOfTxt) {
let allWords = contentOfString.components(separatedBy: "\n")
var randomWord = allWords.randomElement() ?? ""
// 如果在func中,需要空return,并在下面if之外fatalError()
// return
}
}
// fatalError()

NSRange && rangeOfMisspelledWord && NSNotFound

1
2
3
4
5
6
7
// rangeOfMisspelledWord:
// Initiates a search of a range of a string for a misspelled word.
func rangeOfMisspelledWord(in stringToCheck: String,
range: NSRange,
startingAt startingOffset: Int,
wrap wrapFlag: Bool,
language: String) -> NSRange

Read More