├── .gitignore ├── PDF ├── Lecture 1 Slides.pdf ├── Lecture 10 Slides.pdf ├── Lecture 11 Slides.pdf ├── Lecture 12 Slides.pdf ├── Lecture 13 Slides.pdf ├── Lecture 14 Slides.pdf ├── Lecture 15 Slides.pdf ├── Lecture 16 Slides.pdf ├── Lecture 17 Slides.pdf ├── Lecture 2 Slides.pdf ├── Lecture 3 Slides.pdf ├── Lecture 4 Slides.pdf ├── Lecture 5 Slides.pdf ├── Lecture 6 Slides.pdf ├── Lecture 7 Slides.pdf ├── Lecture 8 Slides.pdf ├── Lecture 9 Slides.pdf ├── Programming Project 1_ Concentration.pdf ├── Programming Project 2_ Set.pdf ├── Programming Project 3_ Graphical Set.pdf ├── Programming Project 4_ Animated Set.pdf ├── Programming Project 5_ Image Gallery.pdf ├── Programming Project 6_ Persistent Image Gallery.pdf ├── Reading 1_ Intro to Swift.pdf ├── Reading 2_ Intro to Swift.pdf └── Reading 3_ Finishing Off Swift.pdf ├── README.md ├── downloader.swift ├── en ├── 1. Introduction to iOS 11 Xcode 9 and Swift 4.srt ├── 10. Multithreading and Autolayout.srt ├── 11. Drag and Drop, Table View, and Collection View.srt ├── 12. Drag and Drop, Table View, Collection View, and Text Field.srt ├── 13. Persistence and Documents.srt ├── 14. Persistence and Documents Demo.srt ├── 15. Alerts, Notifications, Application Lifecycle.srt ├── 16. More Segues.srt ├── 2. MVC.srt ├── 3. Swift Programming Language.srt ├── 4. More Swift.srt ├── 5. Drawing.srt ├── 6. Multitouch.srt ├── 7. Multiple MVCs, Timer, and Animation.srt ├── 8. Animation.srt ├── 9. View Controller Lifecycle and Scroll View.srt ├── Friday Session 1. Debugging and Xcode Tips and Tricks.srt ├── Friday Session 2. Github and Source Control Workflow.srt └── Friday Session 3. Instruments.srt └── zh ├── 1. Introduction to iOS 11 Xcode 9 and Swift 4.srt ├── 10. Multithreading and Autolayout.srt ├── 11. Drag and Drop, Table View, and Collection View.srt ├── 12. Drag and Drop, Table View, Collection View, and Text Field.srt ├── 13. Persistence and Documents.srt ├── 14. Persistence and Documents Demo.srt ├── 15. Alerts, Notifications, Application Lifecycle.srt ├── 16. More Segues.srt ├── 2. MVC.srt ├── 3. Swift Programming Language.srt ├── 4. More Swift.srt ├── 5. Drawing.srt ├── 6. Multitouch.srt ├── 7. Multiple MVCs, Timer, and Animation.srt ├── 8. Animation.srt └── 9. View Controller Lifecycle and Scroll View.srt /.gitignore: -------------------------------------------------------------------------------- 1 | *.mp4 2 | *.m4v 3 | -------------------------------------------------------------------------------- /PDF/Lecture 1 Slides.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JaminZhou/Developing-iOS-11-Apps-with-Swift/1985da92c0b788aa0d4de88ba71933a0920ace0d/PDF/Lecture 1 Slides.pdf -------------------------------------------------------------------------------- /PDF/Lecture 10 Slides.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JaminZhou/Developing-iOS-11-Apps-with-Swift/1985da92c0b788aa0d4de88ba71933a0920ace0d/PDF/Lecture 10 Slides.pdf -------------------------------------------------------------------------------- /PDF/Lecture 11 Slides.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JaminZhou/Developing-iOS-11-Apps-with-Swift/1985da92c0b788aa0d4de88ba71933a0920ace0d/PDF/Lecture 11 Slides.pdf -------------------------------------------------------------------------------- /PDF/Lecture 12 Slides.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JaminZhou/Developing-iOS-11-Apps-with-Swift/1985da92c0b788aa0d4de88ba71933a0920ace0d/PDF/Lecture 12 Slides.pdf -------------------------------------------------------------------------------- /PDF/Lecture 13 Slides.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JaminZhou/Developing-iOS-11-Apps-with-Swift/1985da92c0b788aa0d4de88ba71933a0920ace0d/PDF/Lecture 13 Slides.pdf -------------------------------------------------------------------------------- /PDF/Lecture 14 Slides.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JaminZhou/Developing-iOS-11-Apps-with-Swift/1985da92c0b788aa0d4de88ba71933a0920ace0d/PDF/Lecture 14 Slides.pdf -------------------------------------------------------------------------------- /PDF/Lecture 15 Slides.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JaminZhou/Developing-iOS-11-Apps-with-Swift/1985da92c0b788aa0d4de88ba71933a0920ace0d/PDF/Lecture 15 Slides.pdf -------------------------------------------------------------------------------- /PDF/Lecture 16 Slides.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JaminZhou/Developing-iOS-11-Apps-with-Swift/1985da92c0b788aa0d4de88ba71933a0920ace0d/PDF/Lecture 16 Slides.pdf -------------------------------------------------------------------------------- /PDF/Lecture 17 Slides.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JaminZhou/Developing-iOS-11-Apps-with-Swift/1985da92c0b788aa0d4de88ba71933a0920ace0d/PDF/Lecture 17 Slides.pdf -------------------------------------------------------------------------------- /PDF/Lecture 2 Slides.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JaminZhou/Developing-iOS-11-Apps-with-Swift/1985da92c0b788aa0d4de88ba71933a0920ace0d/PDF/Lecture 2 Slides.pdf -------------------------------------------------------------------------------- /PDF/Lecture 3 Slides.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JaminZhou/Developing-iOS-11-Apps-with-Swift/1985da92c0b788aa0d4de88ba71933a0920ace0d/PDF/Lecture 3 Slides.pdf -------------------------------------------------------------------------------- /PDF/Lecture 4 Slides.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JaminZhou/Developing-iOS-11-Apps-with-Swift/1985da92c0b788aa0d4de88ba71933a0920ace0d/PDF/Lecture 4 Slides.pdf -------------------------------------------------------------------------------- /PDF/Lecture 5 Slides.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JaminZhou/Developing-iOS-11-Apps-with-Swift/1985da92c0b788aa0d4de88ba71933a0920ace0d/PDF/Lecture 5 Slides.pdf -------------------------------------------------------------------------------- /PDF/Lecture 6 Slides.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JaminZhou/Developing-iOS-11-Apps-with-Swift/1985da92c0b788aa0d4de88ba71933a0920ace0d/PDF/Lecture 6 Slides.pdf -------------------------------------------------------------------------------- /PDF/Lecture 7 Slides.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JaminZhou/Developing-iOS-11-Apps-with-Swift/1985da92c0b788aa0d4de88ba71933a0920ace0d/PDF/Lecture 7 Slides.pdf -------------------------------------------------------------------------------- /PDF/Lecture 8 Slides.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JaminZhou/Developing-iOS-11-Apps-with-Swift/1985da92c0b788aa0d4de88ba71933a0920ace0d/PDF/Lecture 8 Slides.pdf -------------------------------------------------------------------------------- /PDF/Lecture 9 Slides.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JaminZhou/Developing-iOS-11-Apps-with-Swift/1985da92c0b788aa0d4de88ba71933a0920ace0d/PDF/Lecture 9 Slides.pdf -------------------------------------------------------------------------------- /PDF/Programming Project 1_ Concentration.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JaminZhou/Developing-iOS-11-Apps-with-Swift/1985da92c0b788aa0d4de88ba71933a0920ace0d/PDF/Programming Project 1_ Concentration.pdf -------------------------------------------------------------------------------- /PDF/Programming Project 2_ Set.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JaminZhou/Developing-iOS-11-Apps-with-Swift/1985da92c0b788aa0d4de88ba71933a0920ace0d/PDF/Programming Project 2_ Set.pdf -------------------------------------------------------------------------------- /PDF/Programming Project 3_ Graphical Set.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JaminZhou/Developing-iOS-11-Apps-with-Swift/1985da92c0b788aa0d4de88ba71933a0920ace0d/PDF/Programming Project 3_ Graphical Set.pdf -------------------------------------------------------------------------------- /PDF/Programming Project 4_ Animated Set.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JaminZhou/Developing-iOS-11-Apps-with-Swift/1985da92c0b788aa0d4de88ba71933a0920ace0d/PDF/Programming Project 4_ Animated Set.pdf -------------------------------------------------------------------------------- /PDF/Programming Project 5_ Image Gallery.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JaminZhou/Developing-iOS-11-Apps-with-Swift/1985da92c0b788aa0d4de88ba71933a0920ace0d/PDF/Programming Project 5_ Image Gallery.pdf -------------------------------------------------------------------------------- /PDF/Programming Project 6_ Persistent Image Gallery.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JaminZhou/Developing-iOS-11-Apps-with-Swift/1985da92c0b788aa0d4de88ba71933a0920ace0d/PDF/Programming Project 6_ Persistent Image Gallery.pdf -------------------------------------------------------------------------------- /PDF/Reading 1_ Intro to Swift.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JaminZhou/Developing-iOS-11-Apps-with-Swift/1985da92c0b788aa0d4de88ba71933a0920ace0d/PDF/Reading 1_ Intro to Swift.pdf -------------------------------------------------------------------------------- /PDF/Reading 2_ Intro to Swift.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JaminZhou/Developing-iOS-11-Apps-with-Swift/1985da92c0b788aa0d4de88ba71933a0920ace0d/PDF/Reading 2_ Intro to Swift.pdf -------------------------------------------------------------------------------- /PDF/Reading 3_ Finishing Off Swift.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JaminZhou/Developing-iOS-11-Apps-with-Swift/1985da92c0b788aa0d4de88ba71933a0920ace0d/PDF/Reading 3_ Finishing Off Swift.pdf -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Developing iOS 11 Apps with Swift 2 | 3 | iTunes U 课程地址:[iTunes U/Developing iOS 11 Apps Swift](https://itunes.apple.com/cn/course/developing-ios-11-apps-with-swift/id1309275316) 4 | 5 | CS193P 课程地址:[CS193P iPhone Application Development](http://web.stanford.edu/class/cs193p/cgi-bin/drupal/) 6 | 7 | ### 下载 8 | ```swift downloader.swift``` 下载录像 9 | 10 | [ccextractor](https://www.ccextractor.org/) 提取字幕 11 | 12 | ###  其它 13 | 之前的翻译 [Developing iOS 10 Apps with Swift](https://github.com/JaminZhou/Developing-iOS-10-Apps-with-Swift) 14 | 15 | ### 说明 16 | 此版本字幕翻译为机翻+粗校 17 | 18 | ### 版权说明 19 | 20 | The original work by Stanford University is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License. 21 | 22 | Creative Commons License
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 United States License. 23 | -------------------------------------------------------------------------------- /downloader.swift: -------------------------------------------------------------------------------- 1 | import Foundation 2 | import SystemConfiguration 3 | 4 | class Reachability { 5 | class func isConnectedToNetwork() -> Bool { 6 | guard let flags = getFlags() else {return false} 7 | let isReachable = flags.contains(.reachable) 8 | let needsConnection = flags.contains(.connectionRequired) 9 | return (isReachable && !needsConnection) 10 | } 11 | 12 | class func getFlags() -> SCNetworkReachabilityFlags? { 13 | guard let reachability = ipv4Reachability() ?? ipv6Reachability() else {return nil} 14 | var flags = SCNetworkReachabilityFlags() 15 | if !SCNetworkReachabilityGetFlags(reachability, &flags) {return nil} 16 | return flags 17 | } 18 | 19 | class func ipv6Reachability() -> SCNetworkReachability? { 20 | var zeroAddress = sockaddr_in6() 21 | zeroAddress.sin6_len = UInt8(MemoryLayout.size) 22 | zeroAddress.sin6_family = sa_family_t(AF_INET6) 23 | 24 | return withUnsafePointer(to: &zeroAddress, { 25 | $0.withMemoryRebound(to: sockaddr.self, capacity: 1) { 26 | SCNetworkReachabilityCreateWithAddress(nil, $0) 27 | } 28 | }) 29 | } 30 | 31 | class func ipv4Reachability() -> SCNetworkReachability? { 32 | var zeroAddress = sockaddr_in() 33 | zeroAddress.sin_len = UInt8(MemoryLayout.size) 34 | zeroAddress.sin_family = sa_family_t(AF_INET) 35 | 36 | return withUnsafePointer(to: &zeroAddress, { 37 | $0.withMemoryRebound(to: sockaddr.self, capacity: 1) { 38 | SCNetworkReachabilityCreateWithAddress(nil, $0) 39 | } 40 | }) 41 | } 42 | } 43 | 44 | class DownloadSessionManager : NSObject, URLSessionDownloadDelegate { 45 | 46 | static let shared = DownloadSessionManager() 47 | var filePath : String? 48 | var url: URL? 49 | var resumeData: Data? 50 | 51 | let semaphore = DispatchSemaphore.init(value: 0) 52 | var session : URLSession! 53 | 54 | override init() { 55 | super.init() 56 | self.resetSession() 57 | } 58 | 59 | func resetSession() { 60 | self.session = URLSession(configuration: URLSessionConfiguration.default, delegate: self, delegateQueue: nil) 61 | } 62 | 63 | func downloadFile(fromURL url: URL, toPath path: String) { 64 | self.filePath = path 65 | self.url = url 66 | self.resumeData = nil 67 | taskStartedAt = Date() 68 | let task = session.downloadTask(with: url) 69 | task.resume() 70 | semaphore.wait() 71 | } 72 | 73 | func resumeDownload() { 74 | self.resetSession() 75 | 76 | if let resumeData = self.resumeData { 77 | print("resuming file download...") 78 | let task = session.downloadTask(withResumeData: resumeData) 79 | task.resume() 80 | self.resumeData = nil 81 | semaphore.wait() 82 | } else { 83 | print("retrying file download...") 84 | self.downloadFile(fromURL: self.url!, toPath: self.filePath!) 85 | } 86 | } 87 | 88 | func show(progress: Int, barWidth: Int, speedInK: Int) { 89 | print("\r[", terminator: "") 90 | let pos = Int(Double(barWidth*progress)/100.0) 91 | for i in 0...barWidth { 92 | switch(i) { 93 | case _ where i < pos: 94 | print("🁢", terminator:"") 95 | break 96 | case pos: 97 | print("🁢", terminator:"") 98 | break 99 | default: 100 | print(" ", terminator:"") 101 | break 102 | } 103 | } 104 | 105 | print("] \(progress)% \(speedInK)KB/s", terminator:"") 106 | fflush(__stdoutp) 107 | } 108 | 109 | var taskStartedAt : Date? 110 | //MARK : URLSessionDownloadDelegate 111 | func urlSession(_: URLSession, 112 | downloadTask: URLSessionDownloadTask, 113 | didWriteData bytesWritten: Int64, 114 | totalBytesWritten: Int64, 115 | totalBytesExpectedToWrite: Int64) { 116 | let now = Date() 117 | let timeDownloaded = now.timeIntervalSince(taskStartedAt!) 118 | let kbs = Int( floor( Float(totalBytesWritten) / 1024.0 / Float(timeDownloaded) ) ) 119 | show(progress: Int(Double(totalBytesWritten)/Double(totalBytesExpectedToWrite)*100.0), barWidth: 70, speedInK: kbs) 120 | } 121 | 122 | func urlSession(_: URLSession, downloadTask: URLSessionDownloadTask, didFinishDownloadingTo location: URL) { 123 | defer { 124 | semaphore.signal() 125 | } 126 | 127 | print("") 128 | 129 | guard let _ = self.filePath else { 130 | print("No destination path to copy the downloaded file at \(location)") 131 | return 132 | } 133 | 134 | print("moving \(location) to \(self.filePath!)") 135 | 136 | do { 137 | try FileManager.default.moveItem(at: location, to: URL.init(fileURLWithPath: "\(filePath!)")) 138 | } 139 | 140 | catch let error { 141 | print("Ooops! Something went wrong: \(error)") 142 | } 143 | } 144 | 145 | func urlSession(_: URLSession, task: URLSessionTask, didCompleteWithError error: Error?) { 146 | guard let error = error else {return} 147 | 148 | defer { 149 | defer { 150 | semaphore.signal() 151 | } 152 | 153 | if !Reachability.isConnectedToNetwork() { 154 | print("Waiting for connection to be restored") 155 | repeat { 156 | sleep(1) 157 | } while !Reachability.isConnectedToNetwork() 158 | } 159 | 160 | self.resumeDownload() 161 | } 162 | 163 | print("") 164 | 165 | print("Ooops! Something went wrong: \(error.localizedDescription)") 166 | 167 | guard let resumeData = (error as NSError).userInfo[NSURLSessionDownloadTaskResumeData] as? Data else { 168 | return 169 | } 170 | 171 | self.resumeData = resumeData 172 | } 173 | } 174 | 175 | class iTunesUController { 176 | 177 | class func getVideoResourceURLs(fromHTML: String) -> ([String]) { 178 | var list = [String]() 179 | list.append(contentsOf: getResourceURLs(fromHTML: fromHTML, fileExtension: "m4v")) 180 | list.append(contentsOf: getResourceURLs(fromHTML: fromHTML, fileExtension: "mp4")) 181 | return list 182 | } 183 | 184 | class func getResourceURLs(fromHTML: String, fileExtension: String) -> ([String]) { 185 | let pat = "\\b.*(https://.*\\." + fileExtension + ")\\b" 186 | let regex = try! NSRegularExpression(pattern: pat, options: []) 187 | let matches = regex.matches(in: fromHTML, options: [], range: NSRange(location: 0, length: fromHTML.count)) 188 | var resourceURLs = [String]() 189 | for match in matches { 190 | let range = match.range(at:1) 191 | let r = fromHTML.index(fromHTML.startIndex, offsetBy: range.location) ..< fromHTML.index(fromHTML.startIndex, offsetBy: range.location+range.length) 192 | let url = fromHTML[r] 193 | resourceURLs.append(String(url)) 194 | } 195 | 196 | return resourceURLs 197 | } 198 | 199 | class func getStringContent(fromURL: String) -> (String) { 200 | let session = URLSession(configuration: URLSessionConfiguration.default, delegate: nil, delegateQueue: nil) 201 | var result = "" 202 | guard let URL = URL(string: fromURL) else {return result} 203 | var request = URLRequest(url: URL) 204 | request.httpMethod = "GET" 205 | 206 | let semaphore = DispatchSemaphore.init(value: 0) 207 | let task = session.dataTask(with: request, completionHandler: { (data: Data?, response: URLResponse?, error: Error?) -> Void in 208 | if (error == nil) { 209 | result = String.init(data: data!, encoding: .ascii)! 210 | } else { 211 | print("URL Session Task Failed: %@", error!.localizedDescription); 212 | } 213 | 214 | semaphore.signal() 215 | }) 216 | task.resume() 217 | semaphore.wait() 218 | return result 219 | } 220 | 221 | class func downloadFile(urlString: String) { 222 | let fileName = URL(fileURLWithPath: urlString).lastPathComponent 223 | 224 | guard !FileManager.default.fileExists(atPath: "./" + fileName) else { 225 | print("\(fileName): already exists, nothing to do!") 226 | return 227 | } 228 | 229 | print("Getting \(fileName) (\(urlString)):") 230 | 231 | guard let url = URL(string: urlString) else { 232 | print("<\(urlString)> is not valid URL!") 233 | return 234 | } 235 | 236 | DownloadSessionManager.shared.downloadFile(fromURL: url, toPath: "\(fileName)") 237 | } 238 | } 239 | 240 | let htmlText = iTunesUController.getStringContent(fromURL: "https://itunes.apple.com/cn/course/developing-ios-11-apps-with-swift/id1309275316") 241 | let resourceURLs = iTunesUController.getVideoResourceURLs(fromHTML: htmlText) 242 | for url in resourceURLs { 243 | iTunesUController.downloadFile(urlString: url) 244 | } 245 | -------------------------------------------------------------------------------- /en/Friday Session 1. Debugging and Xcode Tips and Tricks.srt: -------------------------------------------------------------------------------- 1 | 1 2 | 00:00:00,267 --> 00:00:04,603 3 | [MUSIC] 4 | 5 | 2 6 | 00:00:04,672 --> 00:00:07,372 7 | Stanford University. 8 | 9 | 3 10 | 00:00:07,441 --> 00:00:08,940 11 | >> Alrighty, well, 12 | hi everybody. 13 | 14 | 4 15 | 00:00:09,009 --> 00:00:11,176 16 | My name is Jason. 17 | I'm one of your two TAs. 18 | 19 | 5 20 | 00:00:11,178 --> 00:00:13,412 21 | Junjie is the other one. 22 | She is not here today and 23 | 24 | 6 25 | 00:00:13,414 --> 00:00:13,445 26 | we are gonna go over today 27 | kinda two different things. 28 | 29 | 7 30 | 00:00:14,515 --> 00:00:17,249 31 | we are gonna go over today 32 | kinda two different things. 33 | 34 | 8 35 | 00:00:17,317 --> 00:00:19,818 36 | One is just a whirlwind tour 37 | of Xcode just to make your 38 | 39 | 9 40 | 00:00:19,886 --> 00:00:21,353 41 | life a lot simpler for 42 | the entire quarter and 43 | 44 | 10 45 | 00:00:21,421 --> 00:00:23,856 46 | the rest of your life to make 47 | it more familiar to you. 48 | 49 | 11 50 | 00:00:23,924 --> 00:00:26,391 51 | And the other thing is how to 52 | use the debugger within Xcode 53 | 54 | 12 55 | 00:00:26,460 --> 00:00:27,426 56 | in particular. 57 | 58 | 13 59 | 00:00:29,830 --> 00:00:31,830 60 | than most sections just cuz 61 | there's not that much material 62 | 63 | 14 64 | 00:00:31,899 --> 00:00:33,965 65 | to get through, so we'll 66 | be out of here a bit early. 67 | 68 | 15 69 | 00:00:34,034 --> 00:00:35,867 70 | But um, there is also, 71 | there is also no slides or 72 | 73 | 16 74 | 00:00:35,936 --> 00:00:37,803 75 | anything like that, 76 | there is no lecture to give, 77 | 78 | 17 79 | 00:00:37,871 --> 00:00:39,871 80 | it's just I'm gonna hop 81 | right into the demo. 82 | 83 | 18 84 | 00:00:39,940 --> 00:00:42,107 85 | And feel free to stop me 86 | at any point, just shout, 87 | 88 | 19 89 | 00:00:42,175 --> 00:00:44,009 90 | raise your hand if something 91 | is not clear if you wanna, 92 | 93 | 20 94 | 00:00:44,078 --> 00:00:46,110 95 | you wanna learn more about 96 | something or whatever, and 97 | 98 | 21 99 | 00:00:46,179 --> 00:00:47,412 100 | I'll stop to do that. 101 | 102 | 22 103 | 00:00:47,481 --> 00:00:49,414 104 | I know we're going through 105 | a giant list of key commands. 106 | 107 | 23 108 | 00:00:49,483 --> 00:00:50,983 109 | It's not like the most 110 | engaging lecture, so 111 | 112 | 24 113 | 00:00:51,051 --> 00:00:52,850 114 | I'll try to be as 115 | engaging as possible, but 116 | 117 | 25 118 | 00:00:52,919 --> 00:00:54,786 119 | uh, it is actually 120 | super useful. 121 | 122 | 26 123 | 00:00:54,855 --> 00:00:56,922 124 | So if, if I want you to have 125 | like one take away from, 126 | 127 | 27 128 | 00:00:56,991 --> 00:00:59,757 129 | from this whole talk, it's 130 | that, if you wanna get good at 131 | 132 | 28 133 | 00:00:59,826 --> 00:01:01,893 134 | using Xcode and you wanna feel 135 | fluent when you're using it, 136 | 137 | 29 138 | 00:01:01,962 --> 00:01:04,096 139 | you actually need to know 140 | a lot of key commands. 141 | 142 | 30 143 | 00:01:04,164 --> 00:01:05,330 144 | It will really help you a lot. 145 | 146 | 31 147 | 00:01:07,668 --> 00:01:09,968 148 | Xcode from Paul in lectures 149 | but actually learning all the 150 | 151 | 32 152 | 00:01:10,037 --> 00:01:12,437 153 | shortcuts for everything will 154 | make your life way simpler. 155 | 156 | 33 157 | 00:01:12,506 --> 00:01:14,339 158 | And I hope I can kinda 159 | convince you today that all 160 | 161 | 34 162 | 00:01:14,408 --> 00:01:16,208 163 | this stuff is actually 164 | pretty intuitive. 165 | 166 | 35 167 | 00:01:16,276 --> 00:01:18,276 168 | It's not just like memorizing 169 | a bunch of random things, but 170 | 171 | 36 172 | 00:01:18,345 --> 00:01:20,678 173 | it's all laid out by Apple 174 | in a very nice and, and 175 | 176 | 37 177 | 00:01:20,747 --> 00:01:21,346 178 | orderly way. 179 | 180 | 38 181 | 00:01:23,717 --> 00:01:25,651 182 | So let's just dive right in. 183 | 184 | 39 185 | 00:01:25,719 --> 00:01:29,287 186 | I'm gonna pop up an Xcode here 187 | and I have Concentration open, 188 | 189 | 40 190 | 00:01:29,356 --> 00:01:31,856 191 | which is the end of 192 | Wednesday's lecture, 193 | 194 | 41 195 | 00:01:31,925 --> 00:01:33,392 196 | we had this code working. 197 | 198 | 42 199 | 00:01:33,460 --> 00:01:36,761 200 | The catch here though is that 201 | this version of Concentration 202 | 203 | 43 204 | 00:01:36,830 --> 00:01:40,399 205 | that I'm using right now, I've 206 | deemed it sad Concentration. 207 | 208 | 44 209 | 00:01:40,467 --> 00:01:41,433 210 | You can see right 211 | here on this folder, 212 | 213 | 45 214 | 00:01:43,470 --> 00:01:45,971 215 | we're gonna investigate 216 | that using the debugger. 217 | 218 | 46 219 | 00:01:46,039 --> 00:01:48,039 220 | But before we do that, 221 | I am gonna hop in and 222 | 223 | 47 224 | 00:01:48,108 --> 00:01:51,309 225 | give you a tour of Xcode using 226 | key commands and what not. 227 | 228 | 48 229 | 00:01:51,378 --> 00:01:54,713 230 | So you remember things from 231 | lecture where you've seen for 232 | 233 | 49 234 | 00:01:54,782 --> 00:01:56,415 235 | example, you can navigate 236 | around all the panes 237 | 238 | 50 239 | 00:01:56,484 --> 00:01:58,583 240 | by clicking these various 241 | buttons, but it's gonna 242 | 243 | 51 244 | 00:01:58,652 --> 00:02:00,318 245 | get really old really 246 | fast when you're coding. 247 | 248 | 52 249 | 00:02:00,387 --> 00:02:00,886 250 | If you're also 251 | 252 | 53 253 | 00:02:02,989 --> 00:02:04,823 254 | You can get like five 255 | times faster if you just 256 | 257 | 54 258 | 00:02:06,493 --> 00:02:08,159 259 | learn how things are laid 260 | out in some various things. 261 | 262 | 55 263 | 00:02:08,228 --> 00:02:10,228 264 | So the first thing, 265 | I'll just kinda go, 266 | 267 | 56 268 | 00:02:10,297 --> 00:02:12,497 269 | go through Xcode in order, 270 | kinda from the top left. 271 | 272 | 57 273 | 00:02:12,566 --> 00:02:15,767 274 | The first thing is, all this 275 | stuff up here at the top left, 276 | 277 | 58 278 | 00:02:15,836 --> 00:02:17,836 279 | you can actually do 280 | automated by key commands. 281 | 282 | 59 283 | 00:02:17,905 --> 00:02:20,371 284 | So instead of going up and 285 | clicking Run and what not, 286 | 287 | 60 288 | 00:02:20,440 --> 00:02:22,374 289 | you can do Cmd+R, 290 | and that will run. 291 | 292 | 61 293 | 00:02:24,912 --> 00:02:26,110 294 | So there, 295 | there are key commands for 296 | 297 | 62 298 | 00:02:26,179 --> 00:02:28,246 299 | controlling everything about 300 | running and, and stopping and 301 | 302 | 63 303 | 00:02:28,315 --> 00:02:29,515 304 | all that stuff. 305 | 306 | 64 307 | 00:02:29,583 --> 00:02:32,184 308 | Um, the next thing I wanna 309 | talk about is more important 310 | 311 | 65 312 | 00:02:32,252 --> 00:02:35,487 313 | which is just how you actually 314 | navigate around all of Xcode. 315 | 316 | 66 317 | 00:02:35,555 --> 00:02:38,657 318 | And so there are key commands 319 | for doing things like, if you 320 | 321 | 67 322 | 00:02:38,725 --> 00:02:41,927 323 | watch the screen right now, 324 | I can do this, this, this. 325 | 326 | 68 327 | 00:02:43,597 --> 00:02:45,063 328 | You get really fast 329 | at this stuff. 330 | 331 | 69 332 | 00:02:46,867 --> 00:02:49,334 333 | We're, were opening up 334 | a second editor right now. 335 | 336 | 70 337 | 00:02:50,503 --> 00:02:50,936 338 | Right, so 339 | 340 | 71 341 | 00:02:53,473 --> 00:02:55,840 342 | how you can like quickly 343 | get your way around Xcode. 344 | 345 | 72 346 | 00:02:55,909 --> 00:02:59,144 347 | So when I close everything 348 | that I just did. 349 | 350 | 73 351 | 00:02:59,213 --> 00:03:01,513 352 | Suppose that were at a full 353 | main screen like this, 354 | 355 | 74 356 | 00:03:01,582 --> 00:03:03,982 357 | the way that everything is 358 | laid out is very logical, 359 | 360 | 75 361 | 00:03:04,051 --> 00:03:06,852 362 | where the left-most pane, 363 | which is called the Navigator, 364 | 365 | 76 366 | 00:03:06,920 --> 00:03:08,152 367 | is controlled by Cmd+0. 368 | 369 | 77 370 | 00:03:08,221 --> 00:03:10,688 371 | That's what shows it. 372 | 373 | 78 374 | 00:03:10,757 --> 00:03:14,692 375 | And then the right-most pane 376 | is showed using Cmd+Option+0. 377 | 378 | 79 379 | 00:03:14,761 --> 00:03:18,130 380 | That's what shows and 381 | hides the op, right-most pane. 382 | 383 | 80 384 | 00:03:18,198 --> 00:03:21,633 385 | And then within each pane, 386 | so if I'm on the left pane, 387 | 388 | 81 389 | 00:03:21,702 --> 00:03:25,837 390 | if I do Cmd+1,2,3,4,5 you see 391 | how up on the top left here 392 | 393 | 82 394 | 00:03:25,906 --> 00:03:28,072 395 | we're going through 396 | these tabs? 397 | 398 | 83 399 | 00:03:28,141 --> 00:03:30,976 400 | 1, 2, 3, 4, 5, right? 401 | 402 | 84 403 | 00:03:31,045 --> 00:03:33,011 404 | And then if I do Cmd+Option+0, 405 | and 406 | 407 | 85 408 | 00:03:33,079 --> 00:03:36,414 409 | I look at these tabs up here, 410 | I just do Cmd+Option+1, 2 and 411 | 412 | 86 413 | 00:03:36,483 --> 00:03:38,950 414 | I can go through 415 | all those tabs. 416 | 417 | 87 418 | 00:03:39,019 --> 00:03:40,686 419 | So it's actually pretty 420 | nicely laid out like that. 421 | 422 | 88 423 | 00:03:40,754 --> 00:03:43,287 424 | You just remember that, Cmd+0 425 | is for showing and hiding and 426 | 427 | 89 428 | 00:03:43,356 --> 00:03:44,623 429 | then the numbers are for 430 | tabbing. 431 | 432 | 90 433 | 00:03:44,691 --> 00:03:47,058 434 | And then if you add in Option, 435 | it does the right side. 436 | 437 | 91 438 | 00:03:47,127 --> 00:03:48,560 439 | So it's pretty simple. 440 | 441 | 92 442 | 00:03:48,628 --> 00:03:52,464 443 | If you just do Cmd+Shift+y, 444 | it toggles the console and 445 | 446 | 93 447 | 00:03:52,532 --> 00:03:56,168 448 | the debugger at the bottom 449 | which we'll be working with 450 | 451 | 94 452 | 00:03:56,236 --> 00:03:57,469 453 | more later today. 454 | 455 | 95 456 | 00:03:57,538 --> 00:03:59,504 457 | And those commands alone are 458 | enough to just kind of like 459 | 460 | 96 461 | 00:03:59,573 --> 00:04:00,939 462 | constantly manage 463 | your window size, 464 | 465 | 97 466 | 00:04:01,008 --> 00:04:02,874 467 | especially if you're 468 | programming on a laptop where 469 | 470 | 98 471 | 00:04:02,943 --> 00:04:04,676 472 | you only have like limited 473 | screen real estate. 474 | 475 | 99 476 | 00:04:04,745 --> 00:04:07,812 477 | It's actually really nice to 478 | be able to use those commands 479 | 480 | 100 481 | 00:04:07,881 --> 00:04:09,881 482 | to quickly navigate around. 483 | 484 | 101 485 | 00:04:09,949 --> 00:04:12,717 486 | The next thing I'll point out 487 | is that, you know from lecture 488 | 489 | 102 490 | 00:04:12,786 --> 00:04:15,687 491 | that Xcode has uh, 492 | two different editors. 493 | 494 | 103 495 | 00:04:15,755 --> 00:04:18,757 496 | It has the um, the main editor 497 | that we' re in right here, and 498 | 499 | 104 500 | 00:04:18,826 --> 00:04:21,726 501 | it has also this thing 502 | called the assistant editor. 503 | 504 | 105 505 | 00:04:21,795 --> 00:04:27,132 506 | So if we do 507 | a Cmd+Option+Enter. 508 | 509 | 106 510 | 00:04:27,201 --> 00:04:29,834 511 | It pops up in this second 512 | pane over here, and 513 | 514 | 107 515 | 00:04:29,903 --> 00:04:33,171 516 | then we can do Cmd+Enter to 517 | go back to just one pane, so 518 | 519 | 108 520 | 00:04:33,240 --> 00:04:35,207 521 | this is super useful. 522 | 523 | 109 524 | 00:04:35,275 --> 00:04:37,842 525 | The one thing that I'll point 526 | out about this is that, 527 | 528 | 110 529 | 00:04:37,911 --> 00:04:41,380 530 | if you go to your, 531 | your Xcode preferences window, 532 | 533 | 111 534 | 00:04:41,448 --> 00:04:43,949 535 | we have all these things that 536 | we can, that we can toggle and 537 | 538 | 112 539 | 00:04:44,017 --> 00:04:44,549 540 | we won't go through all 541 | 542 | 113 543 | 00:04:46,519 --> 00:04:47,919 544 | You can go around and 545 | change your font colors and 546 | 547 | 114 548 | 00:04:47,988 --> 00:04:49,054 549 | all that good stuff. 550 | 551 | 115 552 | 00:04:51,658 --> 00:04:54,559 553 | recommend that you utilize, 554 | which is in Navigation here. 555 | 556 | 116 557 | 00:04:54,628 --> 00:04:57,896 558 | There's this little check box 559 | that says, Navigation uses 560 | 561 | 117 562 | 00:04:57,965 --> 00:05:01,199 563 | the primary editor, versus 564 | using the focused editor, and 565 | 566 | 118 567 | 00:05:01,268 --> 00:05:04,235 568 | by default, this is set to 569 | using the primary editor. 570 | 571 | 119 572 | 00:05:04,304 --> 00:05:07,539 573 | And that's going to probably 574 | torture you if you use certain 575 | 576 | 120 577 | 00:05:07,608 --> 00:05:09,006 578 | other key commands. 579 | 580 | 121 581 | 00:05:09,075 --> 00:05:10,542 582 | Let me give you an example. 583 | 584 | 122 585 | 00:05:10,610 --> 00:05:13,511 586 | One of the key commands is for 587 | opening certain files without 588 | 589 | 123 590 | 00:05:13,580 --> 00:05:15,146 591 | having to navigate 592 | there in the, 593 | 594 | 124 595 | 00:05:15,215 --> 00:05:16,815 596 | in the file 597 | navigator over here. 598 | 599 | 125 600 | 00:05:16,883 --> 00:05:19,450 601 | So let's say that I, 602 | we all know that there's 603 | 604 | 126 605 | 00:05:19,519 --> 00:05:22,454 606 | a file called Card.swift and 607 | we wanna open it, all I 608 | 609 | 127 610 | 00:05:22,523 --> 00:05:25,490 611 | have to do is Cmd+Shift+O for 612 | open, type card, and 613 | 614 | 128 615 | 00:05:25,559 --> 00:05:29,327 616 | then there's Card.swift, 617 | and that will open it up. 618 | 619 | 129 620 | 00:05:29,396 --> 00:05:30,128 621 | Now what if I wanted 622 | 623 | 130 624 | 00:05:32,900 --> 00:05:35,199 625 | So right here, 626 | I am zooming around in the, 627 | 628 | 131 629 | 00:05:35,268 --> 00:05:37,268 630 | in the right pane and I have 631 | the right pane selected. 632 | 633 | 132 634 | 00:05:37,337 --> 00:05:39,670 635 | If I do Cmd+Shift+O and 636 | I go back to ViewController, 637 | 638 | 133 639 | 00:05:39,739 --> 00:05:42,707 640 | it still opens in 641 | the left pane. 642 | 643 | 134 644 | 00:05:42,775 --> 00:05:45,309 645 | So if you don't have this use 646 | focused editor option selected 647 | 648 | 135 649 | 00:05:45,378 --> 00:05:47,378 650 | it's always going to 651 | use the left editor or 652 | 653 | 136 654 | 00:05:47,447 --> 00:05:49,180 655 | the prime editor for 656 | those key commands. 657 | 658 | 137 659 | 00:05:49,249 --> 00:05:52,150 660 | So that's why you wanna have 661 | this box checked, so that now 662 | 663 | 138 664 | 00:05:52,218 --> 00:05:55,086 665 | when we go over here and 666 | we have the right one selected 667 | 668 | 139 669 | 00:05:55,155 --> 00:05:57,655 670 | and we do card, it opens 671 | it over on the right pan. 672 | 673 | 140 674 | 00:05:57,724 --> 00:05:58,990 675 | Makes sense? 676 | 677 | 141 678 | 00:05:59,059 --> 00:06:02,526 679 | Cool, so I highly 680 | recommend that you do that. 681 | 682 | 142 683 | 00:06:02,595 --> 00:06:05,596 684 | Okay, the next thing, 685 | we're gonna do is actually 686 | 687 | 143 688 | 00:06:05,665 --> 00:06:08,566 689 | talk about text editing within 690 | each window really briefly. 691 | 692 | 144 693 | 00:06:08,635 --> 00:06:11,369 694 | One thing that I wanna point 695 | out is, you haven't dealt with 696 | 697 | 145 698 | 00:06:11,437 --> 00:06:14,038 699 | this problem yet because your 700 | Xcode projects haven't gotten 701 | 702 | 146 703 | 00:06:14,107 --> 00:06:15,907 704 | very large, but 705 | they will get very large. 706 | 707 | 147 708 | 00:06:15,976 --> 00:06:17,241 709 | There will be lots 710 | of code files and 711 | 712 | 148 713 | 00:06:17,310 --> 00:06:19,344 714 | eventually you'll have 715 | hundreds of files in a single 716 | 717 | 149 718 | 00:06:19,413 --> 00:06:21,579 719 | project when you're making 720 | a big Xcode project. 721 | 722 | 150 723 | 00:06:21,648 --> 00:06:24,682 724 | And one of the things that 725 | will help you navigate that is 726 | 727 | 151 728 | 00:06:24,751 --> 00:06:27,452 729 | to help yourself by 730 | leaving Navigation, 731 | 732 | 152 733 | 00:06:27,521 --> 00:06:30,688 734 | like Navigation helper things 735 | throughout your files and 736 | 737 | 153 738 | 00:06:30,757 --> 00:06:33,524 739 | one way to do that is this 740 | comment right here that is 741 | 742 | 154 743 | 00:06:33,593 --> 00:06:35,259 744 | prefixed with comment mark. 745 | 746 | 155 747 | 00:06:35,295 --> 00:06:35,326 748 | excuse me. 749 | 750 | 156 751 | 00:06:37,731 --> 00:06:38,363 752 | below this line. 753 | 754 | 157 755 | 00:06:41,134 --> 00:06:43,535 756 | this notification in 757 | this window up here. 758 | 759 | 158 760 | 00:06:43,604 --> 00:06:44,936 761 | That's where card 762 | touch behavior is and 763 | 764 | 159 765 | 00:06:45,004 --> 00:06:46,270 766 | it will jump to that 767 | point of the file. 768 | 769 | 160 770 | 00:06:46,339 --> 00:06:48,306 771 | And so you can imagine in big 772 | files, using these marks is 773 | 774 | 161 775 | 00:06:48,375 --> 00:06:50,241 776 | actually super helpful 777 | when they're really long, 778 | 779 | 162 780 | 00:06:50,309 --> 00:06:51,543 781 | something to navigate around. 782 | 783 | 163 784 | 00:06:53,313 --> 00:06:56,013 785 | The next thing that I'll point 786 | out about text editing is, 787 | 788 | 164 789 | 00:06:56,082 --> 00:06:58,850 790 | it's very useful just to know 791 | basic text editing commands. 792 | 793 | 165 794 | 00:06:58,919 --> 00:07:01,386 795 | Like for example, if you 796 | have many lines selected and 797 | 798 | 166 799 | 00:07:01,454 --> 00:07:03,821 800 | you want to comment them 801 | out all of a sudden, 802 | 803 | 167 804 | 00:07:03,890 --> 00:07:07,758 805 | just Cmd+/ will comment 806 | things out like that and 807 | 808 | 168 809 | 00:07:07,827 --> 00:07:10,295 810 | then Cmd+/ again 811 | will undo them. 812 | 813 | 169 814 | 00:07:10,363 --> 00:07:12,897 815 | Another maybe even more useful 816 | thing is when you get yourself 817 | 818 | 170 819 | 00:07:12,966 --> 00:07:15,100 820 | into a, 821 | a nasty state like this. 822 | 823 | 171 824 | 00:07:17,070 --> 00:07:19,437 825 | Just mess some stuff up for 826 | no reason. 827 | 828 | 172 829 | 00:07:19,506 --> 00:07:21,372 830 | And you can tell that it's 831 | really not formatted. 832 | 833 | 173 834 | 00:07:21,441 --> 00:07:23,074 835 | We can select everything 836 | that we want and 837 | 838 | 174 839 | 00:07:23,143 --> 00:07:25,743 840 | do Ctrl+I, I for 841 | indent and it will just 842 | 843 | 175 844 | 00:07:25,812 --> 00:07:28,179 845 | magically put everything 846 | exactly like we wanted. 847 | 848 | 176 849 | 00:07:28,248 --> 00:07:29,747 850 | So that'll save you a lot 851 | of time and headache, 852 | 853 | 177 854 | 00:07:29,816 --> 00:07:31,516 855 | if things are not, if you, 856 | 857 | 178 858 | 00:07:31,585 --> 00:07:33,951 859 | if you can't tell things that 860 | are nicely indented or not. 861 | 862 | 179 863 | 00:07:34,020 --> 00:07:36,054 864 | And I'll list these all 865 | online on Piazza by the way, 866 | 867 | 180 868 | 00:07:36,122 --> 00:07:37,922 869 | so it's not like you need 870 | to know down what they are. 871 | 872 | 181 873 | 00:07:37,991 --> 00:07:39,324 874 | I just kinda want 875 | to give you the, 876 | 877 | 182 878 | 00:07:39,393 --> 00:07:41,426 879 | give you the lay 880 | of the land here. 881 | 882 | 183 883 | 00:07:41,495 --> 00:07:43,795 884 | So hopefully we're through 885 | the, the fast sorta boring 886 | 887 | 184 888 | 00:07:43,863 --> 00:07:46,364 889 | part of the lecture where it's 890 | just all of the key commands. 891 | 892 | 185 893 | 00:07:46,433 --> 00:07:48,199 894 | But I highly recommend that 895 | you learn all this stuff and 896 | 897 | 186 898 | 00:07:48,267 --> 00:07:49,967 899 | start getting it into 900 | your muscle memory so 901 | 902 | 187 903 | 00:07:50,036 --> 00:07:51,436 904 | that when you're 905 | navigating on Xcode, 906 | 907 | 188 908 | 00:07:51,505 --> 00:07:54,206 909 | you're not torturing yourself 910 | for having to click around 911 | 912 | 189 913 | 00:07:54,274 --> 00:07:56,941 914 | everywhere and 915 | do everything manually. 916 | 917 | 190 918 | 00:07:57,010 --> 00:07:59,277 919 | The next thing to do is 920 | to actually start using 921 | 922 | 191 923 | 00:07:59,346 --> 00:08:00,111 924 | the debugger. 925 | 926 | 192 927 | 00:08:02,416 --> 00:08:04,449 928 | we're not actually using 929 | the version of concentration 930 | 931 | 193 932 | 00:08:04,518 --> 00:08:06,284 933 | directly from the end 934 | of Wednesday's lecture. 935 | 936 | 194 937 | 00:08:06,353 --> 00:08:08,587 938 | We're actually using a 939 | slightly broken version of it, 940 | 941 | 195 942 | 00:08:08,655 --> 00:08:11,088 943 | and so let's see what happens 944 | when we actually run this one. 945 | 946 | 196 947 | 00:08:11,157 --> 00:08:13,991 948 | I'm gonna run it on 949 | the iPhone X simulator using 950 | 951 | 197 952 | 00:08:14,060 --> 00:08:17,929 953 | our trusty Cmd+R key command, 954 | and let's see what happens. 955 | 956 | 198 957 | 00:08:30,043 --> 00:08:32,843 958 | So I've crashed, it doesn't 959 | even work right from 960 | 961 | 199 962 | 00:08:32,912 --> 00:08:35,747 963 | the start, and we're thrown 964 | to the app delegate, 965 | 966 | 200 967 | 00:08:35,815 --> 00:08:37,515 968 | which is the main file. 969 | 970 | 201 971 | 00:08:37,584 --> 00:08:39,551 972 | It's crashing on the main 973 | method it says, but 974 | 975 | 202 976 | 00:08:39,619 --> 00:08:40,785 977 | that's not very helpful. 978 | 979 | 203 980 | 00:08:40,854 --> 00:08:43,021 981 | So the way that we first 982 | get more insight into this, 983 | 984 | 204 985 | 00:08:43,090 --> 00:08:44,756 986 | is to look into 987 | the actual stack trace, 988 | 989 | 205 990 | 00:08:44,824 --> 00:08:46,691 991 | which shows up in 992 | the console log down here. 993 | 994 | 206 995 | 00:08:46,760 --> 00:08:48,325 996 | And you just see this 997 | huge stack trace, 998 | 999 | 207 1000 | 00:08:48,394 --> 00:08:50,361 1001 | which goes all the way 1002 | into the internals of iOS, 1003 | 1004 | 208 1005 | 00:08:50,430 --> 00:08:51,730 1006 | all the way down 1007 | through UI kit, 1008 | 1009 | 209 1010 | 00:08:51,798 --> 00:08:53,665 1011 | and core foundation, 1012 | and all this stuff, and 1013 | 1014 | 210 1015 | 00:08:53,734 --> 00:08:56,100 1016 | it's not very helpful to see 1017 | all these memory addresses. 1018 | 1019 | 211 1020 | 00:08:56,169 --> 00:08:59,737 1021 | But if you scroll up to the 1022 | top in almost every case you 1023 | 1024 | 212 1025 | 00:08:59,806 --> 00:09:02,140 1026 | will see what 1027 | the actual reason for 1028 | 1029 | 213 1030 | 00:09:02,208 --> 00:09:04,009 1031 | terminating the app was. 1032 | 1033 | 214 1034 | 00:09:04,077 --> 00:09:07,345 1035 | And in this case it tells 1036 | us that there's an uncaught 1037 | 1038 | 215 1039 | 00:09:07,414 --> 00:09:10,081 1040 | exception called 1041 | UnknownKeyException, 1042 | 1043 | 216 1044 | 00:09:10,150 --> 00:09:12,683 1045 | which we may might not 1046 | know what that is. 1047 | 1048 | 217 1049 | 00:09:12,752 --> 00:09:16,253 1050 | It tells us that the reason 1051 | for the crash was, oops, 1052 | 1053 | 218 1054 | 00:09:16,322 --> 00:09:16,821 1055 | sorry. 1056 | 1057 | 219 1058 | 00:09:16,890 --> 00:09:17,622 1059 | The reason for 1060 | 1061 | 220 1062 | 00:09:20,727 --> 00:09:23,727 1063 | so it already tells us what 1064 | file something is happening 1065 | 1066 | 221 1067 | 00:09:23,796 --> 00:09:26,731 1068 | in, has some error on 1069 | setValue:forUndefinedKey. 1070 | 1071 | 222 1072 | 00:09:26,799 --> 00:09:28,433 1073 | Unclear exactly what that is, 1074 | 1075 | 223 1076 | 00:09:28,501 --> 00:09:31,502 1077 | cuz we didn't write that 1078 | method, and then this class is 1079 | 1080 | 224 1081 | 00:09:31,571 --> 00:09:35,006 1082 | not key value coding-compliant 1083 | for the key flipCountLabel. 1084 | 1085 | 225 1086 | 00:09:35,075 --> 00:09:39,710 1087 | So that gives us even more 1088 | insight, is something is wrong 1089 | 1090 | 226 1091 | 00:09:39,779 --> 00:09:44,715 1092 | with flipCountLabel, and so 1093 | if we go into view controller, 1094 | 1095 | 227 1096 | 00:09:44,784 --> 00:09:47,752 1097 | and we go check out 1098 | flipCountLabel, 1099 | 1100 | 228 1101 | 00:09:47,820 --> 00:09:52,590 1102 | we actually see flipCountLabel 1103 | abbreviated to LBL here, 1104 | 1105 | 229 1106 | 00:09:52,659 --> 00:09:56,027 1107 | which is not what it 1108 | says in the debugger. 1109 | 1110 | 230 1111 | 00:09:56,096 --> 00:09:58,896 1112 | Can anyone figure out what's 1113 | actually going on here? 1114 | 1115 | 231 1116 | 00:09:58,965 --> 00:10:00,599 1117 | How does it know to print 1118 | out flipCountLabel? 1119 | 1120 | 232 1121 | 00:10:01,934 --> 00:10:02,900 1122 | Any ideas? 1123 | 1124 | 233 1125 | 00:10:02,969 --> 00:10:05,804 1126 | If we've named 1127 | it flipCountLbl, 1128 | 1129 | 234 1130 | 00:10:05,872 --> 00:10:09,641 1131 | then why is it printing 1132 | out flipCountLabel? 1133 | 1134 | 235 1135 | 00:10:09,709 --> 00:10:11,008 1136 | Can anyone think of why? 1137 | 1138 | 236 1139 | 00:10:11,077 --> 00:10:13,011 1140 | No? 1141 | 1142 | 237 1143 | 00:10:13,079 --> 00:10:14,179 1144 | Yeah, in the back. 1145 | 1146 | 238 1147 | 00:10:17,817 --> 00:10:18,583 1148 | Yeah, exactly. 1149 | 1150 | 239 1151 | 00:10:18,652 --> 00:10:19,751 1152 | So that's what I named it. 1153 | 1154 | 240 1155 | 00:10:19,819 --> 00:10:20,418 1156 | What he said was, 1157 | 1158 | 241 1159 | 00:10:22,622 --> 00:10:24,655 1160 | So if we go over to 1161 | the storyboard over here, 1162 | 1163 | 242 1164 | 00:10:24,724 --> 00:10:27,591 1165 | actually let's just open up 1166 | both side-by-side using all of 1167 | 1168 | 243 1169 | 00:10:27,660 --> 00:10:29,094 1170 | our happy new key commands. 1171 | 1172 | 244 1173 | 00:10:31,631 --> 00:10:33,064 1174 | So we got the storyboard 1175 | open over here. 1176 | 1177 | 245 1178 | 00:10:38,438 --> 00:10:39,804 1179 | I'm gonna make this 1180 | fullscreen for a moment, 1181 | 1182 | 246 1183 | 00:10:39,872 --> 00:10:41,239 1184 | cuz it's easier to 1185 | see what's going on. 1186 | 1187 | 247 1188 | 00:10:43,810 --> 00:10:44,476 1189 | If we look inside here, 1190 | 1191 | 248 1192 | 00:10:48,781 --> 00:10:51,782 1193 | Now this right here, 1194 | that I've selected, 1195 | 1196 | 249 1197 | 00:10:51,851 --> 00:10:54,685 1198 | is not actually the official 1199 | name of the object. 1200 | 1201 | 250 1202 | 00:10:54,754 --> 00:10:58,390 1203 | If I open up our assistant 1204 | editor over here, and 1205 | 1206 | 251 1207 | 00:10:58,458 --> 00:11:00,558 1208 | I go over to the view 1209 | controller, and 1210 | 1211 | 252 1212 | 00:11:00,627 --> 00:11:03,494 1213 | I look at its rightmost pane, 1214 | this is where we see all of 1215 | 1216 | 253 1217 | 00:11:03,563 --> 00:11:04,663 1218 | the things that 1219 | we've connected, and 1220 | 1221 | 254 1222 | 00:11:06,633 --> 00:11:09,567 1223 | on the storyboard what they're 1224 | actually connected to. 1225 | 1226 | 255 1227 | 00:11:09,636 --> 00:11:11,569 1228 | Here's where you see 1229 | that we have this outlet. 1230 | 1231 | 256 1232 | 00:11:11,638 --> 00:11:14,271 1233 | The name of the outlet is 1234 | Flip Count Label, and it's 1235 | 1236 | 257 1237 | 00:11:14,340 --> 00:11:17,175 1238 | connected to flipCountLabel, 1239 | but if you zoom in here, 1240 | 1241 | 258 1242 | 00:11:17,244 --> 00:11:20,244 1243 | you'll notice that there's 1244 | a little exclamation point or 1245 | 1246 | 259 1247 | 00:11:20,313 --> 00:11:22,714 1248 | an empty thing, 1249 | as compared to the normal dot. 1250 | 1251 | 260 1252 | 00:11:22,783 --> 00:11:24,682 1253 | So that's it telling you that 1254 | this thing actually can't 1255 | 1256 | 261 1257 | 00:11:24,751 --> 00:11:27,318 1258 | find what it's 1259 | actually connected to. 1260 | 1261 | 262 1262 | 00:11:27,387 --> 00:11:29,654 1263 | So if we go back over 1264 | into our View Controller, 1265 | 1266 | 263 1267 | 00:11:29,723 --> 00:11:30,655 1268 | in this editor, and 1269 | 1270 | 264 1271 | 00:11:38,865 --> 00:11:41,166 1272 | And then we go back to 1273 | the storyboard editor, 1274 | 1275 | 265 1276 | 00:11:41,234 --> 00:11:44,035 1277 | we should see that, that 1278 | error is no longer there, and 1279 | 1280 | 266 1281 | 00:11:44,103 --> 00:11:46,504 1282 | we actually have 1283 | Flip Count Label connected. 1284 | 1285 | 267 1286 | 00:11:46,573 --> 00:11:49,407 1287 | So this is a bug that you'll 1288 | probably see often at first, 1289 | 1290 | 268 1291 | 00:11:49,476 --> 00:11:52,377 1292 | as you're not quite, I mean, 1293 | it's just hard to keep your 1294 | 1295 | 269 1296 | 00:11:52,445 --> 00:11:54,745 1297 | storyboard in sync with 1298 | your code sometimes. 1299 | 1300 | 270 1301 | 00:11:54,814 --> 00:11:57,581 1302 | So just know that there's this 1303 | error about key value coding 1304 | 1305 | 271 1306 | 00:11:57,650 --> 00:11:59,484 1307 | compliance and 1308 | that's what it means. 1309 | 1310 | 272 1311 | 00:11:59,553 --> 00:12:01,152 1312 | That you should go 1313 | check this right most 1314 | 1315 | 273 1316 | 00:12:01,220 --> 00:12:03,120 1317 | pane with your 1318 | View Controller selected, and 1319 | 1320 | 274 1321 | 00:12:03,189 --> 00:12:04,956 1322 | that will help you get 1323 | rid of these errors. 1324 | 1325 | 275 1326 | 00:12:05,025 --> 00:12:06,524 1327 | Cool, so 1328 | we're gonna save that, and 1329 | 1330 | 276 1331 | 00:12:06,592 --> 00:12:07,759 1332 | then we're gonna run it. 1333 | 1334 | 277 1335 | 00:12:16,569 --> 00:12:18,803 1336 | Okay great, so 1337 | it's actually running now. 1338 | 1339 | 278 1340 | 00:12:18,872 --> 00:12:21,272 1341 | So if you click around 1342 | to play the game. 1343 | 1344 | 279 1345 | 00:12:21,341 --> 00:12:26,243 1346 | Oops, it crashed, and so if I 1347 | look down inside the console 1348 | 1349 | 280 1350 | 00:12:26,312 --> 00:12:29,713 1351 | here, I don't actually 1352 | have any logs. 1353 | 1354 | 281 1355 | 00:12:29,782 --> 00:12:32,316 1356 | It didn't actually show me in 1357 | the console why it crashed. 1358 | 1359 | 282 1360 | 00:12:32,385 --> 00:12:34,919 1361 | In fact, 1362 | I have no info anywhere. 1363 | 1364 | 283 1365 | 00:12:34,987 --> 00:12:36,220 1366 | Let's try and run it again and 1367 | 1368 | 284 1369 | 00:12:36,289 --> 00:12:38,356 1370 | see if we can figure out 1371 | what causes it to crash. 1372 | 1373 | 285 1374 | 00:12:38,425 --> 00:12:39,557 1375 | It looked like that 1376 | time it crashed when 1377 | 1378 | 286 1379 | 00:12:39,626 --> 00:12:40,291 1380 | I clicked on a card. 1381 | 1382 | 287 1383 | 00:12:43,763 --> 00:12:47,365 1384 | Okay, that time, it let me 1385 | click on one of the cards, but 1386 | 1387 | 288 1388 | 00:12:47,434 --> 00:12:50,735 1389 | it still crashed, so 1390 | let's try it again. 1391 | 1392 | 289 1393 | 00:12:50,803 --> 00:12:52,770 1394 | Let's see if we can figure 1395 | out some sort of pattern for 1396 | 1397 | 290 1398 | 00:12:52,839 --> 00:12:53,638 1399 | what's going on. 1400 | 1401 | 291 1402 | 00:12:56,509 --> 00:12:59,076 1403 | That time it crashed 1404 | after four cards. 1405 | 1406 | 292 1407 | 00:12:59,145 --> 00:13:01,679 1408 | So does anyone have an idea 1409 | for how we can actually start 1410 | 1411 | 293 1412 | 00:13:01,748 --> 00:13:03,414 1413 | figuring out how 1414 | to debug this? 1415 | 1416 | 294 1417 | 00:13:03,482 --> 00:13:04,015 1418 | Like how on Earth are we 1419 | 1420 | 295 1421 | 00:13:05,485 --> 00:13:08,386 1422 | It seems to be crashing 1423 | randomly after some number 1424 | 1425 | 296 1426 | 00:13:08,455 --> 00:13:09,220 1427 | of cards, and 1428 | 1429 | 297 1430 | 00:13:15,795 --> 00:13:16,728 1431 | Anyone have an idea? 1432 | 1433 | 298 1434 | 00:13:18,531 --> 00:13:20,898 1435 | Even if you think it's silly, 1436 | just say something. 1437 | 1438 | 299 1439 | 00:13:20,967 --> 00:13:22,200 1440 | Say one way we can do this. 1441 | 1442 | 300 1443 | 00:13:22,268 --> 00:13:23,034 1444 | Print statements? 1445 | 1446 | 301 1447 | 00:13:23,102 --> 00:13:23,935 1448 | Yeah, print statements. 1449 | 1450 | 302 1451 | 00:13:25,671 --> 00:13:27,538 1452 | frustrating if you were in 1453 | a really big app though. 1454 | 1455 | 303 1456 | 00:13:27,607 --> 00:13:29,106 1457 | But you already see 1458 | the problem with print 1459 | 1460 | 304 1461 | 00:13:29,175 --> 00:13:31,175 1462 | statements is, if we're 1463 | gonna use print statements, 1464 | 1465 | 305 1466 | 00:13:31,244 --> 00:13:33,311 1467 | what the heck are we gonna do, 1468 | go inside every method, and 1469 | 1470 | 306 1471 | 00:13:33,380 --> 00:13:34,812 1472 | type print at every 1473 | point in the method, 1474 | 1475 | 307 1476 | 00:13:34,881 --> 00:13:36,514 1477 | and figure out where, 1478 | and what's going on? 1479 | 1480 | 308 1481 | 00:13:36,583 --> 00:13:37,849 1482 | We have to inspect 1483 | our entire app, 1484 | 1485 | 309 1486 | 00:13:37,917 --> 00:13:40,484 1487 | imagine we had like a 100,000 1488 | lines of code that'd be really 1489 | 1490 | 310 1491 | 00:13:40,553 --> 00:13:41,085 1492 | frustrating. 1493 | 1494 | 311 1495 | 00:13:44,424 --> 00:13:47,558 1496 | think about when the actual 1497 | app is crashing. 1498 | 1499 | 312 1500 | 00:13:47,626 --> 00:13:49,593 1501 | At what point is it crashing? 1502 | 1503 | 313 1504 | 00:13:49,662 --> 00:13:51,662 1505 | When you flip a card. 1506 | 1507 | 314 1508 | 00:13:51,731 --> 00:13:52,830 1509 | When I flip a card. 1510 | 1511 | 315 1512 | 00:13:52,899 --> 00:13:54,065 1513 | Can you use a break 1514 | point somewhere? 1515 | 1516 | 316 1517 | 00:13:54,133 --> 00:13:56,034 1518 | Bingo, so you say, 1519 | just using a break point, 1520 | 1521 | 317 1522 | 00:13:56,102 --> 00:13:58,369 1523 | because it's definitely 1524 | crashing when we flip a card. 1525 | 1526 | 318 1527 | 00:13:58,438 --> 00:13:59,571 1528 | So that's a new idea. 1529 | 1530 | 319 1531 | 00:14:03,409 --> 00:14:04,908 1532 | inside that method. 1533 | 1534 | 320 1535 | 00:14:04,977 --> 00:14:07,378 1536 | And then that will help 1537 | us hopefully figure out, 1538 | 1539 | 321 1540 | 00:14:07,447 --> 00:14:10,281 1541 | when the crashing happens 1542 | we'll be able to step through, 1543 | 1544 | 322 1545 | 00:14:10,350 --> 00:14:12,517 1546 | and hopefully figure 1547 | out what's going on. 1548 | 1549 | 323 1550 | 00:14:12,585 --> 00:14:14,319 1551 | So the way break 1552 | points work in Xcode, 1553 | 1554 | 324 1555 | 00:14:14,387 --> 00:14:16,888 1556 | and if you haven't used break 1557 | points before it's a way 1558 | 1559 | 325 1560 | 00:14:16,956 --> 00:14:19,457 1561 | for your debugger to pause 1562 | execution of your program on 1563 | 1564 | 326 1565 | 00:14:19,525 --> 00:14:21,258 1566 | a certain line of code. 1567 | 1568 | 327 1569 | 00:14:21,327 --> 00:14:23,628 1570 | So if we go inside 1571 | the touchCard method, 1572 | 1573 | 328 1574 | 00:14:23,696 --> 00:14:24,995 1575 | which is we know, 1576 | the first thing that 1577 | 1578 | 329 1579 | 00:14:25,064 --> 00:14:27,465 1580 | happens directly when 1581 | we click on a card. 1582 | 1583 | 330 1584 | 00:14:27,533 --> 00:14:30,701 1585 | We can click over here on the 1586 | left pane on the line of code, 1587 | 1588 | 331 1589 | 00:14:30,770 --> 00:14:32,437 1590 | and it pops up this 1591 | little blue arrow, and 1592 | 1593 | 332 1594 | 00:14:32,505 --> 00:14:33,871 1595 | that's what a break point is. 1596 | 1597 | 333 1598 | 00:14:33,940 --> 00:14:36,307 1599 | And that instructs the 1600 | debugger to please stop there 1601 | 1602 | 334 1603 | 00:14:36,375 --> 00:14:37,175 1604 | when we get there. 1605 | 1606 | 335 1607 | 00:14:40,380 --> 00:14:43,447 1608 | in the left down here, and 1609 | you can see that it has this 1610 | 1611 | 336 1612 | 00:14:43,516 --> 00:14:46,551 1613 | little blue arrow that looks 1614 | just like this one, and 1615 | 1616 | 337 1617 | 00:14:46,619 --> 00:14:48,753 1618 | when I click on it 1619 | it turns into grey, 1620 | 1621 | 338 1622 | 00:14:48,821 --> 00:14:51,189 1623 | which means break 1624 | points are disabled. 1625 | 1626 | 339 1627 | 00:14:51,257 --> 00:14:53,223 1628 | All break points in the entire 1629 | program are disabled, so 1630 | 1631 | 340 1632 | 00:14:53,292 --> 00:14:55,293 1633 | if you want break points to be 1634 | enabled you have to activate 1635 | 1636 | 341 1637 | 00:14:55,362 --> 00:14:56,628 1638 | them this way. 1639 | 1640 | 342 1641 | 00:14:56,696 --> 00:14:59,563 1642 | There's also a pane, up inside 1643 | the navigator over here, 1644 | 1645 | 343 1646 | 00:14:59,632 --> 00:15:02,400 1647 | that looks the same way, and 1648 | it'll show you a list of all 1649 | 1650 | 344 1651 | 00:15:02,468 --> 00:15:04,835 1652 | your break points throughout 1653 | your entire Xcode project. 1654 | 1655 | 345 1656 | 00:15:04,904 --> 00:15:06,037 1657 | So, you can also go 1658 | up here to this pane. 1659 | 1660 | 346 1661 | 00:15:08,441 --> 00:15:09,874 1662 | do with break points 1663 | than just this. 1664 | 1665 | 347 1666 | 00:15:09,943 --> 00:15:12,877 1667 | You can also go to this little 1668 | plus at the bottom left of 1669 | 1670 | 348 1671 | 00:15:12,946 --> 00:15:16,180 1672 | this pane, and you can set 1673 | certain special break points. 1674 | 1675 | 349 1676 | 00:15:16,248 --> 00:15:18,916 1677 | So for example, there's one 1678 | called an exception break 1679 | 1680 | 350 1681 | 00:15:18,985 --> 00:15:21,051 1682 | point, so if I add that, 1683 | what this does is, 1684 | 1685 | 351 1686 | 00:15:21,120 --> 00:15:22,687 1687 | anytime an exception 1688 | is thrown, 1689 | 1690 | 352 1691 | 00:15:22,755 --> 00:15:24,555 1692 | if you throw an exception 1693 | in your app, 1694 | 1695 | 353 1696 | 00:15:24,624 --> 00:15:27,391 1697 | instead of just throwing the 1698 | exception to the console, or 1699 | 1700 | 354 1701 | 00:15:27,460 --> 00:15:30,528 1702 | crashing, or what not, it'll 1703 | actually always, by default, 1704 | 1705 | 355 1706 | 00:15:30,597 --> 00:15:32,963 1707 | open the debugger and 1708 | pause on that line of code. 1709 | 1710 | 356 1711 | 00:15:33,032 --> 00:15:34,732 1712 | So if you just always 1713 | have this all exceptions 1714 | 1715 | 357 1716 | 00:15:34,800 --> 00:15:36,467 1717 | break point running 1718 | that will actually help 1719 | 1720 | 358 1721 | 00:15:36,535 --> 00:15:38,002 1722 | you in your developing for 1723 | example, so 1724 | 1725 | 359 1726 | 00:15:38,070 --> 00:15:40,204 1727 | you can actually customize 1728 | break points here that way. 1729 | 1730 | 360 1731 | 00:15:40,273 --> 00:15:42,840 1732 | You can also disable 1733 | individual break points from 1734 | 1735 | 361 1736 | 00:15:42,909 --> 00:15:43,474 1737 | this pane. 1738 | 1739 | 362 1740 | 00:15:46,112 --> 00:15:47,879 1741 | the one that we just 1742 | set right here, but 1743 | 1744 | 363 1745 | 00:15:47,947 --> 00:15:50,514 1746 | this one that I just mentioned 1747 | about exceptions causing 1748 | 1749 | 364 1750 | 00:15:50,583 --> 00:15:52,783 1751 | break points to happen, 1752 | this one is disabled. 1753 | 1754 | 365 1755 | 00:15:52,852 --> 00:15:54,452 1756 | So you can selectively 1757 | go enable and 1758 | 1759 | 366 1760 | 00:15:54,520 --> 00:15:56,988 1761 | disable break points all 1762 | across every file off on this 1763 | 1764 | 367 1765 | 00:15:57,056 --> 00:15:58,756 1766 | one pane, which is nice. 1767 | 1768 | 368 1769 | 00:15:58,824 --> 00:16:00,224 1770 | But oftentimes all 1771 | that you need to do, 1772 | 1773 | 369 1774 | 00:16:00,292 --> 00:16:02,059 1775 | I'm gonna disable 1776 | the exceptions one. 1777 | 1778 | 370 1779 | 00:16:02,128 --> 00:16:04,161 1780 | Oftentimes all that you need 1781 | to do is actually within 1782 | 1783 | 371 1784 | 00:16:04,230 --> 00:16:07,331 1785 | an individual file, set the 1786 | break point in that one file. 1787 | 1788 | 372 1789 | 00:16:07,400 --> 00:16:09,033 1790 | So okay if we run 1791 | the program now. 1792 | 1793 | 373 1794 | 00:16:13,572 --> 00:16:16,507 1795 | When I click on a card we'll 1796 | see that what happens is we 1797 | 1798 | 374 1799 | 00:16:16,576 --> 00:16:18,176 1800 | end up on this line of code. 1801 | 1802 | 375 1803 | 00:16:19,312 --> 00:16:21,712 1804 | So the program is paused at 1805 | 1806 | 376 1807 | 00:16:21,781 --> 00:16:24,048 1808 | this line where we're 1809 | setting off the card number. 1810 | 1811 | 377 1812 | 00:16:24,116 --> 00:16:26,250 1813 | And the first thing to point 1814 | out is that you'll notice 1815 | 1816 | 378 1817 | 00:16:26,319 --> 00:16:28,619 1818 | in the debugger itself we 1819 | actually have a trace of 1820 | 1821 | 379 1822 | 00:16:28,688 --> 00:16:31,355 1823 | everything that's stored off 1824 | in memory at this point. 1825 | 1826 | 380 1827 | 00:16:31,424 --> 00:16:34,625 1828 | So self would refer to the 1829 | view controller in this case. 1830 | 1831 | 381 1832 | 00:16:34,694 --> 00:16:36,160 1833 | And you can see if I zoom in, 1834 | you can see 1835 | 1836 | 382 1837 | 00:16:36,229 --> 00:16:38,096 1838 | that it says concentration 1839 | view controller and 1840 | 1841 | 383 1842 | 00:16:38,164 --> 00:16:40,464 1843 | this is the memory address 1844 | of the view controller, but 1845 | 1846 | 384 1847 | 00:16:40,533 --> 00:16:42,867 1848 | it not only gives you that, 1849 | it gives you a drop down menu 1850 | 1851 | 385 1852 | 00:16:42,936 --> 00:16:45,669 1853 | with all of the things that 1854 | are instance variables 1855 | 1856 | 386 1857 | 00:16:45,738 --> 00:16:49,040 1858 | stored off on that 1859 | instance of that class. 1860 | 1861 | 387 1862 | 00:16:49,108 --> 00:16:53,911 1863 | So things like we have the 1864 | instance of the actual game 1865 | 1866 | 388 1867 | 00:16:53,980 --> 00:16:57,048 1868 | of concentration and that has 1869 | lots of properties itself. 1870 | 1871 | 389 1872 | 00:16:57,116 --> 00:17:00,184 1873 | If it has the array of cards 1874 | and then it also has that 1875 | 1876 | 390 1877 | 00:17:00,252 --> 00:17:02,786 1878 | variable index of the one and 1879 | only face up card, and we can 1880 | 1881 | 391 1882 | 00:17:02,855 --> 00:17:05,957 1883 | expand cards and we can look 1884 | into each card and then we can 1885 | 1886 | 392 1887 | 00:17:06,026 --> 00:17:07,959 1888 | find out that the identifier 1889 | for that card is five. 1890 | 1891 | 393 1892 | 00:17:08,027 --> 00:17:10,461 1893 | So you can actually see all of 1894 | the things that are stored off 1895 | 1896 | 394 1897 | 00:17:10,529 --> 00:17:13,163 1898 | right here, 1899 | inside of the debugger, so 1900 | 1901 | 395 1902 | 00:17:13,232 --> 00:17:17,034 1903 | that's super nice, and we can 1904 | do that, use that to confirm 1905 | 1906 | 396 1907 | 00:17:17,103 --> 00:17:20,438 1908 | something like maybe we 1909 | want to make sure that. 1910 | 1911 | 397 1912 | 00:17:20,506 --> 00:17:21,839 1913 | The identifiers that we've set 1914 | 1915 | 398 1916 | 00:17:21,908 --> 00:17:24,408 1917 | actually have an equal 1918 | number of pairs of options. 1919 | 1920 | 399 1921 | 00:17:24,477 --> 00:17:26,544 1922 | So, if we go through,we 1923 | see that the first one is 1924 | 1925 | 400 1926 | 00:17:26,612 --> 00:17:29,013 1927 | identifier five, the second 1928 | one has identifier three, 1929 | 1930 | 401 1931 | 00:17:29,082 --> 00:17:29,913 1932 | third one has four. 1933 | 1934 | 402 1935 | 00:17:29,982 --> 00:17:32,583 1936 | Four, okay great, 1937 | here's a pair. 1938 | 1939 | 403 1940 | 00:17:32,652 --> 00:17:34,818 1941 | We shouldn't see another four, 1942 | right? 1943 | 1944 | 404 1945 | 00:17:34,887 --> 00:17:37,688 1946 | There's a one and 1947 | we should see, 1948 | 1949 | 405 1950 | 00:17:37,756 --> 00:17:41,325 1951 | we should see one through six, 1952 | correct? 1953 | 1954 | 406 1955 | 00:17:41,394 --> 00:17:42,660 1956 | And we should see 1957 | each one twice, so 1958 | 1959 | 407 1960 | 00:17:42,729 --> 00:17:44,795 1961 | anyway, that'll be like a way 1962 | to figure that out quickly 1963 | 1964 | 408 1965 | 00:17:44,864 --> 00:17:46,864 1966 | without having go manually and 1967 | print everything out. 1968 | 1969 | 409 1970 | 00:17:46,932 --> 00:17:49,967 1971 | Another thing that we can do 1972 | at any given point is over 1973 | 1974 | 410 1975 | 00:17:50,036 --> 00:17:53,336 1976 | inside the console, LLDB, 1977 | stands for your debugger, 1978 | 1979 | 411 1980 | 00:17:53,405 --> 00:17:55,740 1981 | the DB part stands for 1982 | debugger. 1983 | 1984 | 412 1985 | 00:17:55,808 --> 00:17:58,309 1986 | We can actually print out any 1987 | object that we want to be able 1988 | 1989 | 413 1990 | 00:17:58,378 --> 00:17:59,076 1991 | to inspect it. 1992 | 1993 | 414 1994 | 00:17:59,145 --> 00:18:00,111 1995 | So if I do a PO, 1996 | 1997 | 415 1998 | 00:18:05,518 --> 00:18:06,784 1999 | Give it a moment. 2000 | 2001 | 416 2002 | 00:18:06,853 --> 00:18:10,254 2003 | And you notice it prints out 2004 | this optional concentration, 2005 | 2006 | 417 2007 | 00:18:10,323 --> 00:18:12,990 2008 | because concentration 2009 | is of type optional. 2010 | 2011 | 418 2012 | 00:18:13,059 --> 00:18:14,458 2013 | In this case, it's what's 2014 | called an implicitly 2015 | 2016 | 419 2017 | 00:18:14,527 --> 00:18:15,326 2018 | unwrapped optional. 2019 | 2020 | 420 2021 | 00:18:18,531 --> 00:18:19,897 2022 | In other words, it's not nil. 2023 | 2024 | 421 2025 | 00:18:19,966 --> 00:18:22,132 2026 | The other value in the enum 2027 | for an optional is sum. 2028 | 2029 | 422 2030 | 00:18:22,201 --> 00:18:24,902 2031 | So we know that 2032 | concentration is set here. 2033 | 2034 | 423 2035 | 00:18:24,970 --> 00:18:27,871 2036 | Another thing we can do is 2037 | actually we can print out by 2038 | 2039 | 424 2040 | 00:18:27,940 --> 00:18:30,741 2041 | just typing p for print so 2042 | it just works magic 2043 | 2044 | 425 2045 | 00:18:32,578 --> 00:18:35,379 2046 | abilities to figure how to 2047 | format the thing that we need 2048 | 2049 | 426 2050 | 00:18:35,448 --> 00:18:39,584 2051 | to print out in the way they 2052 | think is the best readable. 2053 | 2054 | 427 2055 | 00:18:39,652 --> 00:18:40,485 2056 | So if I print out game, 2057 | 2058 | 428 2059 | 00:18:43,389 --> 00:18:47,158 2060 | prints out cards, all twelve 2061 | values and then all the info 2062 | 2063 | 429 2064 | 00:18:47,227 --> 00:18:49,126 2065 | that we were just looking 2066 | at inside the debugger. 2067 | 2068 | 430 2069 | 00:18:49,195 --> 00:18:51,495 2070 | But it does it all there 2071 | formatted very nicely. 2072 | 2073 | 431 2074 | 00:18:51,563 --> 00:18:52,729 2075 | And then we know that 2076 | index of one and 2077 | 2078 | 432 2079 | 00:18:52,798 --> 00:18:53,898 2080 | only face up card is nil so 2081 | 2082 | 433 2083 | 00:18:57,303 --> 00:18:58,703 2084 | We can also do that for 2085 | any other object. 2086 | 2087 | 434 2088 | 00:18:58,771 --> 00:19:00,671 2089 | So I print out flipCountLabel, 2090 | 2091 | 435 2092 | 00:19:02,475 --> 00:19:04,542 2093 | it prints out that its 2094 | a UI label optional. 2095 | 2096 | 436 2097 | 00:19:05,878 --> 00:19:09,446 2098 | It's a UI view, 2099 | which has a UI responder. 2100 | 2101 | 437 2102 | 00:19:09,515 --> 00:19:12,483 2103 | And anyway the point is p is 2104 | very nice it's very helpful 2105 | 2106 | 438 2107 | 00:19:12,551 --> 00:19:13,951 2108 | for printing anything 2109 | out whatsoever. 2110 | 2111 | 439 2112 | 00:19:15,187 --> 00:19:17,387 2113 | So, I'm back to our actual 2114 | bug, at this point. 2115 | 2116 | 440 2117 | 00:19:17,456 --> 00:19:20,357 2118 | We have this problem we're 2119 | clicking on, we click 2120 | 2121 | 441 2122 | 00:19:20,426 --> 00:19:22,526 2123 | on a card and we're stopped 2124 | at this break point and 2125 | 2126 | 442 2127 | 00:19:22,595 --> 00:19:24,962 2128 | we wanna figure out why 2129 | this thing is crashing. 2130 | 2131 | 443 2132 | 00:19:25,031 --> 00:19:26,530 2133 | So the first thing 2134 | that we might do 2135 | 2136 | 444 2137 | 00:19:26,599 --> 00:19:29,500 2138 | to figure out what line we're 2139 | crashing on in this case, 2140 | 2141 | 445 2142 | 00:19:29,569 --> 00:19:31,135 2143 | since we're not getting 2144 | an exception and 2145 | 2146 | 446 2147 | 00:19:31,204 --> 00:19:34,271 2148 | we have no idea what's going 2149 | on, is we might go use these 2150 | 2151 | 447 2152 | 00:19:34,340 --> 00:19:36,974 2153 | navigation options inside 2154 | the actual debugger here. 2155 | 2156 | 448 2157 | 00:19:37,042 --> 00:19:38,542 2158 | So if you look in right here, 2159 | 2160 | 449 2161 | 00:19:38,610 --> 00:19:40,811 2162 | we have a few different 2163 | options right here. 2164 | 2165 | 450 2166 | 00:19:40,880 --> 00:19:43,047 2167 | One of them is continue 2168 | program execution and 2169 | 2170 | 451 2171 | 00:19:43,115 --> 00:19:46,083 2172 | what that means is okay we're 2173 | paused at a break point. 2174 | 2175 | 452 2176 | 00:19:46,152 --> 00:19:48,452 2177 | Just continue running the 2178 | program until we hit the next 2179 | 2180 | 453 2181 | 00:19:48,520 --> 00:19:50,054 2182 | break point, or 2183 | if there are no breakpoints, 2184 | 2185 | 454 2186 | 00:19:50,123 --> 00:19:51,255 2187 | just continue 2188 | running the program. 2189 | 2190 | 455 2191 | 00:19:52,391 --> 00:19:53,524 2192 | So let's try that right now, 2193 | 2194 | 456 2195 | 00:19:53,592 --> 00:19:58,128 2196 | let's click, let's click 2197 | on continue running. 2198 | 2199 | 457 2200 | 00:19:58,197 --> 00:19:59,363 2201 | Okay, in this case 2202 | we didn't crash, 2203 | 2204 | 458 2205 | 00:19:59,432 --> 00:20:00,864 2206 | that time that we 2207 | tapped on touch card, 2208 | 2209 | 459 2210 | 00:20:00,933 --> 00:20:02,533 2211 | so it's just continuing 2212 | the program. 2213 | 2214 | 460 2215 | 00:20:02,602 --> 00:20:04,502 2216 | We're not on a break point 2217 | right now, so we click again. 2218 | 2219 | 461 2220 | 00:20:05,871 --> 00:20:07,304 2221 | Okay, we're back at 2222 | the same break point, and 2223 | 2224 | 462 2225 | 00:20:07,373 --> 00:20:08,206 2226 | we can continue again. 2227 | 2228 | 463 2229 | 00:20:09,575 --> 00:20:12,676 2230 | Oh, and that time 2231 | the program crashed, and 2232 | 2233 | 464 2234 | 00:20:12,745 --> 00:20:14,078 2235 | we're not sure 2236 | what's going on. 2237 | 2238 | 465 2239 | 00:20:14,147 --> 00:20:16,313 2240 | But we know that it definitely 2241 | happened right after 2242 | 2243 | 466 2244 | 00:20:16,381 --> 00:20:17,948 2245 | this first line of touchCard. 2246 | 2247 | 467 2248 | 00:20:18,017 --> 00:20:22,019 2249 | So let's see how we can go 2250 | into a little bit more detail. 2251 | 2252 | 468 2253 | 00:20:28,160 --> 00:20:29,693 2254 | >> Okay, so 2255 | we click on the card again. 2256 | 2257 | 469 2258 | 00:20:29,762 --> 00:20:31,996 2259 | Rather than continuing 2260 | the execution, instead, 2261 | 2262 | 470 2263 | 00:20:32,064 --> 00:20:34,165 2264 | we can pick the second option 2265 | here which is step over. 2266 | 2267 | 471 2268 | 00:20:36,269 --> 00:20:38,001 2269 | Oops, saw the little 2270 | help dialog pop up for 2271 | 2272 | 472 2273 | 00:20:38,070 --> 00:20:39,070 2274 | a second there. 2275 | 2276 | 473 2277 | 00:20:39,138 --> 00:20:40,404 2278 | I wanna get that pop 2279 | up on the screen. 2280 | 2281 | 474 2282 | 00:20:43,276 --> 00:20:45,109 2283 | So you can step over 2284 | an instruction. 2285 | 2286 | 475 2287 | 00:20:47,713 --> 00:20:49,713 2288 | Let's maybe do the next 2289 | line of code and 2290 | 2291 | 476 2292 | 00:20:49,782 --> 00:20:52,549 2293 | we can keep going like this 2294 | hoping that this is the time 2295 | 2296 | 477 2297 | 00:20:52,618 --> 00:20:54,619 2298 | where the crush 2299 | actually happens. 2300 | 2301 | 478 2302 | 00:21:00,960 --> 00:21:03,760 2303 | And it's not, unfortunately. 2304 | 2305 | 479 2306 | 00:21:03,829 --> 00:21:06,162 2307 | So actually it doesn't 2308 | help us with our problem. 2309 | 2310 | 480 2311 | 00:21:06,231 --> 00:21:07,331 2312 | Does anyone have 2313 | any other ideas? 2314 | 2315 | 481 2316 | 00:21:11,103 --> 00:21:12,502 2317 | How do you think we can 2318 | go further to figure 2319 | 2320 | 482 2321 | 00:21:12,571 --> 00:21:13,904 2322 | out what the problem 2323 | actually is? 2324 | 2325 | 483 2326 | 00:21:15,441 --> 00:21:17,608 2327 | Take a look at, take a look 2328 | at this method here. 2329 | 2330 | 484 2331 | 00:21:26,718 --> 00:21:28,886 2332 | Well actually, 2333 | it didn't crash that time, 2334 | 2335 | 485 2336 | 00:21:28,955 --> 00:21:30,420 2337 | so that was kind 2338 | of a bad exercise. 2339 | 2340 | 486 2341 | 00:21:30,489 --> 00:21:32,389 2342 | But I'm just saying, 2343 | in general, 2344 | 2345 | 487 2346 | 00:21:32,458 --> 00:21:35,025 2347 | once we hit the time that it 2348 | does crash, what's something 2349 | 2350 | 488 2351 | 00:21:35,094 --> 00:21:37,327 2352 | else that we could do 2353 | to actually go further? 2354 | 2355 | 489 2356 | 00:21:37,396 --> 00:21:40,364 2357 | Any ideas? 2358 | >> Set a break point. 2359 | 2360 | 490 2361 | 00:21:42,568 --> 00:21:43,033 2362 | >> There we go. 2363 | 2364 | 491 2365 | 00:21:43,102 --> 00:21:43,668 2366 | So she just, 2367 | 2368 | 492 2369 | 00:21:45,671 --> 00:21:46,671 2370 | inside concentration. 2371 | 2372 | 493 2373 | 00:21:48,541 --> 00:21:50,974 2374 | we're also calling other 2375 | methods inside of touchCard. 2376 | 2377 | 494 2378 | 00:21:51,043 --> 00:21:52,575 2379 | And then there's another 2380 | method after that. 2381 | 2382 | 495 2383 | 00:21:52,644 --> 00:21:54,611 2384 | There's also 2385 | updateViewFromModel, 2386 | 2387 | 496 2388 | 00:21:54,680 --> 00:21:55,313 2389 | so actually, 2390 | 2391 | 497 2392 | 00:21:58,116 --> 00:21:59,483 2393 | Which means, it can be 2394 | crashing anywhere inside 2395 | 2396 | 498 2397 | 00:21:59,552 --> 00:22:01,985 2398 | the chooseCard, or any where 2399 | inside updateViewFromModel. 2400 | 2401 | 499 2402 | 00:22:02,054 --> 00:22:04,054 2403 | So one thing that we could 2404 | do is we could go to 2405 | 2406 | 500 2407 | 00:22:04,123 --> 00:22:06,256 2408 | both of these methods 2409 | like updateViewFromModel, 2410 | 2411 | 501 2412 | 00:22:06,325 --> 00:22:07,658 2413 | and we can set 2414 | a break point there. 2415 | 2416 | 502 2417 | 00:22:07,726 --> 00:22:12,429 2418 | And then we could pop open a, 2419 | let's pop open card over here, 2420 | 2421 | 503 2422 | 00:22:12,498 --> 00:22:15,199 2423 | and we'll go to chooseCard, 2424 | oh excuse me 2425 | 2426 | 504 2427 | 00:22:15,268 --> 00:22:17,835 2428 | we're go to concentration over 2429 | here and we'll put a break 2430 | 2431 | 505 2432 | 00:22:17,904 --> 00:22:20,604 2433 | point inside choose card and 2434 | now we have three different 2435 | 2436 | 506 2437 | 00:22:20,673 --> 00:22:24,274 2438 | break points set as you can 2439 | see in the break point pane. 2440 | 2441 | 507 2442 | 00:22:24,343 --> 00:22:28,479 2443 | And that'll make it stop at 2444 | all the places where it could 2445 | 2446 | 508 2447 | 00:22:28,547 --> 00:22:30,247 2448 | possibly be at the start 2449 | of the method. 2450 | 2451 | 509 2452 | 00:22:30,315 --> 00:22:32,883 2453 | But another, I'm not gonna do 2454 | that right now, another way of 2455 | 2456 | 510 2457 | 00:22:32,952 --> 00:22:37,888 2458 | doing that actually is to use 2459 | the The next option inside 2460 | 2461 | 511 2462 | 00:22:37,956 --> 00:22:38,989 2463 | the debugger pane. 2464 | 2465 | 512 2466 | 00:22:41,961 --> 00:22:43,260 2467 | So if you click on something 2468 | 2469 | 513 2470 | 00:22:45,064 --> 00:22:46,664 2471 | Let me get back to 2472 | one window here. 2473 | 2474 | 514 2475 | 00:22:48,567 --> 00:22:53,137 2476 | Once we get to a method, 2477 | Like chooseCard, 2478 | 2479 | 515 2480 | 00:22:53,206 --> 00:22:55,572 2481 | we can see this next option 2482 | here is step into and 2483 | 2484 | 516 2485 | 00:22:55,641 --> 00:22:58,074 2486 | that's how we get inside 2487 | the actual method, so 2488 | 2489 | 517 2490 | 00:22:58,143 --> 00:23:01,779 2491 | if I click step into, now we 2492 | are at the top of chooseCard. 2493 | 2494 | 518 2495 | 00:23:01,848 --> 00:23:04,047 2496 | And now if we want we can 2497 | step over each line inside of 2498 | 2499 | 519 2500 | 00:23:04,116 --> 00:23:05,249 2501 | chooseCard. 2502 | 2503 | 520 2504 | 00:23:05,317 --> 00:23:06,683 2505 | And we can inspect 2506 | the code if we want, 2507 | 2508 | 521 2509 | 00:23:06,752 --> 00:23:08,452 2510 | we can read it to make 2511 | sure everything's okay. 2512 | 2513 | 522 2514 | 00:23:10,757 --> 00:23:12,523 2515 | We could put extra break 2516 | points inside here if we see 2517 | 2518 | 523 2519 | 00:23:12,592 --> 00:23:14,125 2520 | something that might 2521 | be suspicious. 2522 | 2523 | 524 2524 | 00:23:15,360 --> 00:23:16,559 2525 | Notice that if we're 2526 | inside a four loop, 2527 | 2528 | 525 2529 | 00:23:16,628 --> 00:23:18,329 2530 | that's a lot of iterations, 2531 | 2532 | 526 2533 | 00:23:18,398 --> 00:23:20,764 2534 | it would take forever to 2535 | step over the whole time. 2536 | 2537 | 527 2538 | 00:23:20,832 --> 00:23:22,399 2539 | So you can get yourself 2540 | in trouble real fast 2541 | 2542 | 528 2543 | 00:23:22,468 --> 00:23:23,833 2544 | if you're stepping 2545 | into methods that 2546 | 2547 | 529 2548 | 00:23:23,902 --> 00:23:25,202 2549 | have a lot of 2550 | behavior going on. 2551 | 2552 | 530 2553 | 00:23:25,270 --> 00:23:27,170 2554 | But that aren't relevant to 2555 | actually what's going on. 2556 | 2557 | 531 2558 | 00:23:27,239 --> 00:23:28,239 2559 | But you get the idea. 2560 | 2561 | 532 2562 | 00:23:29,542 --> 00:23:30,107 2563 | So let's continue, 2564 | 2565 | 533 2566 | 00:23:33,779 --> 00:23:37,782 2567 | Let's, this time let's step 2568 | inside updateViewFromModel. 2569 | 2570 | 534 2571 | 00:23:40,019 --> 00:23:41,852 2572 | So that's the method that's 2573 | right beneath this one. 2574 | 2575 | 535 2576 | 00:23:42,888 --> 00:23:44,287 2577 | And let's step over this. 2578 | 2579 | 536 2580 | 00:23:44,356 --> 00:23:47,324 2581 | We're gonna go through all the 2582 | indices inside cardButtons, 2583 | 2584 | 537 2585 | 00:23:47,393 --> 00:23:49,393 2586 | which is kind of a pain 2587 | because we have to go 2588 | 2589 | 538 2590 | 00:23:49,461 --> 00:23:50,795 2591 | through this big for loop and 2592 | 2593 | 539 2594 | 00:23:50,863 --> 00:23:53,697 2595 | see if anyone sees anything 2596 | suspicious in this method. 2597 | 2598 | 540 2599 | 00:23:56,968 --> 00:23:58,803 2600 | Just point it out, 2601 | point it out if you see it. 2602 | 2603 | 541 2604 | 00:24:00,138 --> 00:24:01,405 2605 | If something weird 2606 | is going on. 2607 | 2608 | 542 2609 | 00:24:05,444 --> 00:24:07,377 2610 | Anybody? 2611 | 2612 | 543 2613 | 00:24:07,446 --> 00:24:11,715 2614 | It's the world's worst 2615 | line of code of all time. 2616 | 2617 | 544 2618 | 00:24:11,784 --> 00:24:12,616 2619 | What is this? 2620 | 2621 | 545 2622 | 00:24:14,286 --> 00:24:14,819 2623 | What is this? 2624 | 2625 | 546 2626 | 00:24:14,887 --> 00:24:15,553 2627 | Who put this here? 2628 | 2629 | 547 2630 | 00:24:17,656 --> 00:24:20,757 2631 | it's information this way 2632 | to exit your app, right? 2633 | 2634 | 548 2635 | 00:24:20,826 --> 00:24:24,160 2636 | So we're just actually 2637 | exiting with negative 1. 2638 | 2639 | 549 2640 | 00:24:24,229 --> 00:24:28,198 2641 | It turns out we're exiting a 2642 | flipCount, which that thing we 2643 | 2644 | 550 2645 | 00:24:28,267 --> 00:24:31,935 2646 | keep in track of at the bottom 2647 | of that is greater than some 2648 | 2649 | 551 2650 | 00:24:32,003 --> 00:24:35,538 2651 | random number that's 2652 | generated between this. 2653 | 2654 | 552 2655 | 00:24:35,607 --> 00:24:36,974 2656 | What the heck? 2657 | This line couldn't just make 2658 | 2659 | 553 2660 | 00:24:37,042 --> 00:24:37,675 2661 | any sense, right? 2662 | 2663 | 554 2664 | 00:24:40,746 --> 00:24:44,348 2665 | debugger because it's like 2666 | it's giving us no information 2667 | 2668 | 555 2669 | 00:24:44,416 --> 00:24:45,349 2670 | in the console so 2671 | 2672 | 556 2673 | 00:24:48,754 --> 00:24:52,022 2674 | Anyway, so obviously this is 2675 | the problem, if we get rid 2676 | 2677 | 557 2678 | 00:24:52,090 --> 00:24:54,658 2679 | of this completely 2680 | pointless in same line of 2681 | 2682 | 558 2683 | 00:24:54,727 --> 00:24:57,995 2684 | code that I put in there, 2685 | then we run the program again 2686 | 2687 | 559 2688 | 00:25:04,437 --> 00:25:06,770 2689 | We're gonna stop at that 2690 | break point, so maybe we'll, 2691 | 2692 | 560 2693 | 00:25:06,839 --> 00:25:08,038 2694 | we'll disable it this time, 2695 | 2696 | 561 2697 | 00:25:08,107 --> 00:25:10,207 2698 | cuz we're not sure if we 2699 | fixed all of the problems. 2700 | 2701 | 562 2702 | 00:25:10,275 --> 00:25:13,010 2703 | So we'll keep that break 2704 | point there just in case, but 2705 | 2706 | 563 2707 | 00:25:13,079 --> 00:25:14,644 2708 | we'll disable it and 2709 | hit continue. 2710 | 2711 | 564 2712 | 00:25:14,713 --> 00:25:16,614 2713 | And then we'll try to 2714 | play concentration, 2715 | 2716 | 565 2717 | 00:25:18,351 --> 00:25:19,850 2718 | See if anything 2719 | strange is going on. 2720 | 2721 | 566 2722 | 00:25:19,919 --> 00:25:22,719 2723 | It actually seems like it's 2724 | working more this time. 2725 | 2726 | 567 2727 | 00:25:22,788 --> 00:25:23,887 2728 | Also, I'm not actually 2729 | playing right now. 2730 | 2731 | 568 2732 | 00:25:23,956 --> 00:25:25,389 2733 | I promise I'm not this 2734 | bad at this game. 2735 | 2736 | 569 2737 | 00:25:27,026 --> 00:25:28,592 2738 | But it looks like we 2739 | fixed the problem. 2740 | 2741 | 570 2742 | 00:25:28,661 --> 00:25:31,128 2743 | And we know we have fixed one 2744 | problem because there's not 2745 | 2746 | 571 2747 | 00:25:31,197 --> 00:25:33,063 2748 | an exit in our code which 2749 | is kinda ridiculous. 2750 | 2751 | 572 2752 | 00:25:33,132 --> 00:25:35,266 2753 | Are there any questions about 2754 | using the actual debugger? 2755 | 2756 | 573 2757 | 00:25:35,334 --> 00:25:35,933 2758 | Are you done here? 2759 | 2760 | 574 2761 | 00:25:38,804 --> 00:25:40,069 2762 | Everyone understand kind 2763 | of the general idea? 2764 | 2765 | 575 2766 | 00:25:40,138 --> 00:25:41,771 2767 | It's actually pretty simple, 2768 | for 2769 | 2770 | 576 2771 | 00:25:41,840 --> 00:25:44,007 2772 | navigating around 2773 | the memory hierarchy and 2774 | 2775 | 577 2776 | 00:25:44,076 --> 00:25:46,476 2777 | then using these commands 2778 | to step through your code. 2779 | 2780 | 578 2781 | 00:25:46,545 --> 00:25:47,177 2782 | That's all make sense? 2783 | 2784 | 579 2785 | 00:25:48,514 --> 00:25:50,747 2786 | Okay cool, one thing I 2787 | wanna show you is that 2788 | 2789 | 580 2790 | 00:25:50,816 --> 00:25:52,882 2791 | the debugger is not quite as 2792 | simple as everything that just 2793 | 2794 | 581 2795 | 00:25:52,951 --> 00:25:54,485 2796 | appears the pane right here. 2797 | 2798 | 582 2799 | 00:25:54,553 --> 00:25:56,520 2800 | It turns out that there's 2801 | something inside of Xcode's 2802 | 2803 | 583 2804 | 00:25:56,589 --> 00:25:59,256 2805 | preferences called behaviors, 2806 | and this is an Xcode 2807 | 2808 | 584 2809 | 00:25:59,325 --> 00:26:02,025 2810 | wide thing, it's actually 2811 | really useful and cool. 2812 | 2813 | 585 2814 | 00:26:02,094 --> 00:26:04,895 2815 | And what behaviors is for 2816 | various parts of running your 2817 | 2818 | 586 2819 | 00:26:04,963 --> 00:26:08,265 2820 | app, and 2821 | actually gives you the ability 2822 | 2823 | 587 2824 | 00:26:08,333 --> 00:26:11,835 2825 | to make custom configurations 2826 | for when certain events occur. 2827 | 2828 | 588 2829 | 00:26:11,904 --> 00:26:13,637 2830 | You can say if this 2831 | thing happens, 2832 | 2833 | 589 2834 | 00:26:13,705 --> 00:26:15,138 2835 | then do this thing to x code. 2836 | 2837 | 590 2838 | 00:26:15,207 --> 00:26:18,341 2839 | So for example, right now 2840 | when we pause inside, 2841 | 2842 | 591 2843 | 00:26:18,410 --> 00:26:21,311 2844 | when the app is running, 2845 | whenever we pause, 2846 | 2847 | 592 2848 | 00:26:21,380 --> 00:26:25,215 2849 | we have this option here that 2850 | says, show the navigator. 2851 | 2852 | 593 2853 | 00:26:26,685 --> 00:26:27,684 2854 | We have that checked. 2855 | 2856 | 594 2857 | 00:26:27,753 --> 00:26:30,387 2858 | So if we change this to hide, 2859 | for example, 2860 | 2861 | 595 2862 | 00:26:30,456 --> 00:26:33,524 2863 | then when the app pauses, 2864 | watch what happens. 2865 | 2866 | 596 2867 | 00:26:37,896 --> 00:26:39,129 2868 | So you're gonna 2869 | touch on the card. 2870 | 2871 | 597 2872 | 00:26:40,666 --> 00:26:43,533 2873 | Did you notice that 2874 | the navigator pane on the left 2875 | 2876 | 598 2877 | 00:26:43,602 --> 00:26:45,635 2878 | disappeared when 2879 | that happened? 2880 | 2881 | 599 2882 | 00:26:45,704 --> 00:26:46,370 2883 | So that's interesting. 2884 | 2885 | 600 2886 | 00:26:48,640 --> 00:26:51,041 2887 | we're debugging, we might want 2888 | Xcode to just, by default, 2889 | 2890 | 601 2891 | 00:26:51,110 --> 00:26:53,410 2892 | just go to a certain state cuz 2893 | we don't know whenever we're 2894 | 2895 | 602 2896 | 00:26:53,479 --> 00:26:55,712 2897 | running the app what state our 2898 | Xcode is currently in, but 2899 | 2900 | 603 2901 | 00:26:55,781 --> 00:26:57,647 2902 | we know that when we're 2903 | debugging we want it to be 2904 | 2905 | 604 2906 | 00:26:57,716 --> 00:26:58,782 2907 | in particular state. 2908 | 2909 | 605 2910 | 00:26:58,851 --> 00:27:00,384 2911 | So you can actually go 2912 | customize all that stuff 2913 | 2914 | 606 2915 | 00:27:00,452 --> 00:27:01,652 2916 | inside behaviors. 2917 | 2918 | 607 2919 | 00:27:01,720 --> 00:27:03,987 2920 | In the interests of time and 2921 | sanity, I won't go through 2922 | 2923 | 608 2924 | 00:27:04,056 --> 00:27:05,455 2925 | every single little option, 2926 | like we could play 2927 | 2928 | 609 2929 | 00:27:05,524 --> 00:27:07,758 2930 | a sound when certain 2931 | things happen or whatever. 2932 | 2933 | 610 2934 | 00:27:07,827 --> 00:27:09,560 2935 | You can actually go through 2936 | the Behaviors pane and 2937 | 2938 | 611 2939 | 00:27:09,628 --> 00:27:12,862 2940 | kinda check and see everything 2941 | that it can do for you. 2942 | 2943 | 612 2944 | 00:27:12,931 --> 00:27:14,932 2945 | So it's actually 2946 | pretty useful. 2947 | 2948 | 613 2949 | 00:27:15,000 --> 00:27:15,765 2950 | Any questions about behaviors? 2951 | 2952 | 614 2953 | 00:27:15,834 --> 00:27:16,467 2954 | Does that make sense? 2955 | 2956 | 615 2957 | 00:27:19,638 --> 00:27:20,571 2958 | Okay, great. 2959 | 2960 | 616 2961 | 00:27:23,709 --> 00:27:26,777 2962 | a little bit about what 2963 | actually the Xcode file itself 2964 | 2965 | 617 2966 | 00:27:26,845 --> 00:27:29,479 2967 | contains, because it's kind of 2968 | opaqued people the first time 2969 | 2970 | 618 2971 | 00:27:29,548 --> 00:27:30,413 2972 | they have used Xcode. 2973 | 2974 | 619 2975 | 00:27:30,482 --> 00:27:32,448 2976 | So this is your 2977 | actual project file. 2978 | 2979 | 620 2980 | 00:27:32,517 --> 00:27:35,185 2981 | It has all these different 2982 | tabs inside of it, and 2983 | 2984 | 621 2985 | 00:27:35,254 --> 00:27:38,122 2986 | there's just a lot 2987 | going on inside here. 2988 | 2989 | 622 2990 | 00:27:38,190 --> 00:27:41,291 2991 | In general, I am not going to 2992 | go through every little point, 2993 | 2994 | 623 2995 | 00:27:41,360 --> 00:27:43,927 2996 | but in general, what I would 2997 | encourage you to do is 2998 | 2999 | 624 3000 | 00:27:43,996 --> 00:27:46,797 3001 | understand the difference 3002 | between build settings and 3003 | 3004 | 625 3005 | 00:27:46,866 --> 00:27:49,866 3006 | what goes on inside of build 3007 | settings, and build phases, 3008 | 3009 | 626 3010 | 00:27:49,935 --> 00:27:51,035 3011 | and what that means. 3012 | 3013 | 627 3014 | 00:27:53,672 --> 00:27:56,039 3015 | like figure out what version 3016 | of our programming language 3017 | 3018 | 628 3019 | 00:27:56,107 --> 00:27:57,207 3020 | that we're running with. 3021 | 3022 | 629 3023 | 00:27:57,276 --> 00:27:58,175 3024 | That's something 3025 | that's going to go 3026 | 3027 | 630 3028 | 00:27:58,244 --> 00:27:58,909 3029 | into your build settings. 3030 | 3031 | 631 3032 | 00:28:00,679 --> 00:28:03,780 3033 | So we can either scroll down 3034 | and look for that option like, 3035 | 3036 | 632 3037 | 00:28:03,849 --> 00:28:06,883 3038 | here we find Swift language 3039 | version set to Swift 4.0 or 3040 | 3041 | 633 3042 | 00:28:06,952 --> 00:28:09,586 3043 | you can actually use this 3044 | handy thing coz this menu 3045 | 3046 | 634 3047 | 00:28:09,655 --> 00:28:10,987 3048 | can get kind of large, and 3049 | 3050 | 635 3051 | 00:28:11,056 --> 00:28:14,157 3052 | type something like language, 3053 | and then, okay, great. 3054 | 3055 | 636 3056 | 00:28:14,225 --> 00:28:17,861 3057 | I was just filtering to 3058 | just Swift language. 3059 | 3060 | 637 3061 | 00:28:17,929 --> 00:28:19,863 3062 | So I would encourage you to 3063 | go through build settings, 3064 | 3065 | 638 3066 | 00:28:19,932 --> 00:28:22,465 3067 | and just see what things that 3068 | there are to change and what 3069 | 3070 | 639 3071 | 00:28:22,534 --> 00:28:25,101 3072 | not, and you'll see this, can 3073 | get actually kind of large, 3074 | 3075 | 640 3076 | 00:28:25,170 --> 00:28:27,738 3077 | the more complex your 3078 | Xcode product is. 3079 | 3080 | 641 3081 | 00:28:27,807 --> 00:28:29,406 3082 | Both phases is 3083 | separate from that. 3084 | 3085 | 642 3086 | 00:28:29,475 --> 00:28:32,509 3087 | And the thing that I would 3088 | point you towards here is, 3089 | 3090 | 643 3091 | 00:28:32,578 --> 00:28:33,977 3092 | this is where you see 3093 | all the source files 3094 | 3095 | 644 3096 | 00:28:34,045 --> 00:28:35,179 3097 | that are used inside your app. 3098 | 3099 | 645 3100 | 00:28:38,917 --> 00:28:40,617 3101 | Then there's the second 3102 | section called link 3103 | 3104 | 646 3105 | 00:28:40,686 --> 00:28:42,219 3106 | binary with libraries. 3107 | 3108 | 647 3109 | 00:28:42,287 --> 00:28:43,854 3110 | So that's if you wanna 3111 | actually add a library 3112 | 3113 | 648 3114 | 00:28:43,923 --> 00:28:45,822 3115 | to use to your project which 3116 | we are gonna get into later 3117 | 3118 | 649 3119 | 00:28:45,891 --> 00:28:46,457 3120 | in this quarter. 3121 | 3122 | 650 3123 | 00:28:49,395 --> 00:28:51,495 3124 | besides just putting 3125 | a label or a button or 3126 | 3127 | 651 3128 | 00:28:51,563 --> 00:28:53,730 3129 | any of the stuff that 3130 | comes stock in UI kit. 3131 | 3132 | 652 3133 | 00:28:53,799 --> 00:28:56,567 3134 | Say that we wanted to put like 3135 | a map because we are making 3136 | 3137 | 653 3138 | 00:28:56,635 --> 00:28:59,469 3139 | an app like map something 3140 | like that what we will do is 3141 | 3142 | 654 3143 | 00:28:59,538 --> 00:29:01,872 3144 | add a new library to our 3145 | app and type in map, and 3146 | 3147 | 655 3148 | 00:29:01,940 --> 00:29:04,908 3149 | we find this thing called map 3150 | kit, and then you type add and 3151 | 3152 | 656 3153 | 00:29:04,976 --> 00:29:08,044 3154 | that's where all you're adding 3155 | for frameworks happens. 3156 | 3157 | 657 3158 | 00:29:08,113 --> 00:29:09,880 3159 | And then we see it created 3160 | this new Frameworks folder 3161 | 3162 | 658 3163 | 00:29:09,949 --> 00:29:12,349 3164 | over here, 3165 | it has a MapKit inside of it. 3166 | 3167 | 659 3168 | 00:29:12,417 --> 00:29:15,385 3169 | And now we can go inside any 3170 | file, like ViewController. 3171 | 3172 | 660 3173 | 00:29:15,454 --> 00:29:19,355 3174 | We can go to the top of that 3175 | file and say import MapKit and 3176 | 3177 | 661 3178 | 00:29:19,424 --> 00:29:21,357 3179 | it's there. 3180 | 3181 | 662 3182 | 00:29:21,426 --> 00:29:22,025 3183 | Make sense? 3184 | 3185 | 663 3186 | 00:29:24,129 --> 00:29:25,129 3187 | the Xcode file for now. 3188 | 3189 | 664 3190 | 00:29:28,100 --> 00:29:30,867 3191 | The one caveat I'll talk 3192 | about with the Xcode file is 3193 | 3194 | 665 3195 | 00:29:30,936 --> 00:29:33,670 3196 | if you're using Git or 3197 | any kind of source control, 3198 | 3199 | 666 3200 | 00:29:33,739 --> 00:29:36,406 3201 | you will actually be 3202 | committing the Xcode project 3203 | 3204 | 667 3205 | 00:29:36,474 --> 00:29:39,409 3206 | file itself to the repository 3207 | and imagine that you're 3208 | 3209 | 668 3210 | 00:29:39,478 --> 00:29:42,345 3211 | working on source control 3212 | with other people online. 3213 | 3214 | 669 3215 | 00:29:42,414 --> 00:29:45,048 3216 | If two different people have 3217 | a merged conflict within 3218 | 3219 | 670 3220 | 00:29:45,117 --> 00:29:47,016 3221 | the actual Xcode 3222 | project file itself, 3223 | 3224 | 671 3225 | 00:29:47,085 --> 00:29:49,786 3226 | that's just a big file that 3227 | stores off a bunch of data by 3228 | 3229 | 672 3230 | 00:29:49,855 --> 00:29:52,656 3231 | your Xcode project, that can 3232 | be kind of nasty to learn how 3233 | 3234 | 673 3235 | 00:29:52,725 --> 00:29:54,057 3236 | to merge those conflicts. 3237 | 3238 | 674 3239 | 00:29:54,126 --> 00:29:54,825 3240 | So that means, 3241 | 3242 | 675 3243 | 00:29:56,662 --> 00:29:59,763 3244 | code it might be worth looking 3245 | up online ways to help manage 3246 | 3247 | 676 3248 | 00:29:59,832 --> 00:30:02,032 3249 | the Xcode project file itself 3250 | inside source control. 3251 | 3252 | 677 3253 | 00:30:02,100 --> 00:30:04,367 3254 | Cuz it's not as simple as 3255 | doing source control inside 3256 | 3257 | 678 3258 | 00:30:04,436 --> 00:30:05,302 3259 | an actual code file. 3260 | 3261 | 679 3262 | 00:30:07,406 --> 00:30:09,939 3263 | project file that you'll wanna 3264 | learn about what it actually 3265 | 3266 | 680 3267 | 00:30:10,008 --> 00:30:11,008 3268 | looks like internally. 3269 | 3270 | 681 3271 | 00:30:12,111 --> 00:30:13,209 3272 | So that's all that 3273 | I have planned for 3274 | 3275 | 682 3276 | 00:30:13,278 --> 00:30:14,444 3277 | today does anyone 3278 | have any questions or 3279 | 3280 | 683 3281 | 00:30:14,513 --> 00:30:15,345 3282 | anything that we went over. 3283 | 3284 | 684 3285 | 00:30:15,414 --> 00:30:16,613 3286 | Does all make sense? 3287 | 3288 | 685 3289 | 00:30:18,583 --> 00:30:20,083 3290 | key commands in the debugger, 3291 | yeah sure. 3292 | 3293 | 686 3294 | 00:30:20,152 --> 00:30:23,153 3295 | >> Is it possible to do some 3296 | sort of version control? 3297 | 3298 | 687 3299 | 00:30:23,222 --> 00:30:23,987 3300 | >> Or right sure, so 3301 | 3302 | 688 3303 | 00:30:26,791 --> 00:30:29,526 3304 | So I was just sort of alluding 3305 | to that when I was talking 3306 | 3307 | 689 3308 | 00:30:29,595 --> 00:30:31,861 3309 | about the Xcode 3310 | project file itself. 3311 | 3312 | 690 3313 | 00:30:31,930 --> 00:30:35,098 3314 | The answer is yes, 3315 | you can use source control. 3316 | 3317 | 691 3318 | 00:30:35,166 --> 00:30:37,267 3319 | Most people, pretty much 3320 | everybody these days uses GIT. 3321 | 3322 | 692 3323 | 00:30:37,335 --> 00:30:39,035 3324 | So highly recommend 3325 | you go learn GIT. 3326 | 3327 | 693 3328 | 00:30:39,104 --> 00:30:42,906 3329 | If you dont know GIT, 3330 | you can go online and quickly, 3331 | 3332 | 694 3333 | 00:30:42,975 --> 00:30:44,841 3334 | I would recommend, there's 3335 | something called PRO GIT. 3336 | 3337 | 695 3338 | 00:30:44,910 --> 00:30:49,947 3339 | And that's a free book 3340 | that you can learn online. 3341 | 3342 | 696 3343 | 00:30:50,015 --> 00:30:50,981 3344 | Learn from online. 3345 | 3346 | 697 3347 | 00:30:56,121 --> 00:30:58,788 3348 | Okay, great, so here's 3349 | this book called Pro Git. 3350 | 3351 | 698 3352 | 00:30:58,857 --> 00:31:00,323 3353 | And if you haven't 3354 | used Git before, 3355 | 3356 | 699 3357 | 00:31:00,392 --> 00:31:01,925 3358 | I highly recommend that 3359 | you don't just go off and 3360 | 3361 | 700 3362 | 00:31:01,994 --> 00:31:02,859 3363 | try to use Git. 3364 | 3365 | 701 3366 | 00:31:02,927 --> 00:31:04,695 3367 | I highly recommend 3368 | that you go off and 3369 | 3370 | 702 3371 | 00:31:04,763 --> 00:31:06,563 3372 | you read something 3373 | like Pro Git first. 3374 | 3375 | 703 3376 | 00:31:06,631 --> 00:31:09,265 3377 | So that you actually learn 3378 | what's going on with Git and 3379 | 3380 | 704 3381 | 00:31:09,334 --> 00:31:11,768 3382 | how it represents source 3383 | control under the hood. 3384 | 3385 | 705 3386 | 00:31:11,836 --> 00:31:14,570 3387 | It'll really help you because 3388 | one of the big pain points for 3389 | 3390 | 706 3391 | 00:31:14,639 --> 00:31:17,374 3392 | people, when they are first 3393 | learning source control is you 3394 | 3395 | 707 3396 | 00:31:17,442 --> 00:31:19,843 3397 | learn a few commands for 3398 | how to commit code online and 3399 | 3400 | 708 3401 | 00:31:19,912 --> 00:31:21,311 3402 | how to send it 3403 | to other people. 3404 | 3405 | 709 3406 | 00:31:21,380 --> 00:31:23,580 3407 | But then, when you run into 3408 | a problem, of some sort, and 3409 | 3410 | 710 3411 | 00:31:23,649 --> 00:31:26,616 3412 | you don't know how to 3413 | undo that problem, 3414 | 3415 | 711 3416 | 00:31:26,685 --> 00:31:28,418 3417 | It's really hard to figure out 3418 | what's going on if you don't 3419 | 3420 | 712 3421 | 00:31:28,487 --> 00:31:29,619 3422 | know how to Git 3423 | actually stores things. 3424 | 3425 | 713 3426 | 00:31:29,688 --> 00:31:31,087 3427 | So it's really necessary 3428 | to go through and 3429 | 3430 | 714 3431 | 00:31:31,156 --> 00:31:34,691 3432 | read something like this book 3433 | to understand how Git works. 3434 | 3435 | 715 3436 | 00:31:34,759 --> 00:31:36,059 3437 | And then if you don't 3438 | have a GitHub account, 3439 | 3440 | 716 3441 | 00:31:36,128 --> 00:31:37,660 3442 | go to github.com and 3443 | 3444 | 717 3445 | 00:31:37,729 --> 00:31:39,396 3446 | immediately create 3447 | an account and 3448 | 3449 | 718 3450 | 00:31:39,464 --> 00:31:43,433 3451 | start creating your repository 3452 | and learning all the commands 3453 | 3454 | 719 3455 | 00:31:43,502 --> 00:31:45,135 3456 | that are better explained 3457 | to you inside this book. 3458 | 3459 | 720 3460 | 00:31:45,203 --> 00:31:46,236 3461 | I highly recommend that. 3462 | 3463 | 721 3464 | 00:31:49,241 --> 00:31:50,107 3465 | in particular, 3466 | 3467 | 722 3468 | 00:31:53,044 --> 00:31:58,782 3469 | So I'll do file, new project, 3470 | I'll create a single view 3471 | 3472 | 723 3473 | 00:31:58,851 --> 00:32:04,754 3474 | app,I'll call it test, And 3475 | then you'll see that when I'm 3476 | 3477 | 724 3478 | 00:32:04,823 --> 00:32:06,989 3479 | actually creating the project, 3480 | I'll put in my desktop here. 3481 | 3482 | 725 3483 | 00:32:07,058 --> 00:32:09,025 3484 | It actually has a source 3485 | control option for 3486 | 3487 | 726 3488 | 00:32:09,094 --> 00:32:10,360 3489 | creating a Git repository. 3490 | 3491 | 727 3492 | 00:32:10,428 --> 00:32:12,862 3493 | So actually, when you check 3494 | this it'll, it'll put your 3495 | 3496 | 728 3497 | 00:32:12,931 --> 00:32:15,065 3498 | project with a .git file 3499 | in the actual directory. 3500 | 3501 | 729 3502 | 00:32:15,133 --> 00:32:16,899 3503 | And then you can push 3504 | that online to GitHub. 3505 | 3506 | 730 3507 | 00:32:16,968 --> 00:32:17,968 3508 | So it'll do that 3509 | automatically so 3510 | 3511 | 731 3512 | 00:32:18,037 --> 00:32:18,769 3513 | you don't need to 3514 | do it manually. 3515 | 3516 | 732 3517 | 00:32:22,140 --> 00:32:23,273 3518 | Any other questions? 3519 | 3520 | 733 3521 | 00:32:25,110 --> 00:32:25,609 3522 | No. 3523 | 3524 | 734 3525 | 00:32:27,045 --> 00:32:27,644 3526 | talk to me afterwards. 3527 | 3528 | 735 3529 | 00:32:29,180 --> 00:32:31,114 3530 | Thank you all. 3531 | 3532 | 736 3533 | 00:32:31,182 --> 00:32:35,352 3534 | >> For more please visit 3535 | us at stanford.edu. 3536 | 3537 | -------------------------------------------------------------------------------- /en/Friday Session 2. Github and Source Control Workflow.srt: -------------------------------------------------------------------------------- 1 | 1 2 | 00:00:00,401 --> 00:00:04,803 3 | [MUSIC] 4 | 5 | 2 6 | 00:00:04,872 --> 00:00:10,008 7 | Stanford University. 8 | >> Welcome everyone to 9 | 10 | 3 11 | 00:00:10,077 --> 00:00:14,613 12 | our Friday section of CS193P. 13 | I'm Junjie, one of your TAs. 14 | 15 | 4 16 | 00:00:14,682 --> 00:00:18,249 17 | And today, I'm gonna 18 | talk about the GitHub and 19 | 20 | 5 21 | 00:00:18,318 --> 00:00:23,221 22 | the source control 23 | workflow in Xcode 9. So, 24 | 25 | 6 26 | 00:00:23,290 --> 00:00:28,126 27 | what is source control? 28 | Like have you used like 29 | 30 | 7 31 | 00:00:28,195 --> 00:00:31,029 32 | some source control 33 | systems like GitHub? 34 | 35 | 8 36 | 00:00:31,098 --> 00:00:36,067 37 | Yeah, so as a software 38 | developer, you will like, 39 | 40 | 9 41 | 00:00:36,136 --> 00:00:40,672 42 | almost like it's 43 | a necessary skill set for 44 | 45 | 10 46 | 00:00:40,741 --> 00:00:43,442 47 | you to learn how to use 48 | source control systems. 49 | 50 | 11 51 | 00:00:43,510 --> 00:00:46,611 52 | Like big companies like 53 | Facebook and Google, 54 | 55 | 12 56 | 00:00:46,680 --> 00:00:50,381 57 | they all have their own 58 | source control systems. And 59 | 60 | 13 61 | 00:00:50,450 --> 00:00:54,385 62 | it's very important for you to 63 | master those skills before you 64 | 65 | 14 66 | 00:00:54,454 --> 00:00:58,223 67 | really go into the industry. 68 | So what is source control? 69 | 70 | 15 71 | 00:00:58,291 --> 00:01:03,328 72 | Why should we matter? 73 | Why should we care about it? 74 | 75 | 16 76 | 00:01:03,397 --> 00:01:07,933 77 | It allows you to track your 78 | code changes over time. 79 | 80 | 17 81 | 00:01:08,002 --> 00:01:11,370 82 | You can revert your files 83 | back to a previous state. 84 | 85 | 18 86 | 00:01:11,438 --> 00:01:16,107 87 | You can compare different 88 | versions of code side by side. 89 | 90 | 19 91 | 00:01:16,176 --> 00:01:18,476 92 | And it also makes 93 | it a lot easier for 94 | 95 | 20 96 | 00:01:18,545 --> 00:01:22,481 97 | you to work with 98 | other teammates. And 99 | 100 | 21 101 | 00:01:22,549 --> 00:01:26,884 102 | like when bugs happen, it's 103 | much easier to take it down. 104 | 105 | 22 106 | 00:01:26,953 --> 00:01:31,556 107 | Today, our topic is one of the 108 | biggest source control system 109 | 110 | 23 111 | 00:01:31,625 --> 00:01:36,361 112 | in the world, and 113 | it's built right into Xcode 9. 114 | 115 | 24 116 | 00:01:36,430 --> 00:01:39,697 117 | Xcode 9 has done a lot of 118 | great things to integrate 119 | 120 | 25 121 | 00:01:39,766 --> 00:01:44,670 122 | GitHub to make it 123 | very easy to use. So 124 | 125 | 26 126 | 00:01:44,738 --> 00:01:49,808 127 | I'm gonna try to demo you some 128 | of the basics in Xcode 9, 129 | 130 | 27 131 | 00:01:49,877 --> 00:01:54,446 132 | how to use GitHub. Before 133 | that, you should register 134 | 135 | 28 136 | 00:01:54,515 --> 00:01:59,284 137 | an account in github.com. 138 | The repository, 139 | 140 | 29 141 | 00:01:59,353 --> 00:02:04,690 142 | the public repository or 143 | a repo, is free. And, a repo 144 | 145 | 30 146 | 00:02:04,758 --> 00:02:08,693 147 | is just like a code directory 148 | for you to store the code. 149 | 150 | 31 151 | 00:02:08,762 --> 00:02:12,930 152 | And if you register using 153 | your Stanford email account, 154 | 155 | 32 156 | 00:02:12,999 --> 00:02:17,068 157 | you can get the private 158 | repository for free. I highly, 159 | 160 | 33 161 | 00:02:17,137 --> 00:02:21,072 162 | highly encourage you to use 163 | a private repo to manage 164 | 165 | 34 166 | 00:02:21,141 --> 00:02:24,342 167 | all your assignments, 168 | all your cost projects. 169 | 170 | 35 171 | 00:02:24,411 --> 00:02:28,547 172 | And it's much easier for 173 | you to collaborate with your 174 | 175 | 36 176 | 00:02:28,616 --> 00:02:31,716 177 | teammates, and in- 178 | >> Remind people not to make 179 | 180 | 37 181 | 00:02:31,785 --> 00:02:33,284 182 | it public, though. 183 | >> Yeah, yeah. 184 | 185 | 38 186 | 00:02:33,353 --> 00:02:34,252 187 | >> [CROSSTALK] the public 188 | 189 | 39 190 | 00:02:34,320 --> 00:02:35,020 191 | to private. 192 | >> So 193 | 194 | 40 195 | 00:02:35,088 --> 00:02:39,658 196 | that's why you wanna register 197 | using your Stanford email. 198 | 199 | 41 200 | 00:02:39,727 --> 00:02:41,926 201 | You would never like up, 202 | upload 203 | 204 | 42 205 | 00:02:41,995 --> 00:02:46,164 206 | your assignments onto GitHub 207 | using a public repository cuz 208 | 209 | 43 210 | 00:02:46,233 --> 00:02:50,268 211 | like you don't know who will 212 | just download all the code and 213 | 214 | 44 215 | 00:02:50,337 --> 00:02:55,040 216 | copy, copy out of no place. 217 | So last Friday's session, 218 | 219 | 45 220 | 00:02:55,108 --> 00:02:59,344 221 | Jason also mentioned that 222 | there's a Pro Git ebook. 223 | 224 | 46 225 | 00:02:59,413 --> 00:03:03,181 226 | That's really great for you to 227 | learn some of the basics of 228 | 229 | 47 230 | 00:03:03,250 --> 00:03:06,584 231 | GitHub and the source control, 232 | some of the concepts. 233 | 234 | 48 235 | 00:03:06,653 --> 00:03:09,754 236 | It's a really good 237 | starting point for 238 | 239 | 49 240 | 00:03:09,823 --> 00:03:14,259 241 | you to learn those skills. 242 | I post a link here and 243 | 244 | 50 245 | 00:03:14,327 --> 00:03:19,697 246 | you can like check it out if 247 | you want. So there are three 248 | 249 | 51 250 | 00:03:19,766 --> 00:03:24,069 251 | main components in like Xcode 252 | 9 Source Control Workflow, and 253 | 254 | 52 255 | 00:03:24,138 --> 00:03:27,839 256 | I will go through 257 | each of them briefly. 258 | 259 | 53 260 | 00:03:27,908 --> 00:03:32,410 261 | The first one is pulling and 262 | pushing. For pulling and 263 | 264 | 54 265 | 00:03:32,479 --> 00:03:35,647 266 | pushing is like dealing 267 | with your local changes and 268 | 269 | 55 270 | 00:03:35,716 --> 00:03:39,518 271 | the changes in the remote 272 | repository. So, for 273 | 274 | 56 275 | 00:03:39,586 --> 00:03:40,551 276 | clone and pull, 277 | 278 | 57 279 | 00:03:40,620 --> 00:03:44,522 280 | you can download all the code 281 | from the remote repository to 282 | 283 | 58 284 | 00:03:44,591 --> 00:03:48,860 285 | your own laptop. Xcode 9 286 | actually make it really, 287 | 288 | 59 289 | 00:03:48,929 --> 00:03:53,298 290 | really easy to download 291 | a repository. You just click 292 | 293 | 60 294 | 00:03:53,366 --> 00:03:57,502 295 | the Clone and Download button 296 | and then open it in Xcode. 297 | 298 | 61 299 | 00:03:57,570 --> 00:04:00,571 300 | And Xcode will download 301 | all the changes for 302 | 303 | 62 304 | 00:04:00,640 --> 00:04:02,573 305 | you. It's really nice. And 306 | 307 | 63 308 | 00:04:02,642 --> 00:04:07,112 309 | I will show you how to do that 310 | in a demo. And another way to 311 | 312 | 64 313 | 00:04:07,181 --> 00:04:12,650 314 | do it is to copy out this 315 | GitHub repository address. 316 | 317 | 65 318 | 00:04:12,719 --> 00:04:17,221 319 | And I will also show you how 320 | to do that in a demo. For 321 | 322 | 66 323 | 00:04:17,290 --> 00:04:20,925 324 | the push, it's like you 325 | make some local changes, 326 | 327 | 67 328 | 00:04:20,994 --> 00:04:24,563 329 | you, and you want to push to 330 | the remote so that you can 331 | 332 | 68 333 | 00:04:24,631 --> 00:04:30,469 334 | edit it in another machine or 335 | share it to your friends. 336 | 337 | 69 338 | 00:04:32,606 --> 00:04:37,509 339 | So, every batch of code 340 | changes is called a commit. 341 | 342 | 70 343 | 00:04:37,578 --> 00:04:41,679 344 | The commit it's like 345 | a snapshot of all the code 346 | 347 | 71 348 | 00:04:41,748 --> 00:04:46,784 349 | changes and 350 | also the description message. 351 | 352 | 72 353 | 00:04:46,853 --> 00:04:51,423 354 | It has the author, description 355 | message, and also the time. 356 | 357 | 73 358 | 00:04:51,491 --> 00:04:55,927 359 | I highly encourage you to 360 | make small commits instead of 361 | 362 | 74 363 | 00:04:55,996 --> 00:05:00,732 364 | making a really big commit, 365 | like only one commit for 366 | 367 | 75 368 | 00:05:00,801 --> 00:05:05,303 369 | the repository. That's 370 | like the final finish one. 371 | 372 | 76 373 | 00:05:05,372 --> 00:05:10,875 374 | Because making small commits 375 | makes it much easier to debug. 376 | 377 | 77 378 | 00:05:10,944 --> 00:05:14,279 379 | The rule of thumb is one 380 | feature at a time, and 381 | 382 | 78 383 | 00:05:14,348 --> 00:05:17,449 384 | each commit should 385 | be self-contained. 386 | 387 | 79 388 | 00:05:17,517 --> 00:05:21,653 389 | When bugs happen, you can 390 | easily revert to the previous 391 | 392 | 80 393 | 00:05:21,721 --> 00:05:27,225 394 | state without affecting 395 | the other files. So 396 | 397 | 81 398 | 00:05:27,293 --> 00:05:33,364 399 | here's what the commit panel 400 | looks like in Xcode 9. 401 | 402 | 82 403 | 00:05:33,433 --> 00:05:37,569 404 | You get the commit message 405 | here and also the author, 406 | 407 | 83 408 | 00:05:37,638 --> 00:05:40,304 409 | the branches, 410 | the tags and remotes. 411 | 412 | 84 413 | 00:05:40,373 --> 00:05:44,409 414 | We will go through these 415 | concepts in a demo. And 416 | 417 | 85 418 | 00:05:44,477 --> 00:05:48,113 419 | here's the identifier, 420 | it is the SHA identifier for 421 | 422 | 86 423 | 00:05:48,181 --> 00:05:53,051 424 | this commit. The last 425 | 426 | 87 427 | 00:05:53,119 --> 00:05:57,221 428 | concept I'm introducing 429 | today is the branching. So 430 | 431 | 88 432 | 00:05:57,290 --> 00:06:04,061 433 | each little commits, one after 434 | one, is called a code branch. 435 | 436 | 89 437 | 00:06:04,130 --> 00:06:06,297 438 | In the GitHub repo, 439 | repository, 440 | 441 | 90 442 | 00:06:06,366 --> 00:06:10,435 443 | the most important one is the 444 | master branch. You would wanna 445 | 446 | 91 447 | 00:06:10,503 --> 00:06:13,772 448 | always keep your master 449 | branch clean, bug-free and 450 | 451 | 92 452 | 00:06:13,840 --> 00:06:18,710 453 | stable. And when you want 454 | to work on new features, 455 | 456 | 93 457 | 00:06:18,778 --> 00:06:23,347 458 | you would like to branch 459 | from the master. So 460 | 461 | 94 462 | 00:06:23,416 --> 00:06:28,119 463 | Xcode 9 is really easy for 464 | you to just right-click and 465 | 466 | 95 467 | 00:06:28,188 --> 00:06:32,523 468 | create a new branch. The new 469 | branch will have the same 470 | 471 | 96 472 | 00:06:32,592 --> 00:06:37,595 473 | code as the master. But when 474 | you keep developing your code, 475 | 476 | 97 477 | 00:06:37,664 --> 00:06:43,000 478 | it won't affect the master 479 | branch. And after you finish 480 | 481 | 98 482 | 00:06:43,069 --> 00:06:46,438 483 | developing all your features, 484 | you've unit tested your code, 485 | 486 | 99 487 | 00:06:46,507 --> 00:06:49,841 488 | you can merge it back 489 | to master branch. 490 | 491 | 100 492 | 00:06:49,910 --> 00:06:55,946 493 | I will show you how to do the 494 | branching in this demo. So, 495 | 496 | 101 497 | 00:06:56,015 --> 00:07:01,119 498 | any questions so far? No, 499 | 500 | 102 501 | 00:07:01,187 --> 00:07:06,925 502 | so let's go into the demo. 503 | So let's pull out Xcode. 504 | 505 | 103 506 | 00:07:08,628 --> 00:07:11,896 507 | So before we do the fancy 508 | things in Xcode, 509 | 510 | 104 511 | 00:07:11,964 --> 00:07:15,833 512 | we would like to set up the 513 | GitHub account in Xcode. So 514 | 515 | 105 516 | 00:07:15,902 --> 00:07:21,372 517 | the way to do it as click 518 | on Xcode > Preferences 519 | 520 | 106 521 | 00:07:21,441 --> 00:07:26,077 522 | > Accounts. When you 523 | select the Add button, 524 | 525 | 107 526 | 00:07:26,145 --> 00:07:30,548 527 | you can add GitHub Enterprise. 528 | Enterprise is like, 529 | 530 | 108 531 | 00:07:30,617 --> 00:07:34,419 532 | when you work for a company, 533 | they will have the company 534 | 535 | 109 536 | 00:07:34,488 --> 00:07:38,590 537 | GitHub account. So 538 | I'm gonna select the GitHub. 539 | 540 | 110 541 | 00:07:38,658 --> 00:07:47,165 542 | And I'm using my Stanford 543 | email address. So 544 | 545 | 111 546 | 00:07:47,234 --> 00:07:51,369 547 | we've already signed in. And 548 | before we jump into the code, 549 | 550 | 112 551 | 00:07:51,437 --> 00:07:55,106 552 | we would wanna make sure the 553 | Source Control is setting up 554 | 555 | 113 556 | 00:07:55,175 --> 00:08:00,211 557 | correctly. So, these options 558 | is like, whether you 559 | 560 | 114 561 | 00:08:00,279 --> 00:08:05,850 562 | want Xcode to download 563 | the remote changes for you. 564 | 565 | 115 566 | 00:08:05,919 --> 00:08:11,322 567 | So that you won't need to pull 568 | the changes in command line. 569 | 570 | 116 571 | 00:08:11,391 --> 00:08:14,992 572 | And when you click on the Git 573 | button here, you can set up 574 | 575 | 117 576 | 00:08:15,061 --> 00:08:20,665 577 | some global ignored files. If 578 | you have used GitHub before, 579 | 580 | 118 581 | 00:08:20,734 --> 00:08:24,869 582 | you know that there's 583 | a gitignore file so 584 | 585 | 119 586 | 00:08:24,938 --> 00:08:31,041 587 | that your files won't be 588 | checked by the GitHub system. 589 | 590 | 120 591 | 00:08:31,110 --> 00:08:35,113 592 | So you can set up some global 593 | ignored files here. That means 594 | 595 | 121 596 | 00:08:35,181 --> 00:08:40,652 597 | like .D_Store files won't 598 | be checked by the GitHub. So 599 | 600 | 122 601 | 00:08:40,720 --> 00:08:45,790 602 | let's start by creating 603 | a new Xcode product. 604 | 605 | 123 606 | 00:08:49,529 --> 00:08:54,799 607 | Let's do Single View, 608 | and then GitTutorial. 609 | 610 | 124 611 | 00:08:59,305 --> 00:09:03,174 612 | And it's basically the same 613 | thing as we are doing when 614 | 615 | 125 616 | 00:09:03,243 --> 00:09:06,377 617 | creating the new project for 618 | our homework, 619 | 620 | 126 621 | 00:09:06,446 --> 00:09:10,815 622 | but the difference is that we 623 | wanna select this button. So 624 | 625 | 127 626 | 00:09:10,884 --> 00:09:13,951 627 | this is the mysterious 628 | Source Control button we never 629 | 630 | 128 631 | 00:09:14,020 --> 00:09:15,920 632 | talk about in class. 633 | 634 | 129 635 | 00:09:15,989 --> 00:09:20,591 636 | And this means that Xcode will 637 | initialize a Git repository 638 | 639 | 130 640 | 00:09:20,660 --> 00:09:25,297 641 | for you. So we would select 642 | this button for this section. 643 | 644 | 131 645 | 00:09:26,399 --> 00:09:31,436 646 | Hit Create, and 647 | then we would have 648 | 649 | 132 650 | 00:09:31,504 --> 00:09:36,408 651 | a Git repository, and 652 | 653 | 133 654 | 00:09:36,476 --> 00:09:43,582 655 | we can inspect the Source 656 | Control panel over here. 657 | 658 | 134 659 | 00:09:43,650 --> 00:09:47,485 660 | It's on the top left, 661 | and the second button. 662 | 663 | 135 664 | 00:09:50,056 --> 00:09:53,658 665 | It shows that we are currently 666 | at the master branch. And 667 | 668 | 136 669 | 00:09:53,726 --> 00:09:57,896 670 | the initial commit is just 671 | some free code given by Xcode 672 | 673 | 137 674 | 00:09:57,964 --> 00:10:02,166 675 | that create, initialize, 676 | and then push the code for 677 | 678 | 138 679 | 00:10:02,235 --> 00:10:07,539 680 | you. So, 681 | let's make some change. 682 | 683 | 139 684 | 00:10:09,076 --> 00:10:14,012 685 | Maybe I will delete 686 | this free code. And 687 | 688 | 140 689 | 00:10:14,080 --> 00:10:22,420 690 | then add a print line. 691 | 692 | 141 693 | 00:10:22,489 --> 00:10:27,525 694 | And once I've saved the file, 695 | there's an M appeared here. 696 | 697 | 142 698 | 00:10:27,594 --> 00:10:31,429 699 | That M stands for modified. 700 | Basically, it means that your 701 | 702 | 143 703 | 00:10:31,497 --> 00:10:37,268 704 | local repository have some 705 | uncommitted changes. And 706 | 707 | 144 708 | 00:10:37,337 --> 00:10:41,305 709 | that's also modified in 710 | storyboard. I want to drag our 711 | 712 | 145 713 | 00:10:41,374 --> 00:10:47,645 714 | button. And 715 | 716 | 146 717 | 00:10:47,714 --> 00:10:51,816 718 | put it in the middle. Okay, 719 | 720 | 147 721 | 00:10:51,885 --> 00:10:57,087 722 | let's save the change. So 723 | now I have made some change. 724 | 725 | 148 726 | 00:10:57,156 --> 00:11:00,658 727 | So we wanna commit 728 | those changes. 729 | 730 | 149 731 | 00:11:00,727 --> 00:11:03,828 732 | It's like clicking 733 | the Source Control here, and 734 | 735 | 150 736 | 00:11:03,897 --> 00:11:09,901 737 | then hit Commit. Then 738 | a Version Editor will appear. 739 | 740 | 151 741 | 00:11:09,970 --> 00:11:14,505 742 | And it's really nice because 743 | you can see your change 744 | 745 | 152 746 | 00:11:14,573 --> 00:11:19,110 747 | side by side by clicking the 748 | filename. And it shows that 749 | 750 | 153 751 | 00:11:19,179 --> 00:11:22,780 752 | you've added one line and 753 | you've deleted some code. 754 | 755 | 154 756 | 00:11:22,849 --> 00:11:26,851 757 | And if you click on 758 | the button in the middle, 759 | 760 | 155 761 | 00:11:26,919 --> 00:11:30,554 762 | you can actually don't 763 | commit this specific line or 764 | 765 | 156 766 | 00:11:30,623 --> 00:11:37,962 767 | discards the change. So, let's 768 | also look at the storyboard. 769 | 770 | 157 771 | 00:11:38,030 --> 00:11:41,299 772 | Maybe it's your first time 773 | looking the storyboard. 774 | 775 | 158 776 | 00:11:41,368 --> 00:11:45,603 777 | At this level, we generally 778 | don't like added the file in, 779 | 780 | 159 781 | 00:11:45,671 --> 00:11:49,173 782 | in this level. 783 | It's a XML file, so 784 | 785 | 160 786 | 00:11:49,242 --> 00:11:54,511 787 | it's super complicated. After 788 | you've confirmed that this is 789 | 790 | 161 791 | 00:11:54,580 --> 00:11:58,817 792 | the change you want, you would 793 | enter the commit message here. 794 | 795 | 162 796 | 00:12:00,420 --> 00:12:07,558 797 | Added a button and 798 | a print. Notice that 799 | 800 | 163 801 | 00:12:07,626 --> 00:12:12,096 802 | there's also a Push to remote 803 | button here. But we don't 804 | 805 | 164 806 | 00:12:12,165 --> 00:12:16,767 807 | have a remote now because the 808 | repository only lives locally. 809 | 810 | 165 811 | 00:12:16,836 --> 00:12:19,937 812 | We don't have a remote 813 | repository in the GitHub. 814 | 815 | 166 816 | 00:12:20,006 --> 00:12:25,542 817 | If we have the remote origin, 818 | we can push also to remote. 819 | 820 | 167 821 | 00:12:25,611 --> 00:12:28,879 822 | And I will show you how to add 823 | the remote origin in a second. 824 | 825 | 168 826 | 00:12:28,948 --> 00:12:34,418 827 | So now let's just 828 | commit the files. So 829 | 830 | 169 831 | 00:12:34,487 --> 00:12:37,354 832 | once I do that, 833 | the M disappeared. And 834 | 835 | 170 836 | 00:12:37,423 --> 00:12:41,659 837 | let's go back to 838 | the Source Control Panel. 839 | 840 | 171 841 | 00:12:41,727 --> 00:12:46,764 842 | Look, here's the commit I just 843 | made. And you can inspect 844 | 845 | 172 846 | 00:12:46,833 --> 00:12:51,503 847 | it by clicking it and then 848 | select the button here. And 849 | 850 | 173 851 | 00:12:51,571 --> 00:12:57,007 852 | it shows the author, 853 | the date, the branches, 854 | 855 | 174 856 | 00:12:57,076 --> 00:13:02,079 857 | and also the files. If you 858 | want to inspect the changes, 859 | 860 | 175 861 | 00:13:02,148 --> 00:13:07,051 862 | you can actually click 863 | on the Assistant Editor. 864 | 865 | 176 866 | 00:13:07,119 --> 00:13:10,922 867 | I want my Assistant Editor 868 | on the bottom. And 869 | 870 | 177 871 | 00:13:10,991 --> 00:13:16,427 872 | then when I click the file, 873 | it actually 874 | 875 | 178 876 | 00:13:16,496 --> 00:13:21,232 877 | displays the changes side by 878 | side. You can easily see what 879 | 880 | 179 881 | 00:13:21,301 --> 00:13:26,237 882 | files you've changed in 883 | this specific commit. 884 | 885 | 180 886 | 00:13:32,112 --> 00:13:37,816 887 | And let's make more 888 | changes here, like print, 889 | 890 | 181 891 | 00:13:40,086 --> 00:13:43,621 892 | hello CS193p. 893 | 894 | 182 895 | 00:13:47,393 --> 00:13:51,829 896 | And over to Source Control 897 | button here, I can actually 898 | 899 | 183 900 | 00:13:51,897 --> 00:13:56,734 901 | discard all the changes 902 | in my current repository. 903 | 904 | 184 905 | 00:13:56,802 --> 00:14:01,705 906 | That basically roll back 907 | to the latest commit. But 908 | 909 | 185 910 | 00:14:01,774 --> 00:14:07,544 911 | I will show you how to revert 912 | to several commits ago. 913 | 914 | 186 915 | 00:14:07,613 --> 00:14:11,315 916 | So let's add this 917 | commit first. 918 | 919 | 187 920 | 00:14:11,383 --> 00:14:17,087 921 | Add an, another print. 922 | 923 | 188 924 | 00:14:17,156 --> 00:14:20,625 925 | But we should have 926 | successfully committed because 927 | 928 | 189 929 | 00:14:20,693 --> 00:14:25,095 930 | there's no M over the 931 | ViewController. And say, for 932 | 933 | 190 934 | 00:14:25,164 --> 00:14:30,534 935 | this specific file, I want to 936 | roll back to two commits ago. 937 | 938 | 191 939 | 00:14:30,603 --> 00:14:36,073 940 | I can open this 941 | Source Control button. 942 | 943 | 192 944 | 00:14:36,142 --> 00:14:40,812 945 | And then it's a side by side 946 | comparison between your 947 | 948 | 193 949 | 00:14:40,880 --> 00:14:45,250 950 | local revision and 951 | your commits. So 952 | 953 | 194 954 | 00:14:47,153 --> 00:14:52,990 955 | if I select the commit, 956 | two commits ago, 957 | 958 | 195 959 | 00:14:53,058 --> 00:14:58,495 960 | I can see all my changes. 961 | And I can click on the button 962 | 963 | 196 964 | 00:14:58,564 --> 00:15:03,368 965 | here, if I want to 966 | discard all the changes. 967 | 968 | 197 969 | 00:15:05,204 --> 00:15:10,641 970 | Revert, so 971 | now my local repository 972 | 973 | 198 974 | 00:15:10,710 --> 00:15:15,647 975 | have reverted those changes. 976 | 977 | 199 978 | 00:15:18,584 --> 00:15:25,023 979 | That's commit. Revert. 980 | 981 | 200 982 | 00:15:29,528 --> 00:15:31,495 983 | Like I just mentioned, 984 | 985 | 201 986 | 00:15:31,564 --> 00:15:35,833 987 | all the code now lives 988 | in my local repository, 989 | 990 | 202 991 | 00:15:35,901 --> 00:15:40,971 992 | so there's no way I can share 993 | it to my teammate or work 994 | 995 | 203 996 | 00:15:41,040 --> 00:15:46,076 997 | on the other workstation. And 998 | also if I just lost my laptop, 999 | 1000 | 204 1001 | 00:15:46,145 --> 00:15:49,213 1002 | everything will be gone. 1003 | You certainly don't want that. 1004 | 1005 | 205 1006 | 00:15:49,282 --> 00:15:54,218 1007 | So let's see like how to 1008 | add a remote. Origin for 1009 | 1010 | 206 1011 | 00:15:54,286 --> 00:15:58,322 1012 | this GitTutorial repo. 1013 | And it's super simple here, 1014 | 1015 | 207 1016 | 00:15:58,391 --> 00:16:02,727 1017 | you just click on the Remote, 1018 | and right-click. 1019 | 1020 | 208 1021 | 00:16:02,796 --> 00:16:06,464 1022 | There's a Create GitTutorial 1023 | Remote on GitHub. 1024 | 1025 | 209 1026 | 00:16:08,668 --> 00:16:11,435 1027 | And it allows you 1028 | to select public or 1029 | 1030 | 210 1031 | 00:16:11,504 --> 00:16:15,939 1032 | private repository. So since 1033 | I'm using my Stanford account, 1034 | 1035 | 211 1036 | 00:16:16,008 --> 00:16:18,543 1037 | I can get the free 1038 | private repo. 1039 | 1040 | 212 1041 | 00:16:27,086 --> 00:16:30,788 1042 | Okay, now we have 1043 | a remote origin, and 1044 | 1045 | 213 1046 | 00:16:30,856 --> 00:16:33,724 1047 | let's take a look. So 1048 | 1049 | 214 1050 | 00:16:33,792 --> 00:16:41,198 1051 | here's my 1052 | Stanford GitHub account. 1053 | 1054 | 215 1055 | 00:16:41,267 --> 00:16:45,603 1056 | And there's a GitTutorial 1057 | repository we just created. 1058 | 1059 | 216 1060 | 00:16:47,507 --> 00:16:51,042 1061 | And it has all the files 1062 | we've modified. 1063 | 1064 | 217 1065 | 00:16:54,613 --> 00:16:59,750 1066 | And if you want to work 1067 | in the other workstation, 1068 | 1069 | 218 1070 | 00:16:59,819 --> 00:17:04,989 1071 | you can just clone or download 1072 | and open in Xcode and/or copy 1073 | 1074 | 219 1075 | 00:17:05,058 --> 00:17:10,427 1076 | the address. So before I 1077 | show you how to clone, 1078 | 1079 | 220 1080 | 00:17:10,496 --> 00:17:14,531 1081 | let me show you how to add 1082 | the collaborator. It's super 1083 | 1084 | 221 1085 | 00:17:14,600 --> 00:17:17,668 1086 | useful if you want to work 1087 | with the other teammate. So 1088 | 1089 | 222 1090 | 00:17:17,737 --> 00:17:22,473 1091 | for this specific repository, 1092 | you go to Setting, 1093 | 1094 | 223 1095 | 00:17:23,509 --> 00:17:26,177 1096 | and hit Collaborators. 1097 | 1098 | 224 1099 | 00:17:26,245 --> 00:17:31,282 1100 | And basically, you need to 1101 | know your friend's GitHub ID, 1102 | 1103 | 225 1104 | 00:17:31,350 --> 00:17:34,452 1105 | and you just type it here. 1106 | For demonstration, 1107 | 1108 | 226 1109 | 00:17:34,521 --> 00:17:39,857 1110 | I will add my personal 1111 | account here. And 1112 | 1113 | 227 1114 | 00:17:39,926 --> 00:17:43,761 1115 | GitHub will you send an email 1116 | to your friend and, or 1117 | 1118 | 228 1119 | 00:17:43,830 --> 00:17:46,596 1120 | you can just share 1121 | this invite link. 1122 | 1123 | 229 1124 | 00:17:46,665 --> 00:17:51,502 1125 | Let me copy it here, and then 1126 | sign into my personal account. 1127 | 1128 | 230 1129 | 00:18:05,084 --> 00:18:10,788 1130 | So I click on this link And 1131 | 1132 | 231 1133 | 00:18:10,857 --> 00:18:15,392 1134 | it says that you are invited 1135 | to collaborate. So 1136 | 1137 | 232 1138 | 00:18:15,461 --> 00:18:22,366 1139 | I can accept the invitation. 1140 | Now, 1141 | 1142 | 233 1143 | 00:18:22,434 --> 00:18:28,339 1144 | I have the push access to 1145 | this private repository. 1146 | 1147 | 234 1148 | 00:18:32,278 --> 00:18:36,313 1149 | So another way to create 1150 | the repo is by cloning 1151 | 1152 | 235 1153 | 00:18:36,382 --> 00:18:40,718 1154 | the existing repo 1155 | from the GitHub. Say, 1156 | 1157 | 236 1158 | 00:18:40,787 --> 00:18:45,089 1159 | I have a Concentration 1160 | private repo here. 1161 | 1162 | 237 1163 | 00:18:46,593 --> 00:18:50,327 1164 | And I want to download 1165 | it to my local machine. 1166 | 1167 | 238 1168 | 00:18:50,396 --> 00:18:55,066 1169 | I can just hit Open in Xcode, 1170 | and Yes. 1171 | 1172 | 239 1173 | 00:18:58,070 --> 00:19:02,540 1174 | So all the code were 1175 | downloaded by Xcode. And 1176 | 1177 | 240 1178 | 00:19:02,608 --> 00:19:07,411 1179 | the other way to do it 1180 | is save this address. 1181 | 1182 | 241 1183 | 00:19:07,479 --> 00:19:11,782 1184 | And then, 1185 | 1186 | 242 1187 | 00:19:11,851 --> 00:19:15,619 1188 | in our Xcode welcome page, 1189 | 1190 | 243 1191 | 00:19:15,688 --> 00:19:18,589 1192 | you can select this Clone 1193 | an existing project. 1194 | 1195 | 244 1196 | 00:19:22,261 --> 00:19:29,834 1197 | You can paste in the address, 1198 | and then just clone. 1199 | 1200 | 245 1201 | 00:19:37,409 --> 00:19:39,610 1202 | So Xcode will do 1203 | the exact same thing. 1204 | 1205 | 246 1206 | 00:19:39,678 --> 00:19:42,880 1207 | It's just copying all 1208 | the files from remote. 1209 | 1210 | 247 1211 | 00:19:42,948 --> 00:19:47,585 1212 | The good thing is that in 1213 | the source control panel here, 1214 | 1215 | 248 1216 | 00:19:47,653 --> 00:19:50,254 1217 | you can actually get all 1218 | the code changes for 1219 | 1220 | 249 1221 | 00:19:50,322 --> 00:19:54,258 1222 | the repository. And 1223 | you can also like inspect all 1224 | 1225 | 250 1226 | 00:19:54,327 --> 00:19:59,229 1227 | the changes you've made. 1228 | So for 1229 | 1230 | 251 1231 | 00:19:59,298 --> 00:20:03,968 1232 | this specific repo, actually, 1233 | the master is a fully 1234 | 1235 | 252 1236 | 00:20:04,036 --> 00:20:08,372 1237 | functional version. The, I 1238 | want to like work on the other 1239 | 1240 | 253 1241 | 00:20:08,440 --> 00:20:12,276 1242 | new features that may be, 1243 | I think, is a little risky. So 1244 | 1245 | 254 1246 | 00:20:12,345 --> 00:20:18,749 1247 | I, I can branch from master 1248 | by right-click here. 1249 | 1250 | 255 1251 | 00:20:18,817 --> 00:20:23,688 1252 | And I will call my new branch 1253 | test because I don't have 1254 | 1255 | 256 1256 | 00:20:23,756 --> 00:20:29,092 1257 | better names. 1258 | So my current epository is set 1259 | 1260 | 257 1261 | 00:20:29,161 --> 00:20:33,598 1262 | to test. Now, it has the same 1263 | copy of code as the master. 1264 | 1265 | 258 1266 | 00:20:35,234 --> 00:20:39,870 1267 | Let's work on some 1268 | risky new features. 1269 | 1270 | 259 1271 | 00:20:39,939 --> 00:20:45,542 1272 | So that's to change the color 1273 | of the button. Maybe I 1274 | 1275 | 260 1276 | 00:20:45,611 --> 00:20:49,914 1277 | think blue is too ugly. 1278 | I want to change it to red. 1279 | 1280 | 261 1281 | 00:20:55,287 --> 00:20:57,221 1282 | And I commit this change. 1283 | 1284 | 262 1285 | 00:21:03,395 --> 00:21:06,430 1286 | Notice that because we 1287 | have a remote origin, 1288 | 1289 | 263 1290 | 00:21:06,499 --> 00:21:09,967 1291 | we can actually select 1292 | Push to remote. And 1293 | 1294 | 264 1295 | 00:21:10,035 --> 00:21:14,404 1296 | because the test branch only 1297 | lives locally in my machine, 1298 | 1299 | 265 1300 | 00:21:14,473 --> 00:21:18,475 1301 | actually Xcode can create 1302 | that remote origin test for 1303 | 1304 | 266 1305 | 00:21:18,544 --> 00:21:23,481 1306 | you. So 1307 | we can commit and push. 1308 | 1309 | 267 1310 | 00:21:30,990 --> 00:21:36,493 1311 | So in my current test branch, 1312 | I have this changed. But 1313 | 1314 | 268 1315 | 00:21:36,562 --> 00:21:42,966 1316 | my master branch 1317 | is not affected. 1318 | 1319 | 269 1320 | 00:21:43,035 --> 00:21:46,570 1321 | So, if you want to switch 1322 | back to the master, 1323 | 1324 | 270 1325 | 00:21:46,638 --> 00:21:51,008 1326 | you can right-click on 1327 | master and select Checkout. 1328 | 1329 | 271 1330 | 00:21:55,381 --> 00:21:58,815 1331 | Now my current branch 1332 | is master. And when 1333 | 1334 | 272 1335 | 00:21:58,884 --> 00:22:02,953 1336 | I go back to the storyboard, 1337 | the button is still blue. 1338 | 1339 | 273 1340 | 00:22:06,325 --> 00:22:10,794 1341 | Now say, you've fully tested 1342 | your code, and you're saying 1343 | 1344 | 274 1345 | 00:22:10,863 --> 00:22:15,332 1346 | that the button should be red 1347 | instead of blue in the master. 1348 | 1349 | 275 1350 | 00:22:15,401 --> 00:22:17,634 1351 | You can right-click here, and 1352 | 1353 | 276 1354 | 00:22:17,703 --> 00:22:20,404 1355 | then select Merge 1356 | test into master. 1357 | 1358 | 277 1359 | 00:22:26,111 --> 00:22:30,214 1360 | The version editor is like for 1361 | you to see whether this merge 1362 | 1363 | 278 1364 | 00:22:30,283 --> 00:22:36,553 1365 | is what you want. 1366 | Another thing is that, 1367 | 1368 | 279 1369 | 00:22:36,622 --> 00:22:41,691 1370 | because the storyboard is the 1371 | XML file, you would never want 1372 | 1373 | 280 1374 | 00:22:41,760 --> 00:22:46,330 1375 | to merge the same storyboard 1376 | with the other teammates. 1377 | 1378 | 281 1379 | 00:22:46,399 --> 00:22:49,466 1380 | You can work on different 1381 | storyboards, but 1382 | 1383 | 282 1384 | 00:22:49,535 --> 00:22:54,104 1385 | you would never like want 1386 | to merge the change in 1387 | 1388 | 283 1389 | 00:22:54,173 --> 00:22:59,242 1390 | the same storyboard, because 1391 | that's really, really painful. 1392 | 1393 | 284 1394 | 00:22:59,311 --> 00:23:04,582 1395 | Now we are at the master, 1396 | and we also have the Changed 1397 | 1398 | 285 1399 | 00:23:04,650 --> 00:23:10,054 1400 | button color here. And when 1401 | I go back to the storyboard, 1402 | 1403 | 286 1404 | 00:23:13,425 --> 00:23:17,895 1405 | The button is now red. 1406 | So we've successfully merged 1407 | 1408 | 287 1409 | 00:23:17,963 --> 00:23:23,200 1410 | the change. And 1411 | 1412 | 288 1413 | 00:23:23,269 --> 00:23:27,204 1414 | notice that our current 1415 | master is ahead 1416 | 1417 | 289 1418 | 00:23:27,272 --> 00:23:31,909 1419 | of the original master. 1420 | So another way to push to 1421 | 1422 | 290 1423 | 00:23:31,977 --> 00:23:36,580 1424 | the origin is to click 1425 | Source Control > Push. 1426 | 1427 | 291 1428 | 00:23:44,824 --> 00:23:48,993 1429 | Now all my notes are at 1430 | the same snapshot. 1431 | 1432 | 292 1433 | 00:23:51,364 --> 00:23:54,264 1434 | Another cool thing I 1435 | want to show you is that, 1436 | 1437 | 293 1438 | 00:23:54,333 --> 00:23:57,968 1439 | they make it really easy 1440 | if you want to inspect your 1441 | 1442 | 294 1443 | 00:23:58,036 --> 00:24:02,706 1444 | commits. Like I have 1445 | some commit for button. 1446 | 1447 | 295 1448 | 00:24:02,775 --> 00:24:06,577 1449 | It will show all the commit 1450 | message that should, 1451 | 1452 | 296 1453 | 00:24:06,646 --> 00:24:12,148 1454 | has button in it. And 1455 | you can also filter by author, 1456 | 1457 | 297 1458 | 00:24:12,217 --> 00:24:15,719 1459 | like author is junjie. And 1460 | 1461 | 298 1462 | 00:24:15,788 --> 00:24:20,690 1463 | you can also search last 1464 | 24 hours, last 30 days. 1465 | 1466 | 299 1467 | 00:24:20,759 --> 00:24:24,795 1468 | It's really helpful if you 1469 | want to search your change. 1470 | 1471 | 300 1472 | 00:24:27,166 --> 00:24:32,969 1473 | So that's basically all for 1474 | our demo. 1475 | 1476 | 301 1477 | 00:24:33,038 --> 00:24:38,809 1478 | Do you have any questions? No, 1479 | 1480 | 302 1481 | 00:24:38,877 --> 00:24:44,014 1482 | okay, so that's it for today. 1483 | Thank you for coming, and 1484 | 1485 | 303 1486 | 00:24:44,083 --> 00:24:47,784 1487 | good luck on your assignments. 1488 | >> For more, 1489 | 1490 | 304 1491 | 00:24:47,853 --> 00:24:56,359 1492 | please visit us 1493 | at stanford.edu. 1494 | 1495 | -------------------------------------------------------------------------------- /en/Friday Session 3. Instruments.srt: -------------------------------------------------------------------------------- 1 | 1 2 | 00:00:00,401 --> 00:00:04,769 3 | [MUSIC] 4 | 5 | 2 6 | 00:00:04,838 --> 00:00:09,307 7 | Stanford University. 8 | >> All right welcome class, 9 | 10 | 3 11 | 00:00:09,376 --> 00:00:12,244 12 | everybody, to iPhone 13 | section about instruments. 14 | 15 | 4 16 | 00:00:12,313 --> 00:00:14,212 17 | We're gonna learn today about 18 | instruments which is a tool 19 | 20 | 5 21 | 00:00:14,281 --> 00:00:17,382 22 | that helps you with figuring 23 | how to profile your code, and 24 | 25 | 6 26 | 00:00:17,451 --> 00:00:20,185 27 | profiling means a few 28 | different things. So we'll get 29 | 30 | 7 31 | 00:00:20,254 --> 00:00:22,387 32 | started with a little bit of 33 | background on what it means to 34 | 35 | 8 36 | 00:00:22,455 --> 00:00:24,422 37 | even profile code in the first 38 | place cuz that may not be 39 | 40 | 9 41 | 00:00:24,491 --> 00:00:27,726 42 | something you've done before. 43 | Even outside of Xcode in 44 | 45 | 10 46 | 00:00:27,794 --> 00:00:31,964 47 | general with your programming 48 | career. So, I want you to 49 | 50 | 11 51 | 00:00:32,032 --> 00:00:34,833 52 | rewind back to kinda when 53 | people were building OS' back 54 | 55 | 12 56 | 00:00:34,902 --> 00:00:37,702 57 | in the day and people had all 58 | kinds of questions that they 59 | 60 | 13 61 | 00:00:37,771 --> 00:00:40,806 62 | wanted to ask about their code 63 | as it was running in their OS. 64 | 65 | 14 66 | 00:00:40,874 --> 00:00:43,575 67 | And those questions would 68 | range across things from like 69 | 70 | 15 71 | 00:00:43,644 --> 00:00:46,210 72 | the hard disk to main memory 73 | to the CPU and just to try to 74 | 75 | 16 76 | 00:00:46,279 --> 00:00:49,280 77 | figure out like how the OS was 78 | allocating various resources. 79 | 80 | 17 81 | 00:00:49,349 --> 00:00:51,649 82 | For the disk you might 83 | ask something like, what, 84 | 85 | 18 86 | 00:00:51,718 --> 00:00:54,486 87 | what processes are asking for 88 | a given resource right now and 89 | 90 | 19 91 | 00:00:54,555 --> 00:00:56,989 92 | which command requested that 93 | resource from the OS so 94 | 95 | 20 96 | 00:00:57,057 --> 00:00:59,291 97 | you could figure out whether 98 | there are some bug or 99 | 100 | 21 101 | 00:00:59,360 --> 00:01:00,325 102 | what was going on. 103 | 104 | 22 105 | 00:01:00,394 --> 00:01:02,661 106 | You might have a problem in 107 | memory like there's a leak or 108 | 109 | 23 110 | 00:01:02,730 --> 00:01:03,661 111 | something like that and 112 | 113 | 24 114 | 00:01:03,730 --> 00:01:05,931 115 | you wanna figure out how you 116 | can trace down that leak. 117 | 118 | 25 119 | 00:01:05,999 --> 00:01:08,634 120 | And then very importantly you 121 | might wanna figure out how 122 | 123 | 26 124 | 00:01:08,702 --> 00:01:11,202 125 | long certain processes are 126 | taking on the CPU compared to 127 | 128 | 27 129 | 00:01:11,271 --> 00:01:13,972 130 | other processes or how long a 131 | process is taking in general. 132 | 133 | 28 134 | 00:01:14,040 --> 00:01:16,542 135 | Or, when it spikes to 100% 136 | usage or something like 137 | 138 | 29 139 | 00:01:16,610 --> 00:01:19,143 140 | that so, these are all 141 | really important questions. 142 | 143 | 30 144 | 00:01:19,212 --> 00:01:21,179 145 | But, in order to figure it 146 | out, you'd have to go in and 147 | 148 | 31 149 | 00:01:21,247 --> 00:01:23,915 150 | manually write a bunch of 151 | code, and that's not very fun. 152 | 153 | 32 154 | 00:01:23,984 --> 00:01:26,385 155 | So if somebody came along 156 | around the early 2000s, and 157 | 158 | 33 159 | 00:01:26,454 --> 00:01:29,054 160 | said hey, there must 161 | be a better way. And 162 | 163 | 34 164 | 00:01:29,122 --> 00:01:30,956 165 | so I believe at 166 | Sun Microsystems, 167 | 168 | 35 169 | 00:01:31,024 --> 00:01:33,958 170 | they created this thing called 171 | DTrace, which is a little 172 | 173 | 36 174 | 00:01:34,027 --> 00:01:36,962 175 | lightweight scripting language 176 | that looks kind of like C, so 177 | 178 | 37 179 | 00:01:37,030 --> 00:01:39,398 180 | it fit right in with your 181 | operating systems code. 182 | 183 | 38 184 | 00:01:39,466 --> 00:01:42,233 185 | And DTrace would do is like 186 | you would place this little 187 | 188 | 39 189 | 00:01:42,302 --> 00:01:45,369 190 | things called probes which are 191 | little of code inside your, 192 | 193 | 40 194 | 00:01:45,438 --> 00:01:46,872 195 | inside your bigger system. 196 | 197 | 41 198 | 00:01:46,940 --> 00:01:49,507 199 | And then they would trigger 200 | output about how long it was 201 | 202 | 42 203 | 00:01:49,576 --> 00:01:52,110 204 | taking to run various things 205 | and you could figure how 206 | 207 | 43 208 | 00:01:52,179 --> 00:01:54,913 209 | much memory was being used by 210 | given resource and what not. 211 | 212 | 44 213 | 00:01:54,982 --> 00:01:57,249 214 | And what is great is really, 215 | really expressive which meant 216 | 217 | 45 218 | 00:01:57,318 --> 00:01:59,350 219 | that you could do a lot of 220 | different things with it, 221 | 222 | 46 223 | 00:01:59,419 --> 00:02:01,386 224 | by writing very little code 225 | and you could deploy it to 226 | 227 | 47 228 | 00:02:01,455 --> 00:02:03,422 229 | production no problem because 230 | when it was running in 231 | 232 | 48 233 | 00:02:03,490 --> 00:02:05,423 234 | production mode. It wouldn't 235 | take any hit on the CPU, so 236 | 237 | 49 238 | 00:02:05,492 --> 00:02:07,191 239 | you can just leave 240 | your code in there, 241 | 242 | 50 243 | 00:02:07,260 --> 00:02:10,595 244 | in the live production system. 245 | So, that's a super great step 246 | 247 | 51 248 | 00:02:10,664 --> 00:02:13,265 249 | in the right direction, but I 250 | had a little bit of a problem, 251 | 252 | 52 253 | 00:02:14,601 --> 00:02:16,435 254 | which is that, 255 | for example here, 256 | 257 | 53 258 | 00:02:16,503 --> 00:02:18,703 259 | when you see this one that 260 | compares process times, 261 | 262 | 54 263 | 00:02:18,772 --> 00:02:20,939 264 | you see this one in 265 | the center here is, 266 | 267 | 55 268 | 00:02:21,008 --> 00:02:25,043 269 | is outputting a distributing 270 | of processes as Atsy text. So 271 | 272 | 56 273 | 00:02:25,111 --> 00:02:27,178 274 | it's showing you these little 275 | at signs to say that that, 276 | 277 | 57 278 | 00:02:27,247 --> 00:02:29,447 279 | that process in the center is 280 | taking up half of the overall 281 | 282 | 58 283 | 00:02:29,516 --> 00:02:31,215 284 | time and the other ones 285 | are taking up the rest, 286 | 287 | 59 288 | 00:02:31,284 --> 00:02:32,884 289 | it's not the best GUI. So 290 | 291 | 60 292 | 00:02:32,953 --> 00:02:37,823 293 | you end up with this non-ideal 294 | situation where you kinda got 295 | 296 | 61 297 | 00:02:37,891 --> 00:02:41,826 298 | all this console of output and 299 | we really didn't want that 300 | 301 | 62 302 | 00:02:41,895 --> 00:02:44,595 303 | with Apple when we were 304 | creating, you know Xcode and 305 | 306 | 63 307 | 00:02:44,664 --> 00:02:46,765 308 | IOS because in most cases, 309 | 310 | 64 311 | 00:02:46,833 --> 00:02:48,467 312 | people who are using it 313 | are maybe programming for 314 | 315 | 65 316 | 00:02:48,535 --> 00:02:50,735 317 | the first time. They've 318 | never made apps before and 319 | 320 | 66 321 | 00:02:50,804 --> 00:02:53,204 322 | this is just something that 323 | you don't wanna have to go and 324 | 325 | 67 326 | 00:02:53,273 --> 00:02:57,108 327 | deal with. So, This is kind 328 | of the vision in my head 329 | 330 | 68 331 | 00:02:57,177 --> 00:02:59,644 332 | of DTrace, it's like this very 333 | powerful machine that has way 334 | 335 | 69 336 | 00:02:59,713 --> 00:03:00,645 337 | too many knobs and, and 338 | 339 | 70 340 | 00:03:00,714 --> 00:03:01,946 341 | buttons to get 342 | used to easily and 343 | 344 | 71 345 | 00:03:02,015 --> 00:03:04,649 346 | you have to kinda learn it up 347 | front to be able to use it. 348 | 349 | 72 350 | 00:03:04,718 --> 00:03:07,652 351 | And the solution instead, is 352 | to have something happier and 353 | 354 | 73 355 | 00:03:07,721 --> 00:03:09,888 356 | friendlier like our 357 | friend Eve here, 358 | 359 | 74 360 | 00:03:09,957 --> 00:03:12,257 361 | or Eva, 362 | I forget how you say it. And 363 | 364 | 75 365 | 00:03:12,326 --> 00:03:17,228 366 | the solution is called 367 | Instruments, Instruments, 368 | 369 | 76 370 | 00:03:17,297 --> 00:03:21,800 371 | Instruments. And so what 372 | Instruments is, is it's a GUI, 373 | 374 | 77 375 | 00:03:21,869 --> 00:03:24,469 376 | and it sits on top of DTrace, 377 | that same little programming 378 | 379 | 78 380 | 00:03:24,538 --> 00:03:27,238 381 | language, scripting language 382 | that we just talked about. 383 | 384 | 79 385 | 00:03:27,307 --> 00:03:30,041 386 | And it's built right into 387 | Xcode and so it, it kinda 388 | 389 | 80 390 | 00:03:30,109 --> 00:03:33,078 391 | democratized this idea that 392 | you could do CPU analysis or 393 | 394 | 81 395 | 00:03:33,147 --> 00:03:35,079 396 | memory analysis or 397 | disc analysis and 398 | 399 | 82 400 | 00:03:35,148 --> 00:03:39,317 401 | things like these. And one of 402 | the interesting differences 403 | 404 | 83 405 | 00:03:39,386 --> 00:03:43,154 406 | between instruments and using 407 | something like DTrace is that. 408 | 409 | 84 410 | 00:03:43,223 --> 00:03:45,557 411 | Since it's a GUI we now 412 | can't do all of these, 413 | 414 | 85 415 | 00:03:45,626 --> 00:03:48,427 416 | all of the D Trace tasks that 417 | we would wanna do within one 418 | 419 | 86 420 | 00:03:48,495 --> 00:03:51,863 421 | single window because there're 422 | just too many of them. So 423 | 424 | 87 425 | 00:03:51,932 --> 00:03:54,900 426 | what instruments does instead, 427 | is it splits off 428 | 429 | 88 430 | 00:03:54,968 --> 00:03:58,003 431 | all of all of the thing that 432 | you'd possibly wanna do with 433 | 434 | 89 435 | 00:03:58,071 --> 00:04:00,838 436 | an iOS app with profiling into 437 | lots of different flavors. 438 | 439 | 90 440 | 00:04:00,907 --> 00:04:04,509 441 | So, you see that we have these 442 | things like the activity 443 | 444 | 91 445 | 00:04:04,578 --> 00:04:07,145 446 | monitor, which is, you may be 447 | used to from your actual OS, 448 | 449 | 92 450 | 00:04:07,214 --> 00:04:10,381 451 | which shows you how 452 | much stuff different, 453 | 454 | 93 455 | 00:04:10,450 --> 00:04:12,784 456 | processes are taking up, 457 | how many different resources. 458 | 459 | 94 460 | 00:04:12,852 --> 00:04:14,353 461 | And you might have this thing 462 | called the time profiler, 463 | 464 | 95 465 | 00:04:14,421 --> 00:04:17,489 466 | which tells you how long 467 | it takes for a given 468 | 469 | 96 470 | 00:04:17,557 --> 00:04:21,025 471 | CPU process to do a certain 472 | task or something like that. 473 | 474 | 97 475 | 00:04:21,094 --> 00:04:22,894 476 | But, you can't have all of 477 | them within one window, or 478 | 479 | 98 480 | 00:04:22,963 --> 00:04:24,729 481 | it would just be too much 482 | clutter and too much stuff. 483 | 484 | 99 485 | 00:04:24,798 --> 00:04:27,565 486 | So, the way an instruments 487 | works from a high level is, 488 | 489 | 100 490 | 00:04:27,634 --> 00:04:30,902 491 | you're gonna launch a specific 492 | subpar, sub module like this. 493 | 494 | 101 495 | 00:04:30,971 --> 00:04:32,870 496 | And then you're going to 497 | focus on only that for 498 | 499 | 102 500 | 00:04:32,939 --> 00:04:34,139 501 | the duration of your app, and 502 | 503 | 103 504 | 00:04:34,207 --> 00:04:37,242 505 | that way you get this nice 506 | GUI, that's not cluttered up. 507 | 508 | 104 509 | 00:04:37,310 --> 00:04:40,445 510 | And in particular with these 511 | instruments what we'll focus 512 | 513 | 105 514 | 00:04:40,514 --> 00:04:43,681 515 | on today are the ones that are 516 | most useful for probably 95% 517 | 518 | 106 519 | 00:04:43,750 --> 00:04:46,484 520 | of the use cases that you'd 521 | ever use instruments for 522 | 523 | 107 524 | 00:04:46,553 --> 00:04:48,820 525 | you'll be using these 526 | four instruments. 527 | 528 | 108 529 | 00:04:48,888 --> 00:04:50,922 530 | So you'll notice that four of 531 | them are blue because they 532 | 533 | 109 534 | 00:04:50,991 --> 00:04:53,658 535 | have more to do with CPU 536 | stuff. And then two of them 537 | 538 | 110 539 | 00:04:53,727 --> 00:04:55,694 540 | are orange because they have 541 | more to do with RAM stuff 542 | 543 | 111 544 | 00:04:55,762 --> 00:04:58,162 545 | with memory. So, we'll go 546 | through these four today 547 | 548 | 112 549 | 00:04:58,231 --> 00:05:00,999 550 | starting with the two CPU ones 551 | and then we'll end up talking 552 | 553 | 113 554 | 00:05:01,067 --> 00:05:04,770 555 | about memory stuff with 556 | allocations and leaks. And 557 | 558 | 114 559 | 00:05:04,838 --> 00:05:06,737 560 | the one big takeaway 561 | from today, that I want 562 | 563 | 115 564 | 00:05:06,806 --> 00:05:08,407 565 | you to have is that 566 | instruments is not 567 | 568 | 116 569 | 00:05:08,475 --> 00:05:10,675 570 | some kind of like, nice to 571 | have optional thing that 572 | 573 | 117 574 | 00:05:10,743 --> 00:05:13,511 575 | sometimes people go off and 576 | use if they have a problem. 577 | 578 | 118 579 | 00:05:13,580 --> 00:05:15,780 580 | It's actually very critical, 581 | even if you think you've 582 | 583 | 119 584 | 00:05:15,849 --> 00:05:18,116 585 | created your app properly and 586 | you don't have any problems or 587 | 588 | 120 589 | 00:05:18,185 --> 00:05:20,252 590 | memory leaks or anything 591 | like that. Going through and 592 | 593 | 121 594 | 00:05:20,320 --> 00:05:22,487 595 | profiling it before you deploy 596 | a real production app and 597 | 598 | 122 599 | 00:05:22,556 --> 00:05:24,856 600 | making sure that it's using 601 | memory properly and that it's 602 | 603 | 123 604 | 00:05:24,925 --> 00:05:26,958 605 | not taking too much CPU time 606 | when it shouldn't be and 607 | 608 | 124 609 | 00:05:27,027 --> 00:05:28,560 610 | things like that 611 | that really matters. 612 | 613 | 125 614 | 00:05:28,628 --> 00:05:30,161 615 | So, Instruments is 616 | something that's 617 | 618 | 126 619 | 00:05:30,230 --> 00:05:33,098 620 | a necessary thing to learn if 621 | you're trying to become a real 622 | 623 | 127 624 | 00:05:33,166 --> 00:05:35,400 625 | iOS programmer. So don't 626 | consider this as like this 627 | 628 | 128 629 | 00:05:35,468 --> 00:05:38,269 630 | optional thing that you 631 | may not need some day. So 632 | 633 | 129 634 | 00:05:38,338 --> 00:05:40,371 635 | we're gonna go into right 636 | into a demo here and 637 | 638 | 130 639 | 00:05:40,440 --> 00:05:43,207 640 | we're gonna show a little app, 641 | a little mobile app and it's 642 | 643 | 131 644 | 00:05:43,276 --> 00:05:46,210 645 | gonna have a few, few issues 646 | with it that we're gonna track 647 | 648 | 132 649 | 00:05:46,279 --> 00:05:48,379 650 | down like little detectives 651 | inside of Instruments and 652 | 653 | 133 654 | 00:05:48,448 --> 00:05:50,181 655 | it'll illustrate 656 | how those four 657 | 658 | 134 659 | 00:05:50,250 --> 00:05:54,786 660 | flavors of Instruments work 661 | that I was speaking about. So, 662 | 663 | 135 664 | 00:05:57,724 --> 00:06:00,791 665 | The first thing I'll do 666 | is I'll run this app in 667 | 668 | 136 669 | 00:06:00,860 --> 00:06:03,428 670 | the most basic version of 671 | Instruments and we'll kinda 672 | 673 | 137 674 | 00:06:03,497 --> 00:06:07,232 675 | just give you a little tour of 676 | what this app is doing. And 677 | 678 | 138 679 | 00:06:07,301 --> 00:06:10,568 680 | the idea with Instruments 681 | is that you go up to this 682 | 683 | 139 684 | 00:06:10,637 --> 00:06:13,238 685 | Product menu and 686 | you click on Profile, 687 | 688 | 140 689 | 00:06:13,306 --> 00:06:16,708 690 | which you can access with Cmd 691 | + I. And then when you run 692 | 693 | 141 694 | 00:06:16,777 --> 00:06:19,511 695 | Profile it builds a version 696 | of your app with certain 697 | 698 | 142 699 | 00:06:19,579 --> 00:06:22,147 700 | debugging stuff built in and 701 | pops up in the same so 702 | 703 | 143 704 | 00:06:22,215 --> 00:06:24,950 705 | many flavors thing that I 706 | showed you from earlier. 707 | 708 | 144 709 | 00:06:25,018 --> 00:06:27,252 710 | And then we'll select 711 | the Activity Monitor, 712 | 713 | 145 714 | 00:06:27,320 --> 00:06:29,587 715 | which just as it says 716 | monitor CPU memory disk and 717 | 718 | 146 719 | 00:06:29,656 --> 00:06:32,657 720 | network usage statistics for 721 | processes and the file system. 722 | 723 | 147 724 | 00:06:32,725 --> 00:06:36,895 725 | So just general statistics 726 | about stuff going 727 | 728 | 148 729 | 00:06:36,964 --> 00:06:40,732 730 | on on your system, and 731 | in this case we click Choose. 732 | 733 | 149 734 | 00:06:40,801 --> 00:06:43,734 735 | This is the instruments GUI 736 | that pops up that's gonna show 737 | 738 | 150 739 | 00:06:43,803 --> 00:06:45,236 740 | us all of our statistics. 741 | 742 | 151 743 | 00:06:45,304 --> 00:06:48,172 744 | And in order to run your app 745 | while recording all of these 746 | 747 | 152 748 | 00:06:48,241 --> 00:06:51,342 749 | statistics, you click the 750 | Record button. It's kind of 751 | 752 | 153 753 | 00:06:51,411 --> 00:06:55,413 754 | like using iTunes or something 755 | but for debugging information, 756 | 757 | 154 758 | 00:06:55,482 --> 00:06:58,783 759 | and then we'll pop open 760 | our simulator here. And 761 | 762 | 155 763 | 00:06:58,852 --> 00:07:01,686 764 | first thing I'll do is explain 765 | what this app is just briefly, 766 | 767 | 156 768 | 00:07:01,754 --> 00:07:04,355 769 | the idea is that we're 770 | number theorists here. 771 | 772 | 157 773 | 00:07:04,424 --> 00:07:07,992 774 | And we've got some big prime 775 | number that you see right here 776 | 777 | 158 778 | 00:07:08,061 --> 00:07:10,895 779 | at the top, and we want to 780 | figure out what its prime 781 | 782 | 159 783 | 00:07:10,964 --> 00:07:12,763 784 | factors are. 785 | So since the number's so 786 | 787 | 160 788 | 00:07:12,832 --> 00:07:15,566 789 | big that's gonna take a lot 790 | of CPU time to compute so 791 | 792 | 161 793 | 00:07:15,635 --> 00:07:18,903 794 | let's see what happens when we 795 | try to compute it. And I also 796 | 797 | 162 798 | 00:07:18,972 --> 00:07:21,272 799 | have two options here for 800 | how we're gonna factor this, 801 | 802 | 163 803 | 00:07:21,341 --> 00:07:22,740 804 | one is to do it 805 | the main thread, and 806 | 807 | 164 808 | 00:07:22,809 --> 00:07:24,876 809 | the other one is to do it 810 | on the background thread. 811 | 812 | 165 813 | 00:07:24,944 --> 00:07:27,479 814 | Now the first thing we'll 815 | experience is what Paul talked 816 | 817 | 166 818 | 00:07:27,547 --> 00:07:28,446 819 | about in lecture this 820 | 821 | 167 822 | 00:07:28,515 --> 00:07:31,049 823 | week which is that when you do 824 | things on the main thread, and 825 | 826 | 168 827 | 00:07:31,118 --> 00:07:33,685 828 | they're really CPU intensive, 829 | you actually block the main 830 | 831 | 169 832 | 00:07:33,753 --> 00:07:37,989 833 | thread. So when we click on 834 | this button here, We notice 835 | 836 | 170 837 | 00:07:38,057 --> 00:07:40,358 838 | that the UI is completely 839 | blocked I can't click anywhere 840 | 841 | 171 842 | 00:07:40,426 --> 00:07:42,460 843 | else, because it's off 844 | factoring this number, 845 | 846 | 172 847 | 00:07:42,528 --> 00:07:45,129 848 | figuring out the prime 849 | factors on the main thread. 850 | 851 | 173 852 | 00:07:45,198 --> 00:07:47,164 853 | You'll also notice over here, 854 | we have a list of all 855 | 856 | 174 857 | 00:07:47,233 --> 00:07:49,200 858 | of the processes running 859 | on my machine right now. 860 | 861 | 175 862 | 00:07:49,269 --> 00:07:52,370 863 | Since we're in the simulator 864 | this is my actual Mac Book all 865 | 866 | 176 867 | 00:07:52,439 --> 00:07:54,339 868 | of the processes and 869 | the name of this app 870 | 871 | 177 872 | 00:07:54,408 --> 00:07:56,974 873 | as you can see up here 874 | is instruments demo. So 875 | 876 | 178 877 | 00:07:57,043 --> 00:08:00,011 878 | one of the first things we can 879 | do is instruments demo here is 880 | 881 | 179 882 | 00:08:00,080 --> 00:08:02,847 883 | taking up a lot of 884 | the CPU time in fact, 885 | 886 | 180 887 | 00:08:02,916 --> 00:08:05,717 888 | Twice as much as the total 889 | percentage, because it's off 890 | 891 | 181 892 | 00:08:05,786 --> 00:08:08,352 893 | finding the prime factors 894 | of this large number. And 895 | 896 | 182 897 | 00:08:08,421 --> 00:08:11,656 898 | if we want we can even filter 899 | to get rid of all the noise, 900 | 901 | 183 902 | 00:08:11,724 --> 00:08:14,859 903 | by typing in the name of the 904 | process we want to inspect. 905 | 906 | 184 907 | 00:08:14,928 --> 00:08:18,763 908 | You'll also notice 909 | that there's other 910 | 911 | 185 912 | 00:08:18,831 --> 00:08:20,598 913 | information displayed here 914 | in the activity monitor 915 | 916 | 186 917 | 00:08:20,667 --> 00:08:22,299 918 | like the number of threads 919 | that are currently running. 920 | 921 | 187 922 | 00:08:22,368 --> 00:08:25,970 923 | So since I have this. Forking 924 | on a background thread options 925 | 926 | 188 927 | 00:08:26,039 --> 00:08:28,039 928 | down here at the bottom. 929 | To solve this problem, 930 | 931 | 189 932 | 00:08:28,107 --> 00:08:29,707 933 | I'll go ahead and 934 | click on this a few times, 935 | 936 | 190 937 | 00:08:29,776 --> 00:08:33,911 938 | fork up a bunch of threads. 939 | And 940 | 941 | 191 942 | 00:08:33,980 --> 00:08:36,481 943 | you can see I hover over 944 | here inside instruments for 945 | 946 | 192 947 | 00:08:36,550 --> 00:08:39,784 948 | seeing the number of threads 949 | pop up. Now we have a bunch of 950 | 951 | 193 952 | 00:08:39,852 --> 00:08:42,120 953 | different threads all going 954 | off in the background to try 955 | 956 | 194 957 | 00:08:42,189 --> 00:08:44,288 958 | to figure out these prime 959 | factors all on their own. 960 | 961 | 195 962 | 00:08:44,357 --> 00:08:46,691 963 | And then they're gonna report 964 | back when done. And we'll see 965 | 966 | 196 967 | 00:08:46,759 --> 00:08:48,359 968 | these number of threads 969 | go down when they finish. 970 | 971 | 197 972 | 00:08:48,428 --> 00:08:50,494 973 | So this just kind of gives 974 | you a general sense of what 975 | 976 | 198 977 | 00:08:50,563 --> 00:08:52,897 978 | instruments is like. The 979 | activity monitor you can see, 980 | 981 | 199 982 | 00:08:52,966 --> 00:08:55,132 983 | doesn't give you superfine 984 | grained information. 985 | 986 | 200 987 | 00:08:55,201 --> 00:08:57,402 988 | It kinda just gives you 989 | the high level, like. You know 990 | 991 | 201 992 | 00:08:57,470 --> 00:08:59,537 993 | what the name of the process 994 | is and how much CPU time and 995 | 996 | 202 997 | 00:08:59,606 --> 00:09:02,373 998 | the number of threads and the 999 | memory, just basic stats, but 1000 | 1001 | 203 1002 | 00:09:02,442 --> 00:09:03,941 1003 | it doesn't allow you to 1004 | dig deeper. To do that, 1005 | 1006 | 204 1007 | 00:09:04,010 --> 00:09:05,944 1008 | you have to as I said earlier 1009 | you have to go back and 1010 | 1011 | 205 1012 | 00:09:06,013 --> 00:09:07,612 1013 | run instruments with 1014 | a different flavor, 1015 | 1016 | 206 1017 | 00:09:07,681 --> 00:09:10,215 1018 | so that you can inspect 1019 | that particular thing. 1020 | 1021 | 207 1022 | 00:09:10,283 --> 00:09:15,119 1023 | So, let's do that. 1024 | We'll stop this 1025 | 1026 | 208 1027 | 00:09:15,188 --> 00:09:18,056 1028 | here. And 1029 | you can actually save 1030 | 1031 | 209 1032 | 00:09:18,124 --> 00:09:21,092 1033 | off what's called the trace. 1034 | So the actual record of what 1035 | 1036 | 210 1037 | 00:09:21,161 --> 00:09:22,226 1038 | happened with that 1039 | run of instrument. 1040 | 1041 | 211 1042 | 00:09:22,295 --> 00:09:24,695 1043 | So you can maybe compare 1044 | across multiple runs. Or 1045 | 1046 | 212 1047 | 00:09:24,764 --> 00:09:31,002 1048 | do additional analysis later. 1049 | And we will 1050 | 1051 | 213 1052 | 00:09:31,070 --> 00:09:35,206 1053 | go back to profile. And the 1054 | next time we want to profile 1055 | 1056 | 214 1057 | 00:09:35,275 --> 00:09:37,842 1058 | on what's actually taking so 1059 | long to factor this prime 1060 | 1061 | 215 1062 | 00:09:37,911 --> 00:09:40,344 1063 | number? Maybe there's some way 1064 | we can optimize this algorithm 1065 | 1066 | 216 1067 | 00:09:40,413 --> 00:09:43,047 1068 | to make it faster for example 1069 | and we've looked at our code 1070 | 1071 | 217 1072 | 00:09:43,116 --> 00:09:46,117 1073 | a bunch. They aren't quite 1074 | sure what's going on. 1075 | 1076 | 218 1077 | 00:09:46,186 --> 00:09:48,453 1078 | Why is it taking so long to 1079 | factor this prime number? 1080 | 1081 | 219 1082 | 00:09:48,522 --> 00:09:51,022 1083 | And so we don't have a theory 1084 | we're not sure why but 1085 | 1086 | 220 1087 | 00:09:51,091 --> 00:09:52,823 1088 | we know figure on 1089 | the time profiler. 1090 | 1091 | 221 1092 | 00:09:52,892 --> 00:09:55,159 1093 | Maybe it will help us dig in 1094 | to exactly what's taken so 1095 | 1096 | 222 1097 | 00:09:55,228 --> 00:09:58,363 1098 | much time on the CPU. And like 1099 | of us more algorithmic insight 1100 | 1101 | 223 1102 | 00:09:58,432 --> 00:10:01,165 1103 | into what we're doing wrong, 1104 | with our, with our 1105 | 1106 | 224 1107 | 00:10:01,234 --> 00:10:04,268 1108 | factorization methods. So 1109 | we'll run the time profiler. 1110 | 1111 | 225 1112 | 00:10:04,337 --> 00:10:06,404 1113 | Okay, so here we are in 1114 | the time profiler, and 1115 | 1116 | 226 1117 | 00:10:06,472 --> 00:10:08,873 1118 | we can see we have this nice 1119 | graph at the top that says, 1120 | 1121 | 227 1122 | 00:10:08,942 --> 00:10:10,441 1123 | CPU usage, and 1124 | it's also showing 1125 | 1126 | 228 1127 | 00:10:10,510 --> 00:10:13,010 1128 | us how much usage in 1129 | the main thread, versus this 1130 | 1131 | 229 1132 | 00:10:13,079 --> 00:10:15,880 1133 | dispatch worker thread that's 1134 | running in the background and 1135 | 1136 | 230 1137 | 00:10:15,948 --> 00:10:20,051 1138 | these other threads. And like 1139 | earlier, we'll notice that 1140 | 1141 | 231 1142 | 00:10:20,120 --> 00:10:22,620 1143 | when we click do it on 1144 | the main thread, and we block 1145 | 1146 | 232 1147 | 00:10:22,689 --> 00:10:26,557 1148 | the UI here, we see that the 1149 | CP usage skyrockets. So you'll 1150 | 1151 | 233 1152 | 00:10:26,626 --> 00:10:29,127 1153 | got this long running process. 1154 | And if we wait long enough, 1155 | 1156 | 234 1157 | 00:10:29,196 --> 00:10:31,395 1158 | eventually we should see 1159 | the factorization complete. 1160 | 1161 | 235 1162 | 00:10:31,464 --> 00:10:33,931 1163 | And that CP usage should 1164 | drop back down to zero. 1165 | 1166 | 236 1167 | 00:10:34,000 --> 00:10:36,667 1168 | So this is the general idea 1169 | behind the time profiler. 1170 | 1171 | 237 1172 | 00:10:36,736 --> 00:10:38,102 1173 | Also at the bottom 1174 | while this is going on, 1175 | 1176 | 238 1177 | 00:10:38,171 --> 00:10:40,872 1178 | you'll see that we have this 1179 | sort of call trace here. 1180 | 1181 | 239 1182 | 00:10:40,940 --> 00:10:45,142 1183 | This call stack, which is 1184 | actually it's a tree of calls. 1185 | 1186 | 240 1187 | 00:10:45,211 --> 00:10:47,846 1188 | And it shows you what's going 1189 | on nested inside all of 1190 | 1191 | 241 1192 | 00:10:49,049 --> 00:10:51,215 1193 | this apps different threads. 1194 | 1195 | 242 1196 | 00:10:51,284 --> 00:10:54,151 1197 | So if you'll notice we're 1198 | done factoring here. 1199 | 1200 | 243 1201 | 00:10:54,220 --> 00:10:56,888 1202 | We've found all the prime 1203 | factors of this number. And 1204 | 1205 | 244 1206 | 00:10:58,925 --> 00:11:03,127 1207 | it took a 99% of the CPU time 1208 | within the main thread and 1209 | 1210 | 245 1211 | 00:11:03,196 --> 00:11:06,464 1212 | 99.3 within the start method. 1213 | And then so on and so forth. 1214 | 1215 | 246 1216 | 00:11:06,532 --> 00:11:09,067 1217 | And so it's showing us the 1218 | percentage of total CPU time, 1219 | 1220 | 247 1221 | 00:11:09,135 --> 00:11:12,970 1222 | that's happening within 1223 | all this different stuff. 1224 | 1225 | 248 1226 | 00:11:13,039 --> 00:11:14,305 1227 | this is not a very fun process 1228 | to dig through all these 1229 | 1230 | 249 1231 | 00:11:14,306 --> 00:11:15,572 1232 | But obviously, 1233 | 1234 | 250 1235 | 00:11:15,641 --> 00:11:16,507 1236 | system methods right, 1237 | 1238 | 251 1239 | 00:11:16,576 --> 00:11:17,809 1240 | because we didn't even 1241 | write these methods. 1242 | 1243 | 252 1244 | 00:11:17,878 --> 00:11:19,677 1245 | We're just, we're just 1246 | curious about our code. 1247 | 1248 | 253 1249 | 00:11:19,746 --> 00:11:21,846 1250 | All we care about is 1251 | the app what I wrote. 1252 | 1253 | 254 1254 | 00:11:21,915 --> 00:11:23,781 1255 | Where is all the information 1256 | for that stuff? So 1257 | 1258 | 255 1259 | 00:11:23,849 --> 00:11:26,317 1260 | there's a nice set of options 1261 | down here inside this call 1262 | 1263 | 256 1264 | 00:11:26,385 --> 00:11:28,519 1265 | tree menu, so 1266 | if I click on this, 1267 | 1268 | 257 1269 | 00:11:28,588 --> 00:11:31,656 1270 | you'll notice there's already 1271 | this option checked by default 1272 | 1273 | 258 1274 | 00:11:31,724 --> 00:11:34,259 1275 | to separate things by thread 1276 | as we've seen up at the top. 1277 | 1278 | 259 1279 | 00:11:34,327 --> 00:11:37,128 1280 | But another thing we can do 1281 | is hide system libraries. So 1282 | 1283 | 260 1284 | 00:11:37,196 --> 00:11:40,131 1285 | the moment that we click, 1286 | hide system libraries, you'll 1287 | 1288 | 261 1289 | 00:11:40,200 --> 00:11:42,766 1290 | notice that it collapsed all 1291 | of that junk that we were just 1292 | 1293 | 262 1294 | 00:11:42,835 --> 00:11:45,470 1295 | look at into just the stuff 1296 | inside of our application. 1297 | 1298 | 263 1299 | 00:11:45,538 --> 00:11:47,371 1300 | So we see stuff inside of 1301 | our view controller for 1302 | 1303 | 264 1304 | 00:11:47,440 --> 00:11:50,140 1305 | example, like factor on mean 1306 | thread which is the method 1307 | 1308 | 265 1309 | 00:11:50,209 --> 00:11:52,776 1310 | that went off to do factoring 1311 | of this prime number on 1312 | 1313 | 266 1314 | 00:11:52,845 --> 00:11:55,280 1315 | the mean thread as opposed to 1316 | on the background thread. And 1317 | 1318 | 267 1319 | 00:11:55,348 --> 00:11:56,547 1320 | then inside of there, 1321 | 1322 | 268 1323 | 00:11:56,616 --> 00:11:59,183 1324 | there was part of it that 1325 | was taking a lot of time. So 1326 | 1327 | 269 1328 | 00:11:59,252 --> 00:12:02,453 1329 | we'll go dig into our methods 1330 | and we see this prime factors 1331 | 1332 | 270 1333 | 00:12:02,521 --> 00:12:04,355 1334 | method that we've written 1335 | apparently is taking up a lot 1336 | 1337 | 271 1338 | 00:12:04,424 --> 00:12:07,292 1339 | of time. And then there is 1340 | what some method in there 1341 | 1342 | 272 1343 | 00:12:07,360 --> 00:12:10,494 1344 | taking up a quarter of the 1345 | time that's testing a divisor. 1346 | 1347 | 273 1348 | 00:12:10,563 --> 00:12:13,231 1349 | So at some point we we we're 1350 | down to a part of the code 1351 | 1352 | 274 1353 | 00:12:13,300 --> 00:12:14,932 1354 | that seems more granular and 1355 | 1356 | 275 1357 | 00:12:15,001 --> 00:12:17,068 1358 | this is where the actual 1359 | problem is happening. 1360 | 1361 | 276 1362 | 00:12:17,136 --> 00:12:20,371 1363 | And we can actually go into 1364 | code level detail from 1365 | 1366 | 277 1367 | 00:12:20,439 --> 00:12:23,674 1368 | instruments by double 1369 | clicking on that method. So 1370 | 1371 | 278 1372 | 00:12:23,743 --> 00:12:26,544 1373 | the moment I click on that, or 1374 | thrown into our code base, and 1375 | 1376 | 279 1377 | 00:12:26,613 --> 00:12:29,013 1378 | we look at this, we have 1379 | on the right side here, 1380 | 1381 | 280 1382 | 00:12:29,082 --> 00:12:32,016 1383 | we have different percentages 1384 | of what lines were taking so 1385 | 1386 | 281 1387 | 00:12:32,085 --> 00:12:34,886 1388 | long to run. So if we're 1389 | looking at our algorithm here 1390 | 1391 | 282 1392 | 00:12:34,954 --> 00:12:39,357 1393 | for factoring prime numbers, 1394 | this is the overall function 1395 | 1396 | 283 1397 | 00:12:39,426 --> 00:12:41,392 1398 | that computes it. 1399 | We pass in some big integer, 1400 | 1401 | 284 1402 | 00:12:41,461 --> 00:12:44,462 1403 | and or a long long in this 1404 | case just because it's so 1405 | 1406 | 285 1407 | 00:12:44,530 --> 00:12:46,964 1408 | large and then we have 1409 | an enter function here 1410 | 1411 | 286 1412 | 00:12:47,033 --> 00:12:49,433 1413 | called test which is gonna 1414 | test a given divisor. 1415 | 1416 | 287 1417 | 00:12:49,502 --> 00:12:52,770 1418 | To see is that large number 1419 | divisible by this divisor? And 1420 | 1421 | 288 1422 | 00:12:52,838 --> 00:12:56,774 1423 | since that divisor can be 1424 | a divisor multiple times of of 1425 | 1426 | 289 1427 | 00:12:56,843 --> 00:12:59,243 1428 | a given number maybe, 1429 | you know, 1430 | 1431 | 290 1432 | 00:12:59,312 --> 00:13:02,313 1433 | the number 31 that actually is 1434 | 2 times 31 times 31, right? 1435 | 1436 | 291 1437 | 00:13:02,382 --> 00:13:04,448 1438 | So we have this while loop 1439 | here which checks that same 1440 | 1441 | 292 1442 | 00:13:04,517 --> 00:13:08,052 1443 | number multiple times, and 1444 | then appends it to a result 1445 | 1446 | 293 1447 | 00:13:08,121 --> 00:13:10,354 1448 | array which is that thing that 1449 | we were outputting in the app 1450 | 1451 | 294 1452 | 00:13:10,423 --> 00:13:14,525 1453 | earlier. And then what we do 1454 | to figure out the overall 1455 | 1456 | 295 1457 | 00:13:14,594 --> 00:13:17,362 1458 | total set of prime factors 1459 | here in this algorithm is we 1460 | 1461 | 296 1462 | 00:13:17,430 --> 00:13:22,766 1463 | go from i b 2 up by ones 1464 | all the way up to N, 1465 | 1466 | 297 1467 | 00:13:22,835 --> 00:13:25,103 1468 | which is the number. So we're 1469 | checking every number from 2, 1470 | 1471 | 298 1472 | 00:13:25,171 --> 00:13:27,371 1473 | 3, 4, 5, 6 all the way up 1474 | to this large number N. 1475 | 1476 | 299 1477 | 00:13:27,440 --> 00:13:30,207 1478 | And we're testing each of them 1479 | to see whether it's a divisor 1480 | 1481 | 300 1482 | 00:13:30,276 --> 00:13:34,244 1483 | of that number. And what our 1484 | instruments demo here is 1485 | 1486 | 301 1487 | 00:13:34,313 --> 00:13:39,183 1488 | saying is that 43.5% 1489 | of it is taken up, 1490 | 1491 | 302 1492 | 00:13:39,251 --> 00:13:40,952 1493 | I think this line 1494 | should actually 1495 | 1496 | 303 1497 | 00:13:41,021 --> 00:13:43,253 1498 | be referring to the line 1499 | immediately following it, so 1500 | 1501 | 304 1502 | 00:13:43,322 --> 00:13:45,356 1503 | the line that's actually doing 1504 | the stride. So just doing this 1505 | 1506 | 305 1507 | 00:13:45,425 --> 00:13:47,992 1508 | many iterations of a loop is 1509 | taking a lot of time. And 1510 | 1511 | 306 1512 | 00:13:48,060 --> 00:13:50,661 1513 | then inside of here, we're 1514 | doing, we're spending a lot of 1515 | 1516 | 307 1517 | 00:13:50,730 --> 00:13:53,598 1518 | time iterating on these loops. 1519 | So it's just as 1520 | 1521 | 308 1522 | 00:13:53,666 --> 00:13:58,102 1523 | if the total number of 1524 | loops that we're doing 1525 | 1526 | 309 1527 | 00:13:58,171 --> 00:13:59,938 1528 | is just too many. So that 1529 | might give us the insight. 1530 | 1531 | 310 1532 | 00:14:00,006 --> 00:14:01,972 1533 | Maybe we're just trying to 1534 | divide too many numbers, 1535 | 1536 | 311 1537 | 00:14:02,041 --> 00:14:04,509 1538 | right. Which of course we are, 1539 | because we're going all 1540 | 1541 | 312 1542 | 00:14:04,577 --> 00:14:07,879 1543 | the way from two to 1544 | the actual number and itself, 1545 | 1546 | 313 1547 | 00:14:07,947 --> 00:14:09,413 1548 | which is kind of crazy 1549 | if you think about it. 1550 | 1551 | 314 1552 | 00:14:09,482 --> 00:14:12,783 1553 | Because first off, 1554 | if we were to go over 1555 | 1556 | 315 1557 | 00:14:12,852 --> 00:14:15,787 1558 | n divided by 2, that's already 1559 | kind of crazy because we 1560 | 1561 | 316 1562 | 00:14:15,855 --> 00:14:17,488 1563 | already tried all the numbers 1564 | that were less than that. 1565 | 1566 | 317 1567 | 00:14:17,557 --> 00:14:20,391 1568 | So we would have found them 1569 | already by that point. And 1570 | 1571 | 318 1572 | 00:14:20,459 --> 00:14:23,061 1573 | if you think it a little 1574 | bit harder actually, 1575 | 1576 | 319 1577 | 00:14:23,129 --> 00:14:26,096 1578 | the more standard mathematical 1579 | way to solve this problem 1580 | 1581 | 320 1582 | 00:14:26,165 --> 00:14:29,100 1583 | would actually be to only 1584 | go up the square root of N. 1585 | 1586 | 321 1587 | 00:14:29,169 --> 00:14:31,302 1588 | You can think about that from 1589 | a math perspective with like 1590 | 1591 | 322 1592 | 00:14:31,371 --> 00:14:33,371 1593 | a proof by contradiction where 1594 | you you assume that if there 1595 | 1596 | 323 1597 | 00:14:33,440 --> 00:14:35,706 1598 | were some. Prime factor of 1599 | a number that were larger 1600 | 1601 | 324 1602 | 00:14:35,774 --> 00:14:39,110 1603 | than the square root of n 1604 | that that number in order for 1605 | 1606 | 325 1607 | 00:14:39,179 --> 00:14:40,945 1608 | it to be multiplied by another 1609 | number to reach that number 1610 | 1611 | 326 1612 | 00:14:41,013 --> 00:14:43,213 1613 | would need to be larger than 1614 | the square root of n which, 1615 | 1616 | 327 1617 | 00:14:43,282 --> 00:14:45,316 1618 | is a contradiction so 1619 | it doesn't make sense. So 1620 | 1621 | 328 1622 | 00:14:45,385 --> 00:14:49,053 1623 | anyway we have that already 1624 | typed down here for 1625 | 1626 | 329 1627 | 00:14:49,121 --> 00:14:53,991 1628 | you so. I will prove that 1629 | instruments has done it's task 1630 | 1631 | 330 1632 | 00:14:54,060 --> 00:14:58,295 1633 | correctly by, instead of 1634 | iterating over every number, 1635 | 1636 | 331 1637 | 00:14:58,364 --> 00:15:01,665 1638 | doing it by starting 1639 | with the divisor two, 1640 | 1641 | 332 1642 | 00:15:01,734 --> 00:15:05,002 1643 | because that's our 1644 | first prime number, and 1645 | 1646 | 333 1647 | 00:15:05,071 --> 00:15:08,906 1648 | testing two. And then 1649 | starting at the number three, 1650 | 1651 | 334 1652 | 00:15:08,974 --> 00:15:11,809 1653 | because only odd numbers can 1654 | be prime numbers after two. 1655 | 1656 | 335 1657 | 00:15:11,877 --> 00:15:13,877 1658 | And then going up to 1659 | the square root of n by 2, so 1660 | 1661 | 336 1662 | 00:15:13,946 --> 00:15:16,713 1663 | we're gonna try 3 5 7 all the 1664 | way up to the square root of n 1665 | 1666 | 337 1667 | 00:15:16,782 --> 00:15:17,949 1668 | which should be a much 1669 | smaller number. 1670 | 1671 | 338 1672 | 00:15:18,018 --> 00:15:19,250 1673 | And then when we go off and 1674 | 1675 | 339 1676 | 00:15:19,318 --> 00:15:27,591 1677 | we profile this. 1678 | We'll notice that, 1679 | 1680 | 340 1681 | 00:15:27,660 --> 00:15:29,961 1682 | when we click on factor 1683 | on the main thread 1684 | 1685 | 341 1686 | 00:15:30,029 --> 00:15:32,930 1687 | we're done immediately, yay. 1688 | So instruments did its job, 1689 | 1690 | 342 1691 | 00:15:32,999 --> 00:15:34,932 1692 | the time profiler's very 1693 | useful, and it can help you 1694 | 1695 | 343 1696 | 00:15:35,001 --> 00:15:37,101 1697 | find what's taking up 1698 | a lot of your CPU time, 1699 | 1700 | 344 1701 | 00:15:37,170 --> 00:15:40,170 1702 | it's about that simple. You 1703 | can bumble around in all these 1704 | 1705 | 345 1706 | 00:15:40,239 --> 00:15:42,073 1707 | other options inside here to 1708 | see what they afford you. 1709 | 1710 | 346 1711 | 00:15:42,141 --> 00:15:45,476 1712 | But the main ones are the ones 1713 | that I just showed you. So 1714 | 1715 | 347 1716 | 00:15:45,545 --> 00:15:47,978 1717 | let's switch gears for a 1718 | moment. I've shown you kind of 1719 | 1720 | 348 1721 | 00:15:48,047 --> 00:15:54,351 1722 | the two first instruments that 1723 | have to do with CPU time and 1724 | 1725 | 349 1726 | 00:15:54,420 --> 00:15:56,420 1727 | what we'll do instead is start 1728 | looking more into memory. So 1729 | 1730 | 350 1731 | 00:15:56,421 --> 00:15:58,421 1732 | processes. And 1733 | 1734 | 351 1735 | 00:15:58,491 --> 00:16:00,224 1736 | like I mentioned earlier, the 1737 | two that are most useful for 1738 | 1739 | 352 1740 | 00:16:00,293 --> 00:16:03,828 1741 | this are going to be 1742 | allocations and leaks. So 1743 | 1744 | 353 1745 | 00:16:03,896 --> 00:16:05,629 1746 | the simpler of the two 1747 | would be allocations. 1748 | 1749 | 354 1750 | 00:16:05,698 --> 00:16:07,832 1751 | We're just gonna see how much 1752 | memory is getting allocated at 1753 | 1754 | 355 1755 | 00:16:07,900 --> 00:16:11,936 1756 | various times in the program. 1757 | And we will hit Choose and 1758 | 1759 | 356 1760 | 00:16:12,004 --> 00:16:17,041 1761 | Run. We will go over to this 1762 | classroom section here. And 1763 | 1764 | 357 1765 | 00:16:17,110 --> 00:16:19,010 1766 | the first thing that we'll 1767 | notice about allocations, 1768 | 1769 | 358 1770 | 00:16:19,078 --> 00:16:20,844 1771 | which is showing us 1772 | the memory that's being used, 1773 | 1774 | 359 1775 | 00:16:20,913 --> 00:16:22,313 1776 | is that when we 1777 | go off to a new 1778 | 1779 | 360 1780 | 00:16:22,382 --> 00:16:24,048 1781 | view controller, the number, 1782 | 1783 | 361 1784 | 00:16:24,116 --> 00:16:26,384 1785 | the amount of memory being 1786 | used currently sky rockets up 1787 | 1788 | 362 1789 | 00:16:26,452 --> 00:16:28,152 1790 | because we have a stack of 1791 | navigation controllers. 1792 | 1793 | 363 1794 | 00:16:28,221 --> 00:16:29,820 1795 | We have the one that's still 1796 | allocated that's still on 1797 | 1798 | 364 1799 | 00:16:29,889 --> 00:16:31,088 1800 | the stack and 1801 | then we have this new one. 1802 | 1803 | 365 1804 | 00:16:31,157 --> 00:16:33,991 1805 | So we should see this go up. 1806 | And likewise, when we go back 1807 | 1808 | 366 1809 | 00:16:34,059 --> 00:16:36,527 1810 | we should see it go down 1811 | a little bit. And it does. 1812 | 1813 | 367 1814 | 00:16:36,595 --> 00:16:39,864 1815 | So that's great. As we go to 1816 | the classroom controller and 1817 | 1818 | 368 1819 | 00:16:39,932 --> 00:16:41,999 1820 | from it we see allocations 1821 | going up and down. 1822 | 1823 | 369 1824 | 00:16:42,067 --> 00:16:43,701 1825 | Sometimes if you have some 1826 | kind of bug in your app 1827 | 1828 | 370 1829 | 00:16:43,770 --> 00:16:46,403 1830 | you might have it that when 1831 | you go back from a view 1832 | 1833 | 371 1834 | 00:16:46,472 --> 00:16:49,506 1835 | controller. All of a sudden 1836 | you don't see the allocations 1837 | 1838 | 372 1839 | 00:16:49,575 --> 00:16:50,475 1840 | go down, which would be a bug. 1841 | 1842 | 373 1843 | 00:16:50,543 --> 00:16:53,711 1844 | So that's something that 1845 | you might wanna check for. 1846 | 1847 | 374 1848 | 00:16:53,780 --> 00:16:55,779 1849 | And what we'll do 1850 | here in this case is, 1851 | 1852 | 375 1853 | 00:16:55,848 --> 00:16:58,583 1854 | is explain what's going on in 1855 | this tab of the app. And then 1856 | 1857 | 376 1858 | 00:16:58,651 --> 00:17:02,052 1859 | we'll kind of look for, for 1860 | any possible errors. So when 1861 | 1862 | 377 1863 | 00:17:02,121 --> 00:17:06,457 1864 | we click this little button 1865 | here, we will show a hint. And 1866 | 1867 | 378 1868 | 00:17:06,525 --> 00:17:09,393 1869 | the hint is that Jack, this 1870 | friendly student here who is 1871 | 1872 | 379 1873 | 00:17:09,462 --> 00:17:11,028 1874 | an image we fetched 1875 | from online, 1876 | 1877 | 380 1878 | 00:17:11,097 --> 00:17:13,831 1879 | is asking what tips can we use 1880 | to speed up prime fact-finding 1881 | 1882 | 381 1883 | 00:17:13,900 --> 00:17:16,266 1884 | prime factors? Paul says, have 1885 | you considered the domain or 1886 | 1887 | 382 1888 | 00:17:16,335 --> 00:17:17,835 1889 | possible factors? 1890 | Maybe you're trying too many, 1891 | 1892 | 383 1893 | 00:17:17,904 --> 00:17:20,337 1894 | which is the exact bug that we 1895 | went and fixed earlier with 1896 | 1897 | 384 1898 | 00:17:20,406 --> 00:17:23,373 1899 | the time profiling so. We 1900 | noticed that when we clicked 1901 | 1902 | 385 1903 | 00:17:23,442 --> 00:17:26,811 1904 | on Show Hint, this really 1905 | skyrocketed up a lot because 1906 | 1907 | 386 1908 | 00:17:26,880 --> 00:17:28,379 1909 | here we are loading 1910 | an image into the app and 1911 | 1912 | 387 1913 | 00:17:28,448 --> 00:17:29,980 1914 | that contains a lot of data. 1915 | So 1916 | 1917 | 388 1918 | 00:17:30,049 --> 00:17:32,849 1919 | what we might do is just play 1920 | around with all the operations 1921 | 1922 | 389 1923 | 00:17:32,918 --> 00:17:38,389 1924 | we can do inside the app. And 1925 | just see what's going on here. 1926 | 1927 | 390 1928 | 00:17:38,458 --> 00:17:39,823 1929 | And we immediately 1930 | find something kind of 1931 | 1932 | 391 1933 | 00:17:39,892 --> 00:17:42,893 1934 | suspicious which is that every 1935 | time I click Show Hint, we see 1936 | 1937 | 392 1938 | 00:17:42,961 --> 00:17:45,662 1939 | our memory usage is going up, 1940 | but when we unclick it and 1941 | 1942 | 393 1943 | 00:17:45,731 --> 00:17:48,399 1944 | click it again, it doesn't go 1945 | back down. So that seems like 1946 | 1947 | 394 1948 | 00:17:48,467 --> 00:17:51,202 1949 | we've got some kind of problem 1950 | here, seems kinda strange. 1951 | 1952 | 395 1953 | 00:17:51,270 --> 00:17:53,237 1954 | Why would it be going up each 1955 | time you click Show Hint? 1956 | 1957 | 396 1958 | 00:17:53,305 --> 00:17:55,172 1959 | All that happens each time is 1960 | we display an image on the 1961 | 1962 | 397 1963 | 00:17:55,240 --> 00:17:58,042 1964 | screen, and then we put these 1965 | two labels on the screen. 1966 | 1967 | 398 1968 | 00:17:58,111 --> 00:18:01,645 1969 | So why would there not be 1970 | memory being, being freed? 1971 | 1972 | 399 1973 | 00:18:01,714 --> 00:18:02,346 1974 | In this case it's not like, 1975 | 1976 | 400 1977 | 00:18:02,414 --> 00:18:04,482 1978 | not like necessary that 1979 | we have a leak, but 1980 | 1981 | 401 1982 | 00:18:04,550 --> 00:18:06,917 1983 | we're just using too much 1984 | memory for some reason. And so 1985 | 1986 | 402 1987 | 00:18:06,986 --> 00:18:10,454 1988 | let's go off into 1989 | the code here. And 1990 | 1991 | 403 1992 | 00:18:10,523 --> 00:18:12,690 1993 | we're inside this 1994 | ClassroomViewController, 1995 | 1996 | 404 1997 | 00:18:12,758 --> 00:18:17,361 1998 | which I will kinda show how 1999 | it works at the high level. 2000 | 2001 | 405 2002 | 00:18:17,430 --> 00:18:21,632 2003 | It's pretty simple. We have 2004 | this UI switch, which is that 2005 | 2006 | 406 2007 | 00:18:21,701 --> 00:18:24,869 2008 | button at the top that appears 2009 | next to the show hint text. 2010 | 2011 | 407 2012 | 00:18:24,937 --> 00:18:27,838 2013 | And when we tap on it and it's 2014 | on, we fetch this hint. So 2015 | 2016 | 408 2017 | 00:18:27,907 --> 00:18:30,474 2018 | the idea is we're simulating 2019 | doing a number of requests and 2020 | 2021 | 409 2022 | 00:18:30,543 --> 00:18:31,409 2023 | getting this data back for 2024 | 2025 | 410 2026 | 00:18:31,477 --> 00:18:32,710 2027 | this particular 2028 | hint from online. 2029 | 2030 | 411 2031 | 00:18:32,779 --> 00:18:35,045 2032 | And then we're displaying 2033 | it on the screen and 2034 | 2035 | 412 2036 | 00:18:35,114 --> 00:18:39,050 2037 | otherwise we're going to null 2038 | out this stuff that existed. 2039 | 2040 | 413 2041 | 00:18:41,620 --> 00:18:43,888 2042 | So when we call fetch hint, 2043 | what we do is we 2044 | 2045 | 414 2046 | 00:18:45,624 --> 00:18:47,824 2047 | allocate this thing called 2048 | the factorization question, 2049 | 2050 | 415 2051 | 00:18:47,893 --> 00:18:51,395 2052 | which is this class down here 2053 | at the bottom. And we allocate 2054 | 2055 | 416 2056 | 00:18:51,464 --> 00:18:54,165 2057 | a factorization answer which 2058 | is this other class down here 2059 | 2060 | 417 2061 | 00:18:54,234 --> 00:18:55,899 2062 | at the bottom, which has 2063 | text for the answers. 2064 | 2065 | 418 2066 | 00:18:55,968 --> 00:18:58,535 2067 | So these two labels being 2068 | displayed at the bottom. And 2069 | 2070 | 419 2071 | 00:18:58,604 --> 00:19:01,038 2072 | then on the answer itself, 2073 | 2074 | 420 2075 | 00:19:01,107 --> 00:19:03,707 2076 | we give a handle back 2077 | to the question. 2078 | 2079 | 421 2080 | 00:19:03,776 --> 00:19:08,345 2081 | And then inside of 2082 | the question itself, 2083 | 2084 | 422 2085 | 00:19:08,414 --> 00:19:11,515 2086 | we also have an array, which 2087 | is trying to store references 2088 | 2089 | 423 2090 | 00:19:11,584 --> 00:19:13,484 2091 | off to all the answers for 2092 | that particular question. 2093 | 2094 | 424 2095 | 00:19:13,552 --> 00:19:15,953 2096 | In this case, there's just 2097 | one. But we can imagine a data 2098 | 2099 | 425 2100 | 00:19:16,022 --> 00:19:17,321 2101 | structure needing 2102 | many answers, 2103 | 2104 | 426 2105 | 00:19:17,390 --> 00:19:19,623 2106 | because we might have many 2107 | answers to a given question. 2108 | 2109 | 427 2110 | 00:19:19,692 --> 00:19:21,325 2111 | So that's what's 2112 | going on there. 2113 | 2114 | 428 2115 | 00:19:21,394 --> 00:19:23,694 2116 | And then we have this thing 2117 | called the askerImgUrl, 2118 | 2119 | 429 2120 | 00:19:23,763 --> 00:19:26,196 2121 | which is the, the image that's 2122 | gonna get displayed for 2123 | 2124 | 430 2125 | 00:19:26,265 --> 00:19:30,667 2126 | the person asking the current 2127 | question. And that is the URL 2128 | 2129 | 431 2130 | 00:19:30,736 --> 00:19:34,805 2131 | of some image from the web. 2132 | And so once we fetch the hint, 2133 | 2134 | 432 2135 | 00:19:34,874 --> 00:19:37,007 2136 | and we update the UI, 2137 | we go off and 2138 | 2139 | 433 2140 | 00:19:37,076 --> 00:19:40,844 2141 | we set off all the text on 2142 | those labels. We set that 2143 | 2144 | 434 2145 | 00:19:40,913 --> 00:19:44,081 2146 | the whole bottom section on 2147 | the screen should be mapped to 2148 | 2149 | 435 2150 | 00:19:44,149 --> 00:19:47,651 2151 | whether that's reach up the 2152 | top is enabled or disabled. 2153 | 2154 | 436 2155 | 00:19:47,720 --> 00:19:51,488 2156 | And then, if it has just 2157 | been turn on we go often and 2158 | 2159 | 437 2160 | 00:19:51,557 --> 00:19:54,925 2161 | fetch this image online. 2162 | So, that's what this code is 2163 | 2164 | 438 2165 | 00:19:54,993 --> 00:19:57,761 2166 | doing here and when we're 2167 | done way inside here and 2168 | 2169 | 439 2170 | 00:19:57,830 --> 00:20:00,298 2171 | may have the data from this 2172 | image, we're making image for 2173 | 2174 | 440 2175 | 00:20:00,366 --> 00:20:03,300 2176 | you. And we set it off 2177 | as an image with data, 2178 | 2179 | 441 2180 | 00:20:03,369 --> 00:20:06,236 2181 | like Paul did in lecture. And 2182 | then we set its contentMode, 2183 | 2184 | 442 2185 | 00:20:06,305 --> 00:20:10,408 2186 | and we add a subview. So every 2187 | time that the hintSwitch gets 2188 | 2189 | 443 2190 | 00:20:10,476 --> 00:20:12,843 2191 | turns on, we go off and fetch 2192 | the image from online, and 2193 | 2194 | 444 2195 | 00:20:12,912 --> 00:20:14,711 2196 | we add a subview to 2197 | the screen. So, of course, 2198 | 2199 | 445 2200 | 00:20:14,780 --> 00:20:15,645 2201 | we have a problem here, 2202 | 2203 | 446 2204 | 00:20:15,714 --> 00:20:17,948 2205 | which is that if you keep 2206 | adding successive subviews, 2207 | 2208 | 447 2209 | 00:20:18,017 --> 00:20:20,017 2210 | you are adding memory to 2211 | your app, repeatedly. 2212 | 2213 | 448 2214 | 00:20:22,154 --> 00:20:24,455 2215 | That's why we saw the blue 2216 | line going up and up and 2217 | 2218 | 449 2219 | 00:20:24,524 --> 00:20:27,225 2220 | up and up and up, right? 2221 | So that kinda tipped us off, 2222 | 2223 | 450 2224 | 00:20:27,293 --> 00:20:28,659 2225 | since it's going up and 2226 | up we should go look 2227 | 2228 | 451 2229 | 00:20:28,728 --> 00:20:30,461 2230 | inside the code and see if 2231 | there is any reason why we 2232 | 2233 | 452 2234 | 00:20:30,529 --> 00:20:32,563 2235 | would just be repeatedly 2236 | allocating memory without 2237 | 2238 | 453 2239 | 00:20:32,631 --> 00:20:34,598 2240 | ever freeing it. And 2241 | here is our answer right here, 2242 | 2243 | 454 2244 | 00:20:34,667 --> 00:20:36,534 2245 | its this highlighted 2246 | line of code. So it's 2247 | 2248 | 455 2249 | 00:20:36,602 --> 00:20:38,502 2250 | kind of how the allocation 2251 | still works, it's for kind of 2252 | 2253 | 456 2254 | 00:20:38,571 --> 00:20:40,604 2255 | generally figuring out where 2256 | memory is being allocated. 2257 | 2258 | 457 2259 | 00:20:40,673 --> 00:20:42,673 2260 | But it doesn't actually 2261 | go that much deeper into 2262 | 2263 | 458 2264 | 00:20:42,741 --> 00:20:45,342 2265 | figuring out did you leak 2266 | memory or anything like that. 2267 | 2268 | 459 2269 | 00:20:45,410 --> 00:20:47,578 2270 | So that's what the next and 2271 | final instrument that we're 2272 | 2273 | 460 2274 | 00:20:47,647 --> 00:20:50,347 2275 | going to show today is for, 2276 | which is the lease instrument. 2277 | 2278 | 461 2279 | 00:20:50,416 --> 00:20:54,285 2280 | So let's pop open 2281 | instruments again, 2282 | 2283 | 462 2284 | 00:20:54,353 --> 00:20:56,787 2285 | stop our previous run. 2286 | 2287 | 463 2288 | 00:21:01,994 --> 00:21:04,428 2289 | And we will click on the next 2290 | and final leaks tool. 2291 | 2292 | 464 2293 | 00:21:10,836 --> 00:21:14,004 2294 | And then we will head on over 2295 | to the classroom again and 2296 | 2297 | 465 2298 | 00:21:14,073 --> 00:21:16,706 2299 | we'll notice that at 2300 | the bottom here we have this 2301 | 2302 | 466 2303 | 00:21:16,775 --> 00:21:19,877 2304 | kind of like, periodic little 2305 | green check mark saying, 2306 | 2307 | 467 2308 | 00:21:19,946 --> 00:21:22,979 2309 | you have no memory leaks, 2310 | good job. So far for this, 2311 | 2312 | 468 2313 | 00:21:23,048 --> 00:21:25,816 2314 | one of the instrument's trees 2315 | we haven't detected any leaked 2316 | 2317 | 469 2318 | 00:21:25,885 --> 00:21:27,351 2319 | memory and you can see 2320 | it's still green and 2321 | 2322 | 470 2323 | 00:21:27,419 --> 00:21:28,218 2324 | that's great. So 2325 | 2326 | 471 2327 | 00:21:28,287 --> 00:21:30,754 2328 | what you might do in this case 2329 | is just go off through normal 2330 | 2331 | 472 2332 | 00:21:30,823 --> 00:21:32,022 2333 | use cases of your app and 2334 | 2335 | 473 2336 | 00:21:32,091 --> 00:21:34,458 2337 | just try things out and 2338 | see if you have any problems. 2339 | 2340 | 474 2341 | 00:21:34,526 --> 00:21:38,362 2342 | So we might, like, show this 2343 | hint, unshow this hint, and 2344 | 2345 | 475 2346 | 00:21:38,431 --> 00:21:40,498 2347 | the moment that we do that we 2348 | actually see that we already 2349 | 2350 | 476 2351 | 00:21:40,566 --> 00:21:43,033 2352 | have a red x here which means 2353 | that the leak checker has 2354 | 2355 | 477 2356 | 00:21:43,102 --> 00:21:46,871 2357 | detected we have a memory leak 2358 | somehow. That's no good. So 2359 | 2360 | 478 2361 | 00:21:46,939 --> 00:21:48,939 2362 | if we want to figure out more 2363 | details about what's going 2364 | 2365 | 479 2366 | 00:21:49,008 --> 00:21:51,108 2367 | on let's say that we go onto 2368 | one of these x's where it says 2369 | 2370 | 480 2371 | 00:21:51,176 --> 00:21:53,944 2372 | look there are twenty new 2373 | leaks. And we click on it and 2374 | 2375 | 481 2376 | 00:21:54,013 --> 00:21:56,280 2377 | wow look at all these 2378 | leaks that are happening, 2379 | 2380 | 482 2381 | 00:21:56,348 --> 00:21:58,281 2382 | and how could so many leaks 2383 | be happening within a single 2384 | 2385 | 483 2386 | 00:21:58,350 --> 00:22:00,851 2387 | simple application? But 2388 | it turns out that these leaks 2389 | 2390 | 484 2391 | 00:22:00,920 --> 00:22:04,821 2392 | are all happening very under 2393 | the hood like malic this malic 2394 | 2395 | 485 2396 | 00:22:04,890 --> 00:22:08,259 2397 | leak to this leak. So those 2398 | might be caused by one general 2399 | 2400 | 486 2401 | 00:22:08,327 --> 00:22:11,394 2402 | greater leak inside of our 2403 | app which is things like 2404 | 2405 | 487 2406 | 00:22:11,463 --> 00:22:14,465 2407 | question and answer which were 2408 | those classes that we created. 2409 | 2410 | 488 2411 | 00:22:14,534 --> 00:22:17,233 2412 | You can see them here inside 2413 | that view controller. So 2414 | 2415 | 489 2416 | 00:22:17,302 --> 00:22:19,803 2417 | these are the things that are 2418 | the root cause of these leaks. 2419 | 2420 | 490 2421 | 00:22:19,872 --> 00:22:21,739 2422 | But still looking at this 2423 | trace right here it's not 2424 | 2425 | 491 2426 | 00:22:21,807 --> 00:22:22,839 2427 | exactly obvious 2428 | 2429 | 492 2430 | 00:22:22,908 --> 00:22:24,475 2431 | why that leak happened. And so 2432 | 2433 | 493 2434 | 00:22:24,543 --> 00:22:26,209 2435 | we might want to go into 2436 | a little bit more detail, 2437 | 2438 | 494 2439 | 00:22:26,278 --> 00:22:28,745 2440 | and figure out, what the heck, 2441 | why, where is this thing 2442 | 2443 | 495 2444 | 00:22:28,814 --> 00:22:31,715 2445 | coming from and what's going 2446 | on here. And if you'll notice 2447 | 2448 | 496 2449 | 00:22:31,783 --> 00:22:35,085 2450 | up here on this tab, which 2451 | exists in all the instruments, 2452 | 2453 | 497 2454 | 00:22:35,153 --> 00:22:37,922 2455 | we can actually select the way 2456 | that we want it to display 2457 | 2458 | 498 2459 | 00:22:37,990 --> 00:22:40,758 2460 | this data. So right here we 2461 | just have this kind of big, 2462 | 2463 | 499 2464 | 00:22:40,827 --> 00:22:41,859 2465 | set of all the information, 2466 | 2467 | 500 2468 | 00:22:41,927 --> 00:22:44,862 2469 | but we can also do this thing 2470 | called cycles and routes, 2471 | 2472 | 501 2473 | 00:22:44,930 --> 00:22:47,698 2474 | inside the leak checker. 2475 | So when we go inside here, 2476 | 2477 | 502 2478 | 00:22:47,766 --> 00:22:51,001 2479 | we see that we have some 2480 | kind of MallocBlock 2481 | 2482 | 503 2483 | 00:22:51,070 --> 00:22:53,203 2484 | right here that is referencing 2485 | something else that was 2486 | 2487 | 504 2488 | 00:22:53,272 --> 00:22:56,740 2489 | referenced up here earlier. 2490 | And that there is overall some 2491 | 2492 | 505 2493 | 00:22:56,809 --> 00:22:59,343 2494 | circular reference of memory, 2495 | some circular dependency. And 2496 | 2497 | 506 2498 | 00:22:59,411 --> 00:23:01,545 2499 | so maybe when we're freeing 2500 | memory from one thing, 2501 | 2502 | 507 2503 | 00:23:01,614 --> 00:23:04,415 2504 | it's not actually freeing 2505 | everything because things 2506 | 2507 | 508 2508 | 00:23:04,483 --> 00:23:07,017 2509 | still have references to 2510 | things that don't exist in 2511 | 2512 | 509 2513 | 00:23:07,086 --> 00:23:09,353 2514 | memory. So we're actually 2515 | getting rid of all of our 2516 | 2517 | 510 2518 | 00:23:09,422 --> 00:23:12,289 2519 | references. And sometimes when 2520 | you run instruments it won't 2521 | 2522 | 511 2523 | 00:23:12,358 --> 00:23:16,393 2524 | actually symbolicate your, the 2525 | names of your code classes, 2526 | 2527 | 512 2528 | 00:23:16,462 --> 00:23:19,663 2529 | or objects in memory, 2530 | into the cycle checker, 2531 | 2532 | 513 2533 | 00:23:19,731 --> 00:23:21,498 2534 | and sometimes it will 2535 | properly do that. So, 2536 | 2537 | 514 2538 | 00:23:21,567 --> 00:23:26,736 2539 | if I run it again here We'll 2540 | start, we'll restart the app, 2541 | 2542 | 515 2543 | 00:23:26,805 --> 00:23:31,308 2544 | maybe we'll get 2545 | luckier this time. And 2546 | 2547 | 516 2548 | 00:23:31,377 --> 00:23:37,848 2549 | when we show a hint, And 2550 | then we unshow a hint. 2551 | 2552 | 517 2553 | 00:23:41,687 --> 00:23:45,055 2554 | We'll see that there's a leak 2555 | here. Oops, I guess we 2556 | 2557 | 518 2558 | 00:23:45,123 --> 00:23:48,659 2559 | have the same problem. So, 2560 | first lesson of instruments is 2561 | 2562 | 519 2563 | 00:23:48,728 --> 00:23:50,527 2564 | that sometimes in the leaks 2565 | checker you'll notice that it 2566 | 2567 | 520 2568 | 00:23:50,596 --> 00:23:53,630 2569 | doesn't actually symbolicate 2570 | this. In some use cases of 2571 | 2572 | 521 2573 | 00:23:53,699 --> 00:23:57,334 2574 | this you might see it show 2575 | that the questions arrayed 2576 | 2577 | 522 2578 | 00:23:57,403 --> 00:23:59,136 2579 | down here in the answers 2580 | array are up at the top and 2581 | 2582 | 523 2583 | 00:23:59,204 --> 00:24:01,104 2584 | that they have a circular 2585 | reference to each other. 2586 | 2587 | 524 2588 | 00:24:01,173 --> 00:24:03,741 2589 | It might actually show that if 2590 | it symbolicates the debugging 2591 | 2592 | 525 2593 | 00:24:03,809 --> 00:24:06,977 2594 | information. But you're not 2595 | guaranteed to have that. But 2596 | 2597 | 526 2598 | 00:24:07,046 --> 00:24:08,912 2599 | in any case, we know that 2600 | we have a cycle here. 2601 | 2602 | 527 2603 | 00:24:08,981 --> 00:24:11,515 2604 | So what we can do now is 2605 | go off knowing that we have 2606 | 2607 | 528 2608 | 00:24:11,583 --> 00:24:15,919 2609 | a memory cycle somehow, and 2610 | look at our code and go hm, 2611 | 2612 | 529 2613 | 00:24:15,988 --> 00:24:18,789 2614 | something about questions and 2615 | answers has a memory cycle. 2616 | 2617 | 530 2618 | 00:24:18,857 --> 00:24:21,391 2619 | And so I, you can see I have 2620 | these helpful comments here 2621 | 2622 | 531 2623 | 00:24:21,460 --> 00:24:24,027 2624 | after each line like the 2625 | factorization reference count 2626 | 2627 | 532 2628 | 00:24:24,096 --> 00:24:26,397 2629 | is 1. So 2630 | when we allocate when we 2631 | 2632 | 533 2633 | 00:24:26,465 --> 00:24:29,399 2634 | allocate a question to set off 2635 | to this instance variable, 2636 | 2637 | 534 2638 | 00:24:29,468 --> 00:24:33,370 2639 | which is the optional question 2640 | here, factorization question, 2641 | 2642 | 535 2643 | 00:24:33,439 --> 00:24:38,075 2644 | then there's also an answer. 2645 | We're making one here and 2646 | 2647 | 536 2648 | 00:24:38,144 --> 00:24:40,477 2649 | one here, which means 2650 | the reference count is one for 2651 | 2652 | 537 2653 | 00:24:40,546 --> 00:24:42,345 2654 | each of them because 2655 | these are classes, 2656 | 2657 | 538 2658 | 00:24:42,414 --> 00:24:45,282 2659 | which means they're reference 2660 | types, so they're allocated 2661 | 2662 | 539 2663 | 00:24:45,351 --> 00:24:48,418 2664 | with reference counting. And 2665 | then you see down here when we 2666 | 2667 | 540 2668 | 00:24:48,487 --> 00:24:52,122 2669 | set off the factorization 2670 | answers question property, 2671 | 2672 | 541 2673 | 00:24:52,191 --> 00:24:55,525 2674 | now factorization question has 2675 | a reference count of 2 because 2676 | 2677 | 542 2678 | 00:24:55,594 --> 00:24:58,595 2679 | we actually stored it off on 2680 | the answer. And then the same 2681 | 2682 | 543 2683 | 00:24:58,664 --> 00:25:01,598 2684 | thing is true for 2685 | setting off on the answers. 2686 | 2687 | 544 2688 | 00:25:01,667 --> 00:25:04,467 2689 | So in theory, it makes sense 2690 | we could have a cycle here 2691 | 2692 | 545 2693 | 00:25:04,536 --> 00:25:06,570 2694 | because we have the answer 2695 | referencing the question and 2696 | 2697 | 546 2698 | 00:25:06,639 --> 00:25:08,705 2699 | the question referencing 2700 | the answers. So 2701 | 2702 | 547 2703 | 00:25:08,774 --> 00:25:11,541 2704 | there is circularity going on. 2705 | 2706 | 548 2707 | 00:25:11,610 --> 00:25:15,645 2708 | And of course, we do because 2709 | we are never actually, when we 2710 | 2711 | 549 2712 | 00:25:15,714 --> 00:25:20,383 2713 | uncheck the hint switch up at 2714 | the top, that's this else case 2715 | 2716 | 550 2717 | 00:25:20,452 --> 00:25:23,753 2718 | right here, we're nilling 2719 | out the factorization answer 2720 | 2721 | 551 2722 | 00:25:23,822 --> 00:25:28,057 2723 | optional and 2724 | the question optional. But 2725 | 2726 | 552 2727 | 00:25:28,126 --> 00:25:31,028 2728 | the reference counts were two, 2729 | not one, because we internally 2730 | 2731 | 553 2732 | 00:25:31,097 --> 00:25:34,264 2733 | set references to each of 2734 | them off on each other. So 2735 | 2736 | 554 2737 | 00:25:34,332 --> 00:25:36,233 2738 | we never actually get the 2739 | reference counts down to zero, 2740 | 2741 | 555 2742 | 00:25:36,302 --> 00:25:38,701 2743 | which would cause them 2744 | to be fully deallocated, 2745 | 2746 | 556 2747 | 00:25:38,770 --> 00:25:40,971 2748 | which is why we have a memory 2749 | leak popping up due to what's 2750 | 2751 | 557 2752 | 00:25:41,040 --> 00:25:43,073 2753 | called a complex cycle. That's 2754 | what instruments was telling 2755 | 2756 | 558 2757 | 00:25:43,142 --> 00:25:46,409 2758 | us. So now our question 2759 | becomes, how on earth do we 2760 | 2761 | 559 2762 | 00:25:46,478 --> 00:25:48,612 2763 | solve this problem? And there 2764 | are lots of ways that we could 2765 | 2766 | 560 2767 | 00:25:48,681 --> 00:25:51,248 2768 | solve this problem. One of 2769 | the ways is we could say, 2770 | 2771 | 561 2772 | 00:25:51,317 --> 00:25:53,149 2773 | the problem has to do with 2774 | reference counts. What we 2775 | 2776 | 562 2777 | 00:25:53,218 --> 00:25:55,552 2778 | really need to do is get these 2779 | reference counts down to zero. 2780 | 2781 | 563 2782 | 00:25:55,621 --> 00:25:59,957 2783 | So manually we can go in 2784 | like it's 1995 and say 2785 | 2786 | 564 2787 | 00:26:00,025 --> 00:26:03,727 2788 | factorizationAnswer.question = 2789 | nil, 2790 | 2791 | 565 2792 | 00:26:03,795 --> 00:26:08,899 2793 | which will cause the reference 2794 | count to actually be zero. 2795 | 2796 | 566 2797 | 00:26:08,967 --> 00:26:11,301 2798 | And then that will cause the 2799 | reference count to hit zero. 2800 | 2801 | 567 2802 | 00:26:11,370 --> 00:26:13,436 2803 | And then it will get 2804 | deallocated properly. But 2805 | 2806 | 568 2807 | 00:26:13,505 --> 00:26:15,639 2808 | imagine if every time you ever 2809 | wrote a code that used classes 2810 | 2811 | 569 2812 | 00:26:15,707 --> 00:26:17,707 2813 | and allocated things on the 2814 | heap, you had to manually go 2815 | 2816 | 570 2817 | 00:26:17,776 --> 00:26:19,342 2818 | make sure your reference 2819 | counts were zero, 2820 | 2821 | 571 2822 | 00:26:19,411 --> 00:26:21,678 2823 | that would be really gross. 2824 | And it would be nice if 2825 | 2826 | 572 2827 | 00:26:21,747 --> 00:26:23,947 2828 | the programming language 2829 | itself gave you constructs to 2830 | 2831 | 573 2832 | 00:26:24,016 --> 00:26:26,683 2833 | avoid this problem in the 2834 | first place. And of course, 2835 | 2836 | 574 2837 | 00:26:26,752 --> 00:26:30,520 2838 | Swift does. It affords you 2839 | many. So the first and 2840 | 2841 | 575 2842 | 00:26:30,589 --> 00:26:33,190 2843 | simplest way that Paul has 2844 | gone over in lecture with 2845 | 2846 | 576 2847 | 00:26:33,258 --> 00:26:37,126 2848 | regard to closures is to use 2849 | this keyword called weak. So 2850 | 2851 | 577 2852 | 00:26:37,195 --> 00:26:41,064 2853 | if we store off the reference 2854 | to the question inside answer 2855 | 2856 | 578 2857 | 00:26:41,133 --> 00:26:44,901 2858 | as a weak variable, then it 2859 | doesn't actually increment 2860 | 2861 | 579 2862 | 00:26:44,970 --> 00:26:47,704 2863 | that reference count. 2864 | And there's actually no reason 2865 | 2866 | 580 2867 | 00:26:47,773 --> 00:26:49,806 2868 | that why when the answer 2869 | itself is set to nil, 2870 | 2871 | 581 2872 | 00:26:49,875 --> 00:26:50,874 2873 | it won't go down to zero and 2874 | 2875 | 582 2876 | 00:26:50,943 --> 00:26:53,410 2877 | get deallocated properly. 2878 | So all we really need to do 2879 | 2880 | 583 2881 | 00:26:53,479 --> 00:26:56,212 2882 | is add weak in order to get 2883 | the same behavior as manually 2884 | 2885 | 584 2886 | 00:26:56,281 --> 00:26:59,316 2887 | going off and 2888 | 2889 | 585 2890 | 00:26:59,384 --> 00:27:01,851 2891 | So that's a nice thing that 2892 | Swift provides you to solve 2893 | 2894 | 586 2895 | 00:27:01,920 --> 00:27:03,654 2896 | a reference counting 2897 | problem like that. But 2898 | 2899 | 587 2900 | 00:27:03,722 --> 00:27:06,656 2901 | there's one more way that 2902 | Swift gives you to solve this 2903 | 2904 | 588 2905 | 00:27:06,725 --> 00:27:09,859 2906 | problem up front in the very 2907 | first place. Which is to say, 2908 | 2909 | 589 2910 | 00:27:09,928 --> 00:27:12,562 2911 | you know, we have this 2912 | distinction between 2913 | 2914 | 590 2915 | 00:27:12,631 --> 00:27:16,466 2916 | classes and structs, reference 2917 | types and value types. And why 2918 | 2919 | 591 2920 | 00:27:16,535 --> 00:27:20,270 2921 | on earth are we using classes 2922 | here to increment reference 2923 | 2924 | 592 2925 | 00:27:20,339 --> 00:27:24,207 2926 | types when a struct would have 2927 | done just the same. This is, 2928 | 2929 | 593 2930 | 00:27:24,276 --> 00:27:26,042 2931 | there's no reason that this 2932 | needs to be a class. We don't 2933 | 2934 | 594 2935 | 00:27:26,111 --> 00:27:27,944 2936 | need a handle on it anywhere 2937 | else in the application. 2938 | 2939 | 595 2940 | 00:27:28,013 --> 00:27:30,614 2941 | We don't need a pointer to it. 2942 | We don't need to worry about 2943 | 2944 | 596 2945 | 00:27:30,683 --> 00:27:33,483 2946 | the fact that making many 2947 | of them is going to cause 2948 | 2949 | 597 2950 | 00:27:33,552 --> 00:27:35,686 2951 | way too much memory to happen 2952 | cuz they're just questions and 2953 | 2954 | 598 2955 | 00:27:35,754 --> 00:27:38,188 2956 | answers which are tiny amounts 2957 | of data. So what the heck? 2958 | 2959 | 599 2960 | 00:27:38,256 --> 00:27:40,456 2961 | Why are these classes in the 2962 | first place? All righty, so 2963 | 2964 | 600 2965 | 00:27:40,525 --> 00:27:43,426 2966 | since we've changed these two 2967 | structs now, we should have no 2968 | 2969 | 601 2970 | 00:27:43,495 --> 00:27:45,829 2971 | more reference kind of going 2972 | on anywhere with questions and 2973 | 2974 | 602 2975 | 00:27:45,898 --> 00:27:48,799 2976 | answers. Which means that 2977 | value typing has solved our 2978 | 2979 | 603 2980 | 00:27:48,868 --> 00:27:51,568 2981 | whole allocation leak 2982 | problem outright, 2983 | 2984 | 604 2985 | 00:27:51,637 --> 00:27:54,705 2986 | which is a great feature 2987 | of the Swift language 2988 | 2989 | 605 2990 | 00:27:54,773 --> 00:27:57,006 2991 | in the first place. So 2992 | that's actually kind of the, 2993 | 2994 | 606 2995 | 00:27:57,075 --> 00:27:58,675 2996 | the nuclear bomb and 2997 | great solution to, 2998 | 2999 | 607 3000 | 00:27:58,743 --> 00:28:01,145 3001 | to this problem which is 3002 | that you should just use, 3003 | 3004 | 608 3005 | 00:28:01,213 --> 00:28:03,880 3006 | you should use value types 3007 | when, when possible, and 3008 | 3009 | 609 3010 | 00:28:03,949 --> 00:28:05,983 3011 | when it makes sense to. If you 3012 | don't need a reference type, 3013 | 3014 | 610 3015 | 00:28:06,052 --> 00:28:09,086 3016 | then you should use 3017 | the value type. And 3018 | 3019 | 611 3020 | 00:28:09,154 --> 00:28:11,754 3021 | that's kind of the end of 3022 | the leaks demonstration here. 3023 | 3024 | 612 3025 | 00:28:11,823 --> 00:28:16,459 3026 | So as you can, as you can see, 3027 | we've been through these four 3028 | 3029 | 613 3030 | 00:28:16,528 --> 00:28:19,929 3031 | main types of instruments 3032 | here, the activity monitor, 3033 | 3034 | 614 3035 | 00:28:19,998 --> 00:28:22,266 3036 | the time profiler, 3037 | allocations, and leaks. 3038 | 3039 | 615 3040 | 00:28:22,334 --> 00:28:23,333 3041 | And you've gotten 3042 | a kind of taste 3043 | 3044 | 616 3045 | 00:28:23,402 --> 00:28:24,601 3046 | of how they work. 3047 | If you wanna go off and 3048 | 3049 | 617 3050 | 00:28:24,670 --> 00:28:27,037 3051 | learn how more of them work, 3052 | maybe you're going off, 3053 | 3054 | 618 3055 | 00:28:27,106 --> 00:28:28,772 3056 | you wanna figure out what's 3057 | going over the network. 3058 | 3059 | 619 3060 | 00:28:28,841 --> 00:28:30,974 3061 | You can learn more about the 3062 | Network tab on your own time, 3063 | 3064 | 620 3065 | 00:28:31,042 --> 00:28:33,777 3066 | or you're making some kind 3067 | of graphics application, 3068 | 3069 | 621 3070 | 00:28:33,845 --> 00:28:37,080 3071 | you might use these purple 3072 | colored ones, like scene kit, 3073 | 3074 | 622 3075 | 00:28:37,149 --> 00:28:38,948 3076 | or metal system trace, 3077 | or core animation. 3078 | 3079 | 623 3080 | 00:28:39,017 --> 00:28:40,917 3081 | So there's lots more that 3082 | you can do with instruments. 3083 | 3084 | 624 3085 | 00:28:40,986 --> 00:28:42,653 3086 | And it's very fun to 3087 | go off and use it. 3088 | 3089 | 625 3090 | 00:28:42,721 --> 00:28:45,222 3091 | So I encourage you to go off 3092 | and try all of them out and 3093 | 3094 | 626 3095 | 00:28:45,290 --> 00:28:47,290 3096 | figure out how you 3097 | can Introduce it 3098 | 3099 | 627 3100 | 00:28:47,359 --> 00:28:49,626 3101 | into your debugging process so 3102 | that you're not sitting there 3103 | 3104 | 628 3105 | 00:28:49,695 --> 00:28:52,296 3106 | really confused by your most 3107 | complex bugs. And instead, 3108 | 3109 | 629 3110 | 00:28:52,364 --> 00:28:54,631 3111 | you're going off and just 3112 | getting more data and figuring 3113 | 3114 | 630 3115 | 00:28:54,700 --> 00:28:56,866 3116 | out what's true and not true 3117 | about your application. 3118 | 3119 | 631 3120 | 00:28:56,935 --> 00:28:58,902 3121 | So that's it for the 3122 | instruments demo for today. 3123 | 3124 | 632 3125 | 00:28:58,971 --> 00:29:01,805 3126 | I hope this has been useful, 3127 | and we'll see you in class on 3128 | 3129 | 633 3130 | 00:29:01,874 --> 00:29:03,806 3131 | Monday. 3132 | >> For 3133 | 3134 | 634 3135 | 00:29:03,875 --> 00:29:13,282 3136 | more, please visit 3137 | us at stanford.edu. 3138 | 3139 | --------------------------------------------------------------------------------