diff --git a/answers/Task1.playground/Contents.swift b/answers/Task1.playground/Contents.swift new file mode 100644 index 0000000..e845d5c --- /dev/null +++ b/answers/Task1.playground/Contents.swift @@ -0,0 +1,30 @@ +import Foundation + +func printValue(_ n: Int) { + print("The value of n is \(String(n))") +} + +func countLength(_ n: String) { + print("The length of n is \(n.count)") +} + +func varProcessor(_ n: Any) { + let nInt = n as? Int ?? 0 // 如果n是整形数,nInt将会被赋予n的值 + let nString = n as? String ?? "" // 如果n是字符串,nString将会被赋予n的值 + switch n{ + case is Int: + // 如果n是整形,这里的代码将会被执行 + printValue(nInt) + case is String: + // 如果n是字符串,这里的代码将会被执行 + countLength(nString) + default: + print("An error occurred.") + } +} + +let test: [Any] = [0, 2, 0.2, "Hello"] + +for item in test { + varProcessor(item) +} diff --git a/answers/Task1.playground/contents.xcplayground b/answers/Task1.playground/contents.xcplayground new file mode 100644 index 0000000..1c968e7 --- /dev/null +++ b/answers/Task1.playground/contents.xcplayground @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/answers/Task1.playground/playground.xcworkspace/contents.xcworkspacedata b/answers/Task1.playground/playground.xcworkspace/contents.xcworkspacedata new file mode 100644 index 0000000..ca3329e --- /dev/null +++ b/answers/Task1.playground/playground.xcworkspace/contents.xcworkspacedata @@ -0,0 +1,7 @@ + + + + + diff --git a/answers/Task1.playground/playground.xcworkspace/xcuserdata/ostrichb.xcuserdatad/UserInterfaceState.xcuserstate b/answers/Task1.playground/playground.xcworkspace/xcuserdata/ostrichb.xcuserdatad/UserInterfaceState.xcuserstate new file mode 100644 index 0000000..7534f1c Binary files /dev/null and b/answers/Task1.playground/playground.xcworkspace/xcuserdata/ostrichb.xcuserdatad/UserInterfaceState.xcuserstate differ diff --git a/answers/Task1.playground/timeline.xctimeline b/answers/Task1.playground/timeline.xctimeline new file mode 100644 index 0000000..08fbdd1 --- /dev/null +++ b/answers/Task1.playground/timeline.xctimeline @@ -0,0 +1,10 @@ + + + + + + + diff --git a/answers/Task1.playground/xcuserdata/ostrichb.xcuserdatad/xcschemes/xcschememanagement.plist b/answers/Task1.playground/xcuserdata/ostrichb.xcuserdatad/xcschemes/xcschememanagement.plist new file mode 100644 index 0000000..7fdf092 --- /dev/null +++ b/answers/Task1.playground/xcuserdata/ostrichb.xcuserdatad/xcschemes/xcschememanagement.plist @@ -0,0 +1,16 @@ + + + + + SchemeUserState + + Task1 (Playground).xcscheme + + isShown + + orderHint + 0 + + + + diff --git a/answers/Task2.playground/Pages/Untitled Page.xcplaygroundpage/Contents.swift b/answers/Task2.playground/Pages/Untitled Page.xcplaygroundpage/Contents.swift new file mode 100644 index 0000000..523a635 --- /dev/null +++ b/answers/Task2.playground/Pages/Untitled Page.xcplaygroundpage/Contents.swift @@ -0,0 +1,22 @@ +import Foundation + +func varProcessor(_ n: Any, intProcess: (Int) -> Void, strProcess: (String) -> Void) { + let nInt = n as? Int ?? 0 // 如果n是整形数,nInt将会被赋予n的值 + let nString = n as? String ?? "" // 如果n是字符串,nString将会被赋予n的值 + switch n{ + case is Int: + // 如果n是整型,这里的代码将会被执行 + intProcess(nInt) + case is String: + // 如果n是字符串,这里的代码将会被执行 + strProcess(nString) + default: + print("An error occurred.") + } +} + +let test: [Any] = [0, 2, 0.2, "Hello"] + +for item in test { + varProcessor(item, intProcess: {print("The value of n is \(String($0))")}, strProcess: {print("The length of n is \($0.count)")}) +} diff --git a/answers/Task2.playground/Pages/Untitled Page.xcplaygroundpage/timeline.xctimeline b/answers/Task2.playground/Pages/Untitled Page.xcplaygroundpage/timeline.xctimeline new file mode 100644 index 0000000..b04035f --- /dev/null +++ b/answers/Task2.playground/Pages/Untitled Page.xcplaygroundpage/timeline.xctimeline @@ -0,0 +1,10 @@ + + + + + + + diff --git a/answers/Task2.playground/contents.xcplayground b/answers/Task2.playground/contents.xcplayground new file mode 100644 index 0000000..1c968e7 --- /dev/null +++ b/answers/Task2.playground/contents.xcplayground @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/answers/Task2.playground/playground.xcworkspace/contents.xcworkspacedata b/answers/Task2.playground/playground.xcworkspace/contents.xcworkspacedata new file mode 100644 index 0000000..ca3329e --- /dev/null +++ b/answers/Task2.playground/playground.xcworkspace/contents.xcworkspacedata @@ -0,0 +1,7 @@ + + + + + diff --git a/answers/Task2.playground/playground.xcworkspace/xcuserdata/ostrichb.xcuserdatad/UserInterfaceState.xcuserstate b/answers/Task2.playground/playground.xcworkspace/xcuserdata/ostrichb.xcuserdatad/UserInterfaceState.xcuserstate new file mode 100644 index 0000000..a4d3114 Binary files /dev/null and b/answers/Task2.playground/playground.xcworkspace/xcuserdata/ostrichb.xcuserdatad/UserInterfaceState.xcuserstate differ diff --git a/answers/Task2.playground/xcuserdata/ostrichb.xcuserdatad/xcschemes/xcschememanagement.plist b/answers/Task2.playground/xcuserdata/ostrichb.xcuserdatad/xcschemes/xcschememanagement.plist new file mode 100644 index 0000000..87e2dec --- /dev/null +++ b/answers/Task2.playground/xcuserdata/ostrichb.xcuserdatad/xcschemes/xcschememanagement.plist @@ -0,0 +1,16 @@ + + + + + SchemeUserState + + Task2 (Playground).xcscheme + + isShown + + orderHint + 0 + + + + diff --git a/answers/Task3.playground/Contents.swift b/answers/Task3.playground/Contents.swift new file mode 100644 index 0000000..ccb973f --- /dev/null +++ b/answers/Task3.playground/Contents.swift @@ -0,0 +1,21 @@ +import Foundation + +struct Entity { + let ID, name: String + func verify() -> Bool { + // 以下的内容将能够判断名为 ID 的字符串是否满足13位数字的情况,可以直接照搬 + let reFormula = try! NSRegularExpression(pattern: "^\\d{13}$", options: [NSRegularExpression.Options.anchorsMatchLines]) + let matchRange = NSRange(ID.startIndex.. + + + \ No newline at end of file diff --git a/answers/Task3.playground/playground.xcworkspace/contents.xcworkspacedata b/answers/Task3.playground/playground.xcworkspace/contents.xcworkspacedata new file mode 100644 index 0000000..ca3329e --- /dev/null +++ b/answers/Task3.playground/playground.xcworkspace/contents.xcworkspacedata @@ -0,0 +1,7 @@ + + + + + diff --git a/answers/Task3.playground/playground.xcworkspace/xcuserdata/ostrichb.xcuserdatad/UserInterfaceState.xcuserstate b/answers/Task3.playground/playground.xcworkspace/xcuserdata/ostrichb.xcuserdatad/UserInterfaceState.xcuserstate new file mode 100644 index 0000000..4d3b05a Binary files /dev/null and b/answers/Task3.playground/playground.xcworkspace/xcuserdata/ostrichb.xcuserdatad/UserInterfaceState.xcuserstate differ diff --git a/answers/Task3.playground/timeline.xctimeline b/answers/Task3.playground/timeline.xctimeline new file mode 100644 index 0000000..f2be0e1 --- /dev/null +++ b/answers/Task3.playground/timeline.xctimeline @@ -0,0 +1,26 @@ + + + + + + + + + + + + + + + diff --git a/answers/Task4.playground/Contents.swift b/answers/Task4.playground/Contents.swift new file mode 100644 index 0000000..c526e75 --- /dev/null +++ b/answers/Task4.playground/Contents.swift @@ -0,0 +1,91 @@ +import Foundation + +class Entity { + var ID: String = "" + var name: String = "" + func verify() -> Bool { + // 以下的内容将能够判断名为 ID 的字符串是否满足13位数字的情况,可以直接照搬 + let reFormula = try! NSRegularExpression(pattern: "^\\d{13}$", options: [NSRegularExpression.Options.anchorsMatchLines]) + let matchRange = NSRange(ID.startIndex.. Bool { + // 以下的内容将能够判断名为 ID 的字符串是否满足13位数字,以20xx开头,第5位为1-3的情况,可以直接照搬 + let reFormula = try! NSRegularExpression(pattern: "^20\\d{2}[1-3]\\d{8}$", options: [NSRegularExpression.Options.anchorsMatchLines]) + let matchRange = NSRange(ID.startIndex.. Bool { + // 以下的内容将能够判断名为 ID 的字符串是否满足13位数字,开头为0000的情况,可以直接照搬 + let reFormula = try! NSRegularExpression(pattern: "^0000\\d{9}$", options: [NSRegularExpression.Options.anchorsMatchLines]) + let matchRange = NSRange(ID.startIndex.. + + + \ No newline at end of file diff --git a/answers/Task4.playground/playground.xcworkspace/contents.xcworkspacedata b/answers/Task4.playground/playground.xcworkspace/contents.xcworkspacedata new file mode 100644 index 0000000..ca3329e --- /dev/null +++ b/answers/Task4.playground/playground.xcworkspace/contents.xcworkspacedata @@ -0,0 +1,7 @@ + + + + + diff --git a/answers/Task4.playground/playground.xcworkspace/xcuserdata/ostrichb.xcuserdatad/UserInterfaceState.xcuserstate b/answers/Task4.playground/playground.xcworkspace/xcuserdata/ostrichb.xcuserdatad/UserInterfaceState.xcuserstate new file mode 100644 index 0000000..8151fce Binary files /dev/null and b/answers/Task4.playground/playground.xcworkspace/xcuserdata/ostrichb.xcuserdatad/UserInterfaceState.xcuserstate differ diff --git a/answers/Task4.playground/timeline.xctimeline b/answers/Task4.playground/timeline.xctimeline new file mode 100644 index 0000000..d5b53c8 --- /dev/null +++ b/answers/Task4.playground/timeline.xctimeline @@ -0,0 +1,26 @@ + + + + + + + + + + + + + + + diff --git a/answers/Task4.playground/xcuserdata/ostrichb.xcuserdatad/xcschemes/xcschememanagement.plist b/answers/Task4.playground/xcuserdata/ostrichb.xcuserdatad/xcschemes/xcschememanagement.plist new file mode 100644 index 0000000..4098089 --- /dev/null +++ b/answers/Task4.playground/xcuserdata/ostrichb.xcuserdatad/xcschemes/xcschememanagement.plist @@ -0,0 +1,16 @@ + + + + + SchemeUserState + + Task4 (Playground).xcscheme + + isShown + + orderHint + 0 + + + + diff --git a/answers/Task5.playground/Contents.swift b/answers/Task5.playground/Contents.swift new file mode 100644 index 0000000..3b94478 --- /dev/null +++ b/answers/Task5.playground/Contents.swift @@ -0,0 +1,105 @@ +import Foundation + +enum InsufficientEnergy:Error { + case crazyThursdayVMe50 +} + +class Entity { + let ID, name: String + init?(id ID: String, name: String) { + self.ID = ID + self.name = name + let reFormula = try! NSRegularExpression(pattern: "^\\d{13}$", options: [NSRegularExpression.Options.anchorsMatchLines]) + let matchRange = NSRange(ID.startIndex.. Void { + if Int.random(in: 1...7) == 7 { + throw InsufficientEnergy.crazyThursdayVMe50 + } + } + +} + +class Student: People { + override init?(id ID: String, name: String){ + super.init(id: ID, name: name) + let reFormula = try! NSRegularExpression(pattern: "^20\\d{2}[1-3]\\d{8}$", options: [NSRegularExpression.Options.anchorsMatchLines]) + let matchRange = NSRange(ID.startIndex.. + + + \ No newline at end of file diff --git a/answers/Task5.playground/playground.xcworkspace/contents.xcworkspacedata b/answers/Task5.playground/playground.xcworkspace/contents.xcworkspacedata new file mode 100644 index 0000000..ca3329e --- /dev/null +++ b/answers/Task5.playground/playground.xcworkspace/contents.xcworkspacedata @@ -0,0 +1,7 @@ + + + + + diff --git a/answers/Task5.playground/playground.xcworkspace/xcuserdata/ostrichb.xcuserdatad/UserInterfaceState.xcuserstate b/answers/Task5.playground/playground.xcworkspace/xcuserdata/ostrichb.xcuserdatad/UserInterfaceState.xcuserstate new file mode 100644 index 0000000..056e368 Binary files /dev/null and b/answers/Task5.playground/playground.xcworkspace/xcuserdata/ostrichb.xcuserdatad/UserInterfaceState.xcuserstate differ diff --git a/answers/Task5.playground/timeline.xctimeline b/answers/Task5.playground/timeline.xctimeline new file mode 100644 index 0000000..fb5479b --- /dev/null +++ b/answers/Task5.playground/timeline.xctimeline @@ -0,0 +1,30 @@ + + + + + + + + + + + + + + + + +