<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:media="http://search.yahoo.com/mrss/">
 	<channel>
		<title>Apple Devo | Contra Corner | Bob Peterson</title>
		<link>https://www.contracorner.com/stuff/</link>
		<description></description>
		<language>en</language>
		<lastBuildDate>Wed, 25 May 2022 09:28:29 -0400</lastBuildDate>
		<docs>http://blogs.law.harvard.edu/tech/rss</docs>
		<generator>Sandvox 2.10.12</generator>
		<item>
			<title>Debugging SwiftUI</title>
			<link>https://www.contracorner.com/stuff/debugging-swiftui.html</link>
			<description>
				&lt;div class="article-summary"&gt;&lt;p&gt;To print why a view is being rebuilt, use &lt;span style="font-family: Menlo; font-size: 12px; background-color: rgb(255, 255, 255); font-style: inherit; font-variant-caps: inherit; color: rgb(155, 35, 147);"&gt;&lt;strong&gt;Self&lt;/strong&gt;&lt;/span&gt;&lt;span style="font-family: Menlo; font-size: 12px; background-color: rgb(255, 255, 255); font-style: inherit; font-variant-caps: inherit; color: rgba(0, 0, 0, 0.85);"&gt;.&lt;/span&gt;&lt;span style="color: rgb(108, 54, 169); font-family: Menlo; font-size: 12px; background-color: rgb(255, 255, 255); font-style: inherit; font-variant-caps: inherit;"&gt;_printChanges&lt;/span&gt;&lt;span style="font-family: Menlo; font-size: 12px; background-color: rgb(255, 255, 255); font-style: inherit; font-variant-caps: inherit; color: rgba(0, 0, 0, 0.85);"&gt;()&lt;/span&gt;&lt;span style="font-family: inherit; font-style: inherit; font-variant-caps: inherit;"&gt;. You can add it as a line of code in the body property like this:&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-size: 12px; font-family: Menlo; color: rgb(155, 35, 147);"&gt;&lt;strong&gt;let _ = Self._printChanges()&lt;/strong&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;(Remember you now need to explicitly return the computed view.)&lt;span style="font-family: inherit; font-style: inherit; font-variant-caps: inherit;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;You can also add it as a debugger breakpoint withoit modifying code:&lt;/p&gt;
					
					&lt;div class="first graphic-container wide center ImageElement"&gt;
						&lt;div class="graphic"&gt;
							&lt;div class="figure-content"&gt;
								&lt;!-- sandvox.ImageElement --&gt;&lt;img src="https://www.contracorner.com/_Media/screen-shot-2022-01-21-at_med.png" alt="Screen Shot 2022-01-21 at 14.51.36" width="510" height="277" /&gt;
								&lt;!-- /sandvox.ImageElement --&gt;&lt;/div&gt;
						&lt;/div&gt;
					&lt;/div&gt;&lt;p&gt;The output is something like:&lt;/p&gt;&lt;p style="font-stretch: normal; font-size: 12px; font-family: Menlo; background-color: rgb(255, 255, 255);"&gt;&lt;strong&gt;DanceStack: @self, @identity, _vm, _compact changed.&lt;br /&gt;&lt;/strong&gt;&lt;strong style="font-style: inherit; font-variant-caps: inherit;"&gt;() $R6 = {}&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;(The register dump can be ignored.)&lt;/p&gt;
					
				&lt;/div&gt;
			</description>
			<pubDate>Fri, 21 Jan 2022 14:43:38 -0500</pubDate>
			<guid>https://www.contracorner.com/stuff/debugging-swiftui.html</guid>
            
			
		</item>
		<item>
			<title>CPU Temperature</title>
			<link>https://www.contracorner.com/stuff/cpu-temperature.html</link>
			<description>
				&lt;div class="article-summary"&gt;&lt;p&gt;This is a useful built-in command for checking hardware metrics, especially CPU temperature.&lt;/p&gt;&lt;p&gt;&lt;span style="color: rgb(79, 143, 0); font-style: normal; font-family: Menlo;"&gt;sudo powermetrics&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color: rgb(148, 82, 0); font-family: Menlo;"&gt;[…]&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color: rgb(148, 82, 0); font-style: normal; font-family: Menlo;"&gt;**** SMC sensors ****&lt;br /&gt;&lt;/span&gt;&lt;span style="font-variant-caps: inherit; font-style: normal; font-family: Menlo; color: rgb(148, 82, 0);"&gt;CPU Thermal level: 99&lt;br /&gt;GPU Thermal level: 50&lt;br /&gt;IO Thermal level: 50&lt;br /&gt;Fan: 5219.96 rpm&lt;br /&gt;CPU die temperature: 57.20 C&lt;br /&gt;GPU die temperature: 35.00 C&lt;br /&gt;CPU Plimit: 0.00&lt;br /&gt;GPU Plimit (Int): 0.00&lt;br /&gt;Number of prochots: 0&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family: Times; font-variant-caps: inherit;"&gt;&lt;strong&gt;See:&lt;/strong&gt;&lt;/span&gt;&lt;/p&gt;&lt;ul&gt;&lt;li&gt;AskDifferent/StackExchange: &lt;a href="https://apple.stackexchange.com/a/372928/212045" target="_blank"&gt;Can I get the CPU temperature and fan speed from the command line in OS X?&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;&lt;span style="font-variant-caps: inherit; font-style: normal; font-family: Menlo; color: rgb(148, 82, 0);"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/p&gt;&lt;/div&gt;
			</description>
			<pubDate>Sun, 21 Jun 2020 14:25:51 -0400</pubDate>
			<guid>https://www.contracorner.com/stuff/cpu-temperature.html</guid>
            
			
		</item>
		<item>
			<title>BBEdit Filters</title>
			<link>https://www.contracorner.com/stuff/bbedit-filters.html</link>
			<description>
				&lt;div class="article-summary"&gt;&lt;p&gt;Some useful filters. Put filters in &lt;span style="font-style: normal; font-size: 14px; font-family: Menlo;"&gt;~/Library/Application Support/BBEdit/Text Filters&lt;/span&gt;. They must be &lt;span style="font-style: normal; font-size: 14px; font-family: Menlo;"&gt;bash&lt;/span&gt; scripts.&lt;/p&gt;&lt;p&gt;&lt;strong&gt;XML Format.sh&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-size: 14px; font-family: Menlo;"&gt;#!/bin/bash&lt;br /&gt;&lt;/span&gt;&lt;span style="font-variant-caps: inherit; font-size: 14px; font-family: Menlo;"&gt;xmllint --format -&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;strong&gt;JSON Format.sh&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-size: 14px; font-family: Menlo;"&gt;#!/bin/bash&lt;br /&gt;&lt;/span&gt;&lt;span style="font-variant-caps: inherit; font-size: 14px; font-family: Menlo;"&gt;python -m json.tool&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family: Times;"&gt;Contrary to another suggestion, I did not like using &lt;/span&gt;&lt;span style="font-size: 14px; font-family: Menlo;"&gt;plutil&lt;/span&gt;&lt;span style="font-family: Times;"&gt; as it is fussy about the kind of XML it formats. &lt;/span&gt;&lt;/p&gt;&lt;/div&gt;
			</description>
			<pubDate>Mon, 25 May 2020 11:19:16 -0400</pubDate>
			<guid>https://www.contracorner.com/stuff/bbedit-filters.html</guid>
            
			
		</item>
		<item>
			<title>Second Life ExternalEditor</title>
			<link>https://www.contracorner.com/stuff/second-life-externaleditor.html</link>
			<description>
				&lt;div class="article-summary"&gt;&lt;p&gt;OK, this post has nothing to do with developing for Apple devices. Instead it’s about using an external editor with Second Life text objects such as Notecards and Scripts.&lt;/p&gt;&lt;p&gt;Here’s what Linden Lab says:&lt;/p&gt;
					
					&lt;div class="first blockquote-container wide center"&gt;
						&lt;div style="" class="graphic"&gt;&lt;blockquote class="figure-content"&gt;
								&lt;p style="font-weight: bold; font-style: normal; font-family: Arial, Helvetica, sans-serif;"&gt;&lt;span class="mw-headline"&gt;Opening scripts in another editor directly from the viewer:&lt;/span&gt;&lt;/p&gt;&lt;ul style="font-family: verdana, helvetica, sans-serif; font-size: 12.598400115966797px; font-style: normal;"&gt;&lt;li&gt;Enable: Preferences &amp;gt; Advanced &amp;gt; Show &lt;a href="http://wiki.secondlife.com/wiki/Advanced_menu" title="Advanced menu" style="text-decoration: underline; color: rgb(90, 54, 150);" class="mw-redirect"&gt;Advanced_menu&lt;/a&gt;.&lt;/li&gt;&lt;li&gt;Go to: Advanced &amp;gt; Show &lt;a href="http://wiki.secondlife.com/wiki/Debug_Settings" title="Debug Settings" style="text-decoration: underline; color: rgb(90, 54, 150);"&gt;Debug_Settings&lt;/a&gt;.&lt;/li&gt;&lt;li&gt;Edit: "ExternalEditor”.&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;&lt;span style="font-size: 12.598400115966797px; font-style: normal; font-family: verdana, helvetica, sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="font-family: verdana, helvetica, sans-serif; font-size: 12.598400115966797px; font-style: normal;"&gt;As the description tells you, enter 3 params:&lt;/p&gt;&lt;ul style="font-family: verdana, helvetica, sans-serif; font-size: 12.598400115966797px; font-style: normal;"&gt;&lt;li&gt;Path to editor (enclose in quotes, if path has spaces).&lt;/li&gt;&lt;li&gt;Optional command line params (you can ignore these if you like).&lt;/li&gt;&lt;li&gt;&lt;code style="font-family: monospace, Courier; background-color: rgb(238, 238, 238); font-size: 12px;"&gt;"%s"&lt;/code&gt; will paste script path automatically (dont change this).&lt;/li&gt;&lt;/ul&gt;
								
							&lt;/blockquote&gt;
						&lt;/div&gt;
					&lt;/div&gt;
					&lt;p&gt;Here’s what you really put into the setting value:&lt;/p&gt;&lt;p style="text-align: left;"&gt;&lt;span style="font-family: Menlo;"&gt;&lt;strong&gt;ExternalEditor&lt;/strong&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="text-align: left;"&gt;&lt;span style="font-style: normal; font-family: Menlo;"&gt;&lt;strong&gt;/usr/bin/open -a bbedit "%s&lt;/strong&gt;&lt;/span&gt;&lt;strong style="font-style: inherit; font-variant-caps: inherit; font-family: Menlo;"&gt;"&lt;/strong&gt;&lt;/p&gt;&lt;p style="text-align: left;"&gt;I tried other text and it failed:&lt;/p&gt;&lt;p&gt;&lt;span style="font-style: normal; font-family: Menlo;"&gt;/usr/local/bin/bbedit "%s&lt;/span&gt;&lt;span style="font-family: Menlo; font-style: inherit; font-variant-caps: inherit;"&gt;"&lt;/span&gt;&lt;/p&gt;&lt;p&gt;This is dumb. What the heck? Then, one Second Life JIRA hinted me:&lt;/p&gt;&lt;p&gt;&lt;span style="font-style: normal; font-family: Menlo;"&gt;/usr/bin/open "%s&lt;/span&gt;&lt;span style="font-family: Menlo; font-style: inherit; font-variant-caps: inherit;"&gt;"&lt;/span&gt;&lt;/p&gt;&lt;p&gt;That opened the file in Apple’s own TextEdit! Close, but no cigar. The man page for open said -a might work so I tried it.&lt;/p&gt;&lt;p&gt;What’s extra nice is you can keep the document open and keep re-saving it. When I sigure out how to to keep versions backed up with each save I’ll add that here.&lt;/p&gt;&lt;p&gt;See:&lt;/p&gt;&lt;ul&gt;&lt;li&gt;&lt;span style="font-family: inherit; font-style: inherit; font-variant-caps: inherit;"&gt;&lt;a href="https://jira.secondlife.com/browse/STORM-1949" target="_blank"&gt;https://jira.secondlife.com/browse/STORM-1949&lt;/a&gt;&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;
					
				&lt;/div&gt;
			</description>
			<pubDate>Tue, 12 May 2020 20:40:09 -0400</pubDate>
			<guid>https://www.contracorner.com/stuff/second-life-externaleditor.html</guid>
            
			
		</item>
		<item>
			<title>Playgrounds, Timers, and Async Callbacks</title>
			<link>https://www.contracorner.com/stuff/playground-async.html</link>
			<description>
				&lt;div class="article-summary"&gt;&lt;p&gt;This is a simple example of using Timer in Swift. I’ve seen some Stack Overflow advice that says Timer performs badly but had no backup to support this assertion.&lt;/p&gt;
					
					&lt;div class="first wide left"&gt;
						&lt;div style="" class="graphic"&gt;
							&lt;div class="figure-content HTMLElement"&gt;
								&lt;div style="text-align:end; font-size: small; font-style: italic"&gt;Swift 4.1&lt;/div&gt;
&lt;pre&gt;&lt;code class="language-swift"&gt;import Foundation
import PlaygroundSupport

PlaygroundPage.current.needsIndefiniteExecution = true

print("Starting")
let t = Timer.scheduledTimer(withTimeInterval: 5.0, repeats: false) { (timer) in
    print("TIMER FIRED")
}
//t.fire()
print("Waiting...")
&lt;/code&gt;&lt;/pre&gt;
							&lt;/div&gt;
						&lt;/div&gt;
					&lt;/div&gt;&lt;p&gt;You must invoke &lt;strong&gt;scheduledTimer&lt;/strong&gt; on the main thread. If you don’t, iOS will quietly never fire the timer.&lt;/p&gt;&lt;p&gt;Useful reading:&lt;/p&gt;&lt;ul&gt;&lt;li&gt;&lt;a href="https://stackoverflow.com/questions/24058336/how-do-i-run-asynchronous-callbacks-in-playground/37864618#37864618"&gt;How do I run Asynchronous callbacks in Playground&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="https://stackoverflow.com/questions/24058336/how-do-i-run-asynchronous-callbacks-in-playground" style=""&gt;Session 213 at WWDC 2016&lt;/a&gt;&lt;/li&gt;&lt;li&gt;There a note "Be sure to start timers in the main thread” in &lt;a href="https://stackoverflow.com/questions/50826548/timer-scheduledtimer-not-firing"&gt;Timer.scheduledTimer not firing&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;
					
				&lt;/div&gt;
			</description>
			<pubDate>Wed, 25 Apr 2018 10:20:00 -0400</pubDate>
			<guid>https://www.contracorner.com/stuff/playground-async.html</guid>
            
			
		</item>
		<item>
			<title>Caching CKRecords</title>
			<link>https://www.contracorner.com/stuff/caching-ckrecords.html</link>
			<description>
				&lt;div class="article-summary"&gt;&lt;p&gt;It’s not possible to update an existing record in CloudKit without having saved specific data from a CKRecord given to you by CloudKit. You can’t simply create a new CKRecordID and use the last changeToken and create a CKRecord yourself usingt those. If you do, CloudKit thinks you’re tryingt o create a brand new record. It will given an error if the record name already exists in that zone.&lt;/p&gt;&lt;p&gt;So you must either &lt;/p&gt;&lt;ol&gt;&lt;li&gt;&lt;span style="font-size: 1em;"&gt;fetch a record, change data in the CKRecord you got, and give that CKRecord to the modify operation,&lt;/span&gt;&lt;/li&gt;&lt;/ol&gt;&lt;p&gt;OR&lt;/p&gt;&lt;ol&gt;&lt;li value="2"&gt;save the record in whole or part, change data in that CKRecord, and give that CKRecord to the modify operation. &lt;/li&gt;&lt;/ol&gt;&lt;p&gt;Method is 2 is necessary if you’re coordinating data across devices, because then the modify will correctly fail if another device modified the record since your last caching of it. You can save cache size by saving only the necessary parts of the CKRecord.&lt;/p&gt;&lt;p&gt;&lt;strong&gt;Caching CKRecord&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;The canonical way to cache the salient record data is &lt;span style="font-family: Menlo;"&gt;CKRecord.encodeSystemFields&lt;/span&gt;. The prescribed method is to archive using an NSCoder, however JSONEncoder does not implement that.&lt;/p&gt;&lt;p&gt;&lt;strong&gt;Further reading&lt;/strong&gt;&lt;/p&gt;&lt;ul&gt;&lt;li&gt;Q&amp;amp;A &lt;span style="font-size: 1em;"&gt;&lt;em&gt;&lt;a href="https://stackoverflow.com/questions/49075738/is-it-possible-to-use-ckrecord-encodesystemfields-with-jsonencoder" target="_blank"&gt;Is it possible to use CKRecord.encodeSystemFields with JSONEncoder&lt;/a&gt;&lt;/em&gt;&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="https://stackoverflow.com/questions/39737190/how-and-when-do-i-use-iclouds-encodesystemfields-method-on-ckrecord#39738006" target="_blank" style="font-size: 1em;"&gt;How (and when) do I use iCloud's encodeSystemFields method on CKRecord?&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;&lt;br /&gt;&lt;/div&gt;
			</description>
			<pubDate>Fri, 02 Mar 2018 10:59:08 -0500</pubDate>
			<guid>https://www.contracorner.com/stuff/caching-ckrecords.html</guid>
            
			
		</item>
		<item>
			<title>JSON and Codable</title>
			<link>https://www.contracorner.com/stuff/json-and-codable.html</link>
			<description>
				&lt;div class="article-summary"&gt;&lt;p&gt;Her are some tips for using JSON and Codable.&lt;/p&gt;&lt;p&gt;&lt;span style="color: rgb(0, 0, 0); font-family: Menlo; font-size: 15px; background-color: rgb(255, 255, 255);"&gt;Codable&lt;/span&gt; is a compound protocol containing &lt;span style="color: rgb(112, 61, 170); font-family: Menlo; font-size: 15px; background-color: rgb(255, 255, 255);"&gt;Decodable&lt;/span&gt;&lt;span style="font-family: Menlo; font-size: 15px; background-color: rgb(255, 255, 255); color: rgb(0, 0, 0);"&gt; &amp;amp; &lt;/span&gt;&lt;span style="color: rgb(112, 61, 170); font-family: Menlo; font-size: 15px; background-color: rgb(255, 255, 255);"&gt;Encodable&lt;/span&gt;&lt;span style="font-family: Georgia;"&gt;. When your type implements these, instances can be created from Decoder objects, and instances can be encoded out using &lt;/span&gt;&lt;span style="font-family: Georgia; font-size: 1em;"&gt;Encoder objects.&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-size: 1em; font-family: Georgia;"&gt;JSONEncoders, using &lt;a href="https://en.wikipedia.org/wiki/Inversion_of_control" target="_blank"&gt;Inversion of Control&lt;/a&gt;, will create an Encoder and call your type&lt;/span&gt;&lt;span style="font-family: Georgia;"&gt;’&lt;/span&gt;&lt;span style="font-size: 1em; font-family: Georgia;"&gt;s &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0); font-family: Menlo; font-size: 15px; background-color: rgb(255, 255, 255);"&gt;encode&lt;/span&gt;&lt;span style="font-family: Georgia; font-size: 1em;"&gt; method. JSONDecoders likewise will create instances using the special &lt;/span&gt;&lt;span style="font-family: Menlo; font-size: 15px; background-color: rgb(255, 255, 255); color: rgb(186, 45, 162);"&gt;init&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0); font-family: Menlo; font-size: 15px; background-color: rgb(255, 255, 255);"&gt;(from decoder: &lt;/span&gt;&lt;span style="font-family: Menlo; font-size: 15px; background-color: rgb(255, 255, 255); color: rgb(112, 61, 170);"&gt;Decoder&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0); font-family: Menlo; font-size: 15px; background-color: rgb(255, 255, 255);"&gt;)&lt;/span&gt;&lt;span style="font-family: Georgia;"&gt; method&lt;/span&gt;&lt;span style="font-family: Georgia; font-size: 1em;"&gt;. Inversion of control means entire data graphs can be coded witha single call. Nicely automatic!&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family: Georgia;"&gt;Here’s a sample round trip encode and decode:&lt;/span&gt;&lt;/p&gt;
					
					&lt;div class="first wide center"&gt;
						&lt;div style="" class="graphic"&gt;
							&lt;div class="figure-content HTMLElement"&gt;
								&lt;div style="text-align:end; font-style: italic"&gt;Swift 4&lt;/div&gt;
&lt;pre&gt;&lt;code class="language-swift"&gt;// Encode a Thing
let jencoder = JSONEncoder()
do {
    let d1: Data = try jencoder.encode(thing)
    // d1 is now a Data which is a String in utf8 encoding.
	
    // Decode a Data back to a Thing
    let jdecoder = JSONDecoder()
    let sameAsThing = try jdecoder.decode(Thing.self, from: d1)
    // sameAsThing is now a Thing
}
catch let err {
    print(err.localizedDescription)
}&lt;/code&gt;&lt;/pre&gt;
							&lt;/div&gt;
						&lt;/div&gt;
					&lt;/div&gt;&lt;p&gt;&lt;span style="font-family: Georgia;"&gt;Here what the type looks like:&lt;/span&gt;&lt;/p&gt;
					
					&lt;div class="not-first-item wide right"&gt;
						&lt;div style="" class="graphic"&gt;
							&lt;div class="figure-content HTMLElement"&gt;
								&lt;div style="text-align:end; font-style: italic"&gt;Swift 4&lt;/div&gt;
&lt;pre&gt;&lt;code class="language-swift"&gt;struct Thing: Codable {
    let parts: [String]
    init(_  parts: [String]) { self.parts = parts }
    init(from decoder: Decoder) throws {
        var container = try decoder.unkeyedContainer()
        var parts = [String]()
        while !container.isAtEnd {
            let part = try container.decode(String.self)
            parts.append(part)
        }
        self.init(parts)
    }
    func encode(to encoder: Encoder) throws {
        var container = encoder.unkeyedContainer()
        for part in parts {
            try container.encode(part)
        }
    }
}&lt;/code&gt;&lt;/pre&gt;
							&lt;/div&gt;
						&lt;/div&gt;
					&lt;/div&gt;&lt;p&gt;&lt;span style="font-family: Georgia;"&gt;Another way to encode an array is:&lt;/span&gt;&lt;/p&gt;
					
					&lt;div class="not-first-item wide right"&gt;
						&lt;div style="" class="graphic"&gt;
							&lt;div class="figure-content HTMLElement"&gt;
								&lt;div style="text-align:end; font-style: italic"&gt;Swift 4&lt;/div&gt;
&lt;pre&gt;&lt;code class="language-swift"&gt;struct Thing: Codable {
    let parts: [String]
    init(_  parts: [String]) { self.parts = parts }
    init(from decoder: Decoder) throws {
        let container = try decoder.singleValueContainer()
        let parts = try container.decode([String].self)
        self.init(parts)
    }
    func encode(to encoder: Encoder) throws {
        var container = encoder.singleValueContainer()
        try container.encode(self.parts)
    }
}&lt;/code&gt;&lt;/pre&gt;
							&lt;/div&gt;
						&lt;/div&gt;
					&lt;/div&gt;&lt;p&gt;&lt;span style="font-family: Georgia;"&gt;However, a top-levle JSON cannot be a scalar. It must be an encoded array or dictionary.&lt;/span&gt;&lt;/p&gt;
					
					&lt;div class="not-first-item wide right"&gt;
						&lt;div style="" class="graphic"&gt;
							&lt;div class="figure-content HTMLElement"&gt;
								&lt;div style="text-align:end; font-style: italic"&gt;Swift 4&lt;/div&gt;
&lt;pre&gt;&lt;code class="language-swift"&gt;struct Thing: Codable {
    let parts: [String]
    init(_  parts: [String]) { self.parts = parts }
    init(from decoder: Decoder) throws {
        let raw = try decoder.singleValueContainer().decode(String.self)
        self.init(raw.split(separator: " ").map({ String($0) }))
    }
    func encode(to encoder: Encoder) throws {
        var ven = encoder.singleValueContainer()
        let raw = parts.joined(separator: " ")
        try ven.encode(raw)
    }
}&lt;/code&gt;&lt;/pre&gt;
							&lt;/div&gt;
						&lt;/div&gt;
					&lt;/div&gt;&lt;p&gt;&lt;span style="font-family: Georgia;"&gt;If you try to encode this, it tries to generate a top-level String, which is scalar and creates illegal JSON. You’ll get an &lt;/span&gt;&lt;span style="color: rgb(0, 132, 0); font-family: Menlo; font-size: 15px; background-color: rgb(255, 255, 255);"&gt;invalidValue&lt;/span&gt;&lt;span style="font-family: Georgia; font-size: 1em;"&gt; error:&lt;/span&gt;&lt;/p&gt;&lt;ul&gt;&lt;li&gt;&lt;span style="font-family: Georgia;"&gt;debugDescription: "Top-level Text encoded as string JSON fragment.”&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style="font-family: Georgia;"&gt;localizedDescription: "The data couldn’t be written because it isn’t in the correct format.”&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;&lt;span style="font-family: Georgia;"&gt;Further reading:&lt;/span&gt;&lt;/p&gt;&lt;ul&gt;&lt;li&gt;&lt;a href="https://www.mikeash.com/pyblog/friday-qa-2017-07-14-swiftcodable.html" target="_blank" style="font-size: 1em;"&gt;&lt;span style="font-family: Georgia;"&gt;&lt;em&gt;Friday Q&amp;amp;A 2017-07-14: Swift.Codable &lt;/em&gt;&lt;/span&gt;&lt;span style="font-family: Georgia; font-size: 1em;"&gt;by Mike Ash &lt;/span&gt;&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="https://swiftunboxed.com/stdlib/json-encoder-encodable/" target="_blank"&gt;&lt;em&gt;JSON with Encoder and Encodable&lt;/em&gt; at [swift unboxed]&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="https://stackoverflow.com/questions/45787760/implementing-a-custom-decoder-in-swift-4" target="_blank"&gt;XML encoder/decoder by Shawn Moore&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;
					
				&lt;/div&gt;
			</description>
			<pubDate>Wed, 07 Feb 2018 10:43:21 -0500</pubDate>
			<guid>https://www.contracorner.com/stuff/json-and-codable.html</guid>
            
			
		</item>
		<item>
			<title>Enum Craziness</title>
			<link>https://www.contracorner.com/stuff/enum-craziness.html</link>
			<description>
				&lt;div class="article-summary"&gt;&lt;p&gt;Swift 4 enums kinda drove me crazy until I learned to love the bomb. Let’s look at this example enum type:&lt;/p&gt;
					
					&lt;div class="first wide center"&gt;
						&lt;div style="" class="graphic"&gt;
							&lt;div class="figure-content HTMLElement"&gt;
								&lt;div style="text-align:end; font-style: italic"&gt;Swift 4&lt;/div&gt;
&lt;pre&gt;&lt;code class="language-swift"&gt;enum Color {
    case red(Int8)
    case green(Int8)
    case blue(Int8)
}&lt;/code&gt;&lt;/pre&gt;
							&lt;/div&gt;
						&lt;/div&gt;
					&lt;/div&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;There’s no way to enumerate all values in the enum. That’s probably because of associated values and the lack of a &lt;span style="font-family: Menlo;"&gt;case&lt;/span&gt; attribute. Enums can have associated values, and that turns each case into a static factory function:&lt;/p&gt;
					
					&lt;div class="not-first-item graphic-container wide center ImageElement"&gt;
						&lt;div class="graphic"&gt;
							&lt;div class="figure-content"&gt;
								&lt;!-- sandvox.ImageElement --&gt;&lt;img src="https://www.contracorner.com/_Media/screen-shot-2018-02-06-at_med.png" alt="Screen Shot 2018-02-06 at 16.10.51" width="430" height="23" /&gt;
								&lt;!-- /sandvox.ImageElement --&gt;&lt;/div&gt;
						&lt;/div&gt;
					&lt;/div&gt;&lt;p&gt;Suppose there were a&lt;span style="font-size: 1em;"&gt; &lt;/span&gt;&lt;span style="font-size: 1em; font-family: Menlo;"&gt;Case&lt;/span&gt;&lt;span style="font-size: 1em;"&gt; type and &lt;/span&gt;&lt;span style="font-family: Menlo;"&gt;case&lt;/span&gt;&lt;span style="font-size: 1em;"&gt; attribute. The &lt;/span&gt;&lt;span style="font-size: 1em; font-family: Menlo;"&gt;Case&lt;/span&gt;&lt;span style="font-size: 1em;"&gt; type might be a kind of associated ty&lt;/span&gt;pe&lt;span style="font-size: 1em;"&gt; of the enum (though I don&lt;/span&gt;’&lt;span style="font-size: 1em;"&gt;t think that strictly applies here, but go with it a moment). &lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-size: 1em;"&gt;Then &lt;/span&gt;&lt;span style="font-family: Menlo;"&gt;Color.Cases&lt;/span&gt;&lt;span style="font-size: 1em;"&gt; would be &lt;/span&gt;&lt;span style="font-family: Menlo;"&gt;[Color.Case]&lt;/span&gt;&lt;span style="font-size: 1em;"&gt;, or perhaps a set of &lt;/span&gt;&lt;span style="font-family: Menlo;"&gt;Color.Case&lt;/span&gt;&lt;span style="font-size: 1em;"&gt; if it&lt;/span&gt;’&lt;span style="font-size: 1em;"&gt;s more sensible for the values to have no &lt;/span&gt;order.&lt;/p&gt;&lt;p&gt;&lt;span style="font-size: 1em;"&gt;More valuable would be a way to ask &lt;/span&gt;“&lt;span style="font-size: 1em;"&gt;is this Color value some red?&lt;/span&gt;”&lt;span style="font-size: 1em;"&gt; Right now you have to write &lt;/span&gt;“&lt;span style="font-family: Menlo;"&gt;if case .red = value { … }&lt;/span&gt;”&lt;span style="font-size: 1em;"&gt;. Even more valuable is a way to ask &lt;/span&gt;“&lt;span style="font-size: 1em;"&gt;Are these two the same Color case?&lt;/span&gt;”&lt;span style="font-size: 1em;"&gt; You cannot write an &lt;/span&gt;expression for that today.&lt;/p&gt;&lt;p&gt;&lt;span style="font-size: 1em;"&gt;What about the associated values? That&lt;/span&gt;’&lt;span style="font-size: 1em;"&gt;s complicates the notion of equality to the point Swift cannot make any useful assumptions for you.&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-size: 1em;"&gt;What I have done is this, on a type by type basis, as needed. &lt;/span&gt;&lt;/p&gt;
					
					&lt;div class="not-first-item wide center"&gt;
						&lt;div style="" class="graphic"&gt;
							&lt;div class="figure-content HTMLElement"&gt;
								&lt;div style="text-align:end; font-style: italic"&gt;Swift 4&lt;/div&gt;
&lt;pre&gt;&lt;code class="language-swift"&gt;extension Color : Equatable {
    /// Operator is true if the two enums are the same vaue, ignoring the associated values.
    /// See .identical(rhs) if you want to compare the associated values as well.
    static func ==(lhs: Color, rhs: Color) -&gt; Bool {
        if case .red = lhs, case .red = rhs { return true }
        if case .green = lhs, case .green = rhs { return true }
        if case .blue = lhs, case .blue = rhs { return true }
        return false
    }
    
    static func ===(lhs: Color, rhs: Color) -&gt; Bool {
        if case .red(let lhsv) = lhs, case .red(let rhsv) = rhs, lhsv == rhsv { return true }
        if case .green(let lhsv) = lhs, case .green(let rhsv) = rhs, lhsv == rhsv { return true }
        if case .blue(let lhsv) = lhs, case .blue(let rhsv) = rhs, lhsv == rhsv { return true }
        return false
    }
}

let c1 = Color.red(64)
let c2 = Color.red(64)
let c3 = Color.red(5)
let d4 = Color.green(64)
c1 == c2    // true
c1 === c2   // true
c1 == c3    // true
c1 === c3   // false
c1 == d4    // false
c1 === d4   // false&lt;/code&gt;&lt;/pre&gt;
							&lt;/div&gt;
						&lt;/div&gt;
					&lt;/div&gt;&lt;p&gt;&lt;span style="font-size: 1em;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/p&gt;
					
				&lt;/div&gt;
			</description>
			<pubDate>Tue, 06 Feb 2018 16:04:53 -0500</pubDate>
			<guid>https://www.contracorner.com/stuff/enum-craziness.html</guid>
            
			
		</item>
		<item>
			<title>Add a document type</title>
			<link>https://www.contracorner.com/stuff/add-a-document-type.html</link>
			<description>
				&lt;div class="article-summary"&gt;&lt;p&gt;Supported file types and extensions are registered in the Info.plist file. Everything you read will say to edit that file directly, but there is a better way. Quoting the Q&amp;amp;A page: &lt;span style="font-family: 'Lucida Grande', 'Lucida Sans Unicode', Helvetica, Arial, Verdana, sans-serif; font-size: 1em;"&gt;To add the document type do the following:&lt;/span&gt;&lt;/p&gt;&lt;ol class="ol"&gt;&lt;li class="li"&gt;&lt;span style="font-family: 'Lucida Grande', 'Lucida Sans Unicode', Helvetica, Arial, Verdana, sans-serif;"&gt;In your Xcode project, select the target you want to add the document type to.&lt;/span&gt;&lt;/li&gt;&lt;li class="li"&gt;&lt;span style="font-family: 'Lucida Grande', 'Lucida Sans Unicode', Helvetica, Arial, Verdana, sans-serif;"&gt;Select the Info tab.&lt;/span&gt;&lt;/li&gt;&lt;li class="li"&gt;&lt;span style="font-family: 'Lucida Grande', 'Lucida Sans Unicode', Helvetica, Arial, Verdana, sans-serif;"&gt;Click on the disclosure button for Document Types to open the document types.&lt;/span&gt;&lt;/li&gt;&lt;li class="li"&gt;&lt;span style="font-family: 'Lucida Grande', 'Lucida Sans Unicode', Helvetica, Arial, Verdana, sans-serif;"&gt;Click the “+” button.&lt;/span&gt;&lt;/li&gt;&lt;li class="li"&gt;&lt;span style="font-family: 'Lucida Grande', 'Lucida Sans Unicode', Helvetica, Arial, Verdana, sans-serif;"&gt;In the newly created document type :&lt;/span&gt;&lt;ul class="ul"&gt;&lt;li class="li"&gt;&lt;span style="font-family: 'Lucida Grande', 'Lucida Sans Unicode', Helvetica, Arial, Verdana, sans-serif;"&gt;Type the name of the document type.&lt;/span&gt;&lt;/li&gt;&lt;li class="li"&gt;&lt;span style="font-family: 'Lucida Grande', 'Lucida Sans Unicode', Helvetica, Arial, Verdana, sans-serif;"&gt;In the “Types” section fill in the UTI for the new type.&lt;/span&gt;&lt;/li&gt;&lt;li class="li"&gt;&lt;span style="font-family: 'Lucida Grande', 'Lucida Sans Unicode', Helvetica, Arial, Verdana, sans-serif;"&gt;Provide an icon for the document.&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/li&gt;&lt;li class="li"&gt;&lt;span style="font-family: 'Lucida Grande', 'Lucida Sans Unicode', Helvetica, Arial, Verdana, sans-serif;"&gt;Click the disclosure triangle to open Additional document type properties.&lt;/span&gt;&lt;/li&gt;&lt;li class="li"&gt;&lt;span style="font-family: 'Lucida Grande', 'Lucida Sans Unicode', Helvetica, Arial, Verdana, sans-serif;"&gt;Click in the table to add a new key and value.&lt;/span&gt;&lt;ul class="ul"&gt;&lt;li class="li"&gt;&lt;span style="font-family: 'Lucida Grande', 'Lucida Sans Unicode', Helvetica, Arial, Verdana, sans-serif;"&gt;For the key value type: CFBundleTypeRole.&lt;/span&gt;&lt;/li&gt;&lt;li class="li"&gt;&lt;span style="font-family: 'Lucida Grande', 'Lucida Sans Unicode', Helvetica, Arial, Verdana, sans-serif;"&gt;For the value type: Editor.&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/li&gt;&lt;li class="li"&gt;&lt;span style="font-family: 'Lucida Grande', 'Lucida Sans Unicode', Helvetica, Arial, Verdana, sans-serif;"&gt;Click the + button to add another key/value pair.&lt;/span&gt;&lt;ul class="ul"&gt;&lt;li class="li"&gt;&lt;span style="font-family: 'Lucida Grande', 'Lucida Sans Unicode', Helvetica, Arial, Verdana, sans-serif;"&gt;For the key value type: LSHandlerRank.&lt;/span&gt;&lt;/li&gt;&lt;li class="li"&gt;&lt;span style="font-family: 'Lucida Grande', 'Lucida Sans Unicode', Helvetica, Arial, Verdana, sans-serif;"&gt;For the value type: Owner.&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/li&gt;&lt;/ol&gt;&lt;p&gt;&lt;span style="font-size: 1em;"&gt;There&lt;/span&gt;’&lt;span style="font-size: 1em;"&gt;s more on that same page:&lt;/span&gt;&lt;/p&gt;&lt;ul&gt;&lt;li&gt;&lt;span style="font-size: 1em;"&gt;Adding a custom UTI. If it’s a custom UTI you need to add it. This is how you associate file extensions with the UTI.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style="font-size: 1em;"&gt;How to test (hint: send the document as email)&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style="font-size: 1em;"&gt;Troubleshooting&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;Also look at the original documentation, though it doesn’t mention how XCode can help. See: &lt;a href="https://developer.apple.com/library/content/documentation/FileManagement/Conceptual/DocumentInteraction_TopicsForIOS/Articles/RegisteringtheFileTypesYourAppSupports.html" target="_blank" style="font-size: 1em;"&gt;Registering the File Types Your App Supports&lt;/a&gt; and &lt;a href="https://developer.apple.com/library/content/documentation/FileManagement/Conceptual/understanding_utis/understand_utis_intro/understand_utis_intro.html" target="_blank" style="font-size: 1em;"&gt;Uniform Type Identifiers&lt;/a&gt;.&lt;/p&gt;&lt;p&gt;Detailed info keys for document file types: &lt;a href="https://developer.apple.com/library/content/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html#//apple_ref/doc/uid/TP40009249-101685-TPXREF107" target="_blank" style="font-size: 1em;"&gt;CFBundleDocumentTypes&lt;/a&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-size: 1em;"&gt;h/t&lt;/span&gt;&lt;/p&gt;&lt;ul&gt;&lt;li&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="https://developer.apple.com/library/content/qa/qa1587/_index.html" target="_blank"&gt;Technical Q&amp;amp;A QA1587: &lt;span style="font-size: 1em;"&gt;How do I get my application to show up in the Open in... menu.&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="https://developer.apple.com/library/content/documentation/FileManagement/Conceptual/DocumentInteraction_TopicsForIOS/Articles/RegisteringtheFileTypesYourAppSupports.html" target="_blank"&gt;Registering the File Types Your App Supports&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="https://developer.apple.com/library/content/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html#//apple_ref/doc/uid/TP40009249-101685-TPXREF107" target="_blank"&gt;CFBundleDocumentTypes&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="https://developer.apple.com/library/content/documentation/FileManagement/Conceptual/understanding_utis/understand_utis_intro/understand_utis_intro.html" target="_blank"&gt;Uniform Type Identifiers&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/div&gt;
			</description>
			<pubDate>Sat, 17 Dec 2016 18:49:07 -0500</pubDate>
			<guid>https://www.contracorner.com/stuff/add-a-document-type.html</guid>
            
			
		</item>
		<item>
			<title>Swift Neologisms</title>
			<link>https://www.contracorner.com/stuff/neologisms.html</link>
			<description>
				&lt;div class="article-summary"&gt;&lt;p&gt;Swift evolves and stuff gets renamed. Here is a score card of the ones that trip me.&lt;/p&gt;
					&lt;div class="first wide center"&gt;
						&lt;div style="" class="graphic"&gt;
							&lt;div class="figure-content HTMLElement"&gt;
								&lt;table&gt;
								&lt;tr&gt;
								        &lt;th colspan="2" style="text-align:left"&gt;Was&lt;/th&gt;
								        &lt;th&gt;&lt;/th&gt;
								        &lt;th colspan="2" style="text-align:left"&gt;Now&lt;/th&gt;
								        
								&lt;/tr&gt;
								    &lt;tr&gt;
								        &lt;th style="text-align:left"&gt;Name&lt;/th&gt;
								        &lt;th style="text-align:center"&gt;Swift&lt;/th&gt;
								        &lt;th&gt;&lt;/th&gt;
								        &lt;th style="text-align:left"&gt;Name&lt;/th&gt;
								        &lt;th style="text-align:center"&gt;Swift&lt;/th&gt;
								        
								&lt;/tr&gt;
								    &lt;tr&gt;
								        &lt;td&gt;Printable &lt;/td&gt;
								        &lt;td style="text-align:center"&gt;1 &lt;/td&gt;
								        &lt;td&gt;→&lt;/td&gt;
								        &lt;td&gt;CustomStringConvertible &lt;/td&gt;
								        &lt;td style="text-align:center"&gt;2 &lt;/td&gt;
								        
								&lt;/tr&gt;
								    &lt;tr&gt;
								        &lt;td&gt;DebugPrintable &lt;/td&gt;
								        &lt;td style="text-align:center"&gt;1 &lt;/td&gt;
								        &lt;td&gt;→&lt;/td&gt;
								        &lt;td&gt;CustomDebugStringConvertible &lt;/td&gt;
								        &lt;td style="text-align:center"&gt;2 &lt;/td&gt;
								        
								&lt;/tr&gt;
								&lt;/table&gt;
							&lt;/div&gt;
						&lt;/div&gt;
					&lt;/div&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;
				&lt;/div&gt;
			</description>
			<pubDate>Fri, 16 Dec 2016 19:42:43 -0500</pubDate>
			<guid>https://www.contracorner.com/stuff/neologisms.html</guid>
            
			
		</item>
		<item>
			<title>Table row reordering, deleting, inserting</title>
			<link>https://www.contracorner.com/stuff/table-row-reordering-deleti.html</link>
			<description>
				&lt;div class="article-summary"&gt;&lt;p&gt;https://developer.apple.com/library/content/documentation/UserExperience/Conceptual/TableView_iPhone/ManageReorderRow/ManageReorderRow.html&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;/div&gt;
			</description>
			<pubDate>Sat, 26 Nov 2016 09:20:31 -0500</pubDate>
			<guid>https://www.contracorner.com/stuff/table-row-reordering-deleti.html</guid>
            
			
		</item>
		<item>
			<title>UITabBarController in UINavigationController</title>
			<link>https://www.contracorner.com/stuff/uitabbarcontroller-in-uinav.html</link>
			<description>
				&lt;div class="article-summary"&gt;&lt;p&gt;It can’t be done.&lt;/p&gt;&lt;p&gt;You can fake it by making your own tab controller, so long as you figure out a way to honor the surrounding UINavigationController, meaning a navigation bar atop and optional (disallowed?) tool bar below all tabbed views. &lt;/p&gt;&lt;p&gt;Or you can use a modal segue to display the UITabBarController. That means:&lt;/p&gt;&lt;ol&gt;&lt;li&gt;Convert the class to UIViewController.&lt;/li&gt;&lt;li&gt;Make it implement UITabBarDelegate (if you wish). That means overridden delegate methods are no longer there to override.&lt;/li&gt;&lt;li&gt;Create a fake UINavigationBar since you cannot embed thi&lt;/li&gt;&lt;/ol&gt;&lt;p&gt;adding a navigation bar with a Done/Close bar button and perhaps an exit segue.&lt;/p&gt;&lt;p&gt;&lt;span style="font-style: italic;"&gt;h/t&lt;/span&gt;&lt;/p&gt;&lt;ul&gt;&lt;li&gt;&lt;a href="http://www.alexmedearis.com/uitabbarcontroller-inside-a-uinavigationcontroller/" target="_blank"&gt;UITabBarController inside a UINavigationController&lt;/a&gt; (2011) describes a third method, making the tab bar controller the root and then doginscreen swapping (I think).&lt;/li&gt;&lt;li&gt;Stack Overflow "&lt;a href="http://stackoverflow.com/questions/576764/tab-bar-controller-inside-a-navigation-controller-or-sharing-a-navigation-root" target="_blank"&gt;Tab bar controller inside a navigation controller, or sharing a navigation root view&lt;/a&gt;” (2009) describes the first method.&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;/div&gt;
			</description>
			<pubDate>Wed, 23 Nov 2016 20:57:34 -0500</pubDate>
			<guid>https://www.contracorner.com/stuff/uitabbarcontroller-in-uinav.html</guid>
            
			
		</item>
		<item>
			<title>Making code documentation</title>
			<link>https://www.contracorner.com/stuff/making-code-documentation.html</link>
			<description>
				&lt;div class="article-summary"&gt;&lt;p&gt;&lt;span style="background-color: transparent;"&gt;Here is a cheat sheet&lt;/span&gt;&lt;/p&gt;&lt;p style="font-stretch: normal; font-size: 30px; font-family: &amp;quot;Helvetica Neue&amp;quot;; color: rgb(0, 0, 0);"&gt;&lt;strong&gt;Swift Documentation Comments&lt;/strong&gt;&lt;/p&gt;&lt;p style="font-stretch: normal;"&gt;&lt;span style="font-stretch: normal; font-size: 14px; font-family: Menlo; color: rgb(0, 0, 0);"&gt;// MARK:&lt;/span&gt;&lt;span style="font-size: 14px; font-family: Menlo; color: rgb(0, 0, 0);"&gt; - heading&lt;br /&gt;// TODO: comment&lt;br /&gt;// FIXME: comment&lt;/span&gt;&lt;/p&gt;&lt;p style="font-stretch: normal; font-family: &amp;quot;Helvetica Neue&amp;quot;; color: rgb(0, 0, 0);"&gt;&lt;strong&gt;Block and single line comments&lt;/strong&gt;&lt;/p&gt;&lt;p style="font-stretch: normal;"&gt;&lt;span style="font-size: 14px; font-family: Menlo; color: rgb(0, 0, 0);"&gt;/**&lt;/span&gt;&lt;span style="font-size: 14px; font-family: Menlo; color: rgb(0, 0, 0);" class="Apple-tab-span"&gt;	&lt;/span&gt;&lt;span style="font-size: 14px; font-family: Menlo; color: rgb(0, 0, 0);"&gt;&lt;br /&gt; *&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0); font-family: Menlo; font-size: 14px;" class="Apple-tab-span"&gt;	&lt;/span&gt;&lt;span style="font-size: 14px; font-family: Menlo; color: rgb(0, 0, 0);"&gt;&lt;br /&gt; */&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0); font-family: Menlo; font-size: 14px;" class="Apple-tab-span"&gt;	&lt;/span&gt;&lt;/p&gt;&lt;p&gt;Or&lt;/p&gt;&lt;p style="font-stretch: normal;"&gt;&lt;span style="font-size: 14px; font-family: Menlo; color: rgb(0, 0, 0);"&gt;///&lt;br /&gt;///&lt;br /&gt;///&lt;/span&gt;&lt;/p&gt;&lt;p style="font-stretch: normal; font-size: 14px; font-family: &amp;quot;Helvetica Neue&amp;quot;; color: rgb(0, 0, 0);"&gt;Example:&lt;/p&gt;&lt;p style="font-stretch: normal; font-size: 14px; font-family: Menlo; color: rgb(0, 0, 0);"&gt;/// &lt;span style="background-color: rgb(255, 251, 0);"&gt;One line description of function.&lt;br /&gt;&lt;/span&gt;&lt;span style="background-color: transparent;"&gt;///&lt;br /&gt;/// &lt;/span&gt;&lt;span style="background-color: rgb(255, 150, 141);"&gt;A detailed description&lt;br /&gt;&lt;/span&gt;&lt;span style="background-color: transparent;"&gt;/// &lt;/span&gt;&lt;span style="background-color: rgb(255, 150, 141);"&gt;may be placed on the remaining lines&lt;br /&gt;&lt;/span&gt;&lt;span style="background-color: transparent;"&gt;/// &lt;/span&gt;&lt;span style="background-color: rgb(136, 250, 78);"&gt;- parameter &lt;strong&gt;something&lt;/strong&gt;:&lt;/span&gt;&lt;span style="background-color: transparent;"&gt; the parameter description&lt;br /&gt;/// &lt;/span&gt;&lt;span style="background-color: rgb(136, 250, 78);"&gt;- throws:&lt;/span&gt;&lt;span style="background-color: transparent;"&gt; &lt;/span&gt;&lt;span style="background-color: rgb(115, 253, 234);"&gt;`&lt;/span&gt;&lt;span style="color: rgb(1, 123, 118); background-color: rgb(115, 253, 234);"&gt;ErrorCode.noDevicePersister&lt;/span&gt;&lt;span style="background-color: rgb(115, 253, 234);"&gt;`&lt;/span&gt;&lt;span style="background-color: transparent;"&gt; The &lt;/span&gt;&lt;span style="background-color: rgb(115, 253, 234);"&gt;`&lt;/span&gt;&lt;span style="color: rgb(1, 123, 118); background-color: rgb(115, 253, 234);"&gt;something&lt;/span&gt;&lt;span style="background-color: rgb(115, 253, 234);"&gt;`&lt;/span&gt;&lt;span style="background-color: transparent;"&gt; parameter was blank.&lt;br /&gt;/// &lt;/span&gt;&lt;span style="background-color: rgb(136, 250, 78);"&gt;- returns:&lt;/span&gt;&lt;span style="background-color: transparent;"&gt; a result of the computation&lt;br /&gt;func think(about &lt;/span&gt;&lt;strong&gt;something&lt;/strong&gt;&lt;span style="background-color: transparent;"&gt;: String) throws -&amp;gt; String {   &lt;/span&gt;&lt;/p&gt;
					
					&lt;div class="first graphic-container wide center ImageElement"&gt;
						&lt;div class="graphic"&gt;
							&lt;div class="figure-content"&gt;
								&lt;!-- sandvox.ImageElement --&gt;&lt;img src="https://www.contracorner.com/_Media/screen-shot-2018-04-17-at_med.png" alt="Screen Shot 2018-04-17 at 20.33.28" width="365" height="234" /&gt;
								&lt;!-- /sandvox.ImageElement --&gt;&lt;/div&gt;
						&lt;/div&gt;
					&lt;/div&gt;&lt;p&gt;&lt;strong&gt;Longer parameter lists:&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-size: 14px; font-family: Menlo; color: rgb(0, 0, 0);"&gt;/// - Parameters:&lt;br /&gt;///     - x: The *x* component of the vector.&lt;br /&gt;///     - y: The *y* component of the vector.&lt;br /&gt;///     - z: The *z* component of the vector.&lt;br /&gt;func magnitude3D(x: Double, y: Double, z: Double) -&amp;gt; Double {&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;strong&gt;Description fields headers&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;Algorithm/Safety Information: &lt;em&gt;Precondition, Postcondition, Requires, Invariant, Complexity, Important, Warning&lt;/em&gt;&lt;/p&gt;&lt;p&gt;Metadata: &lt;em&gt;Author, Authors, Copyright, Date, SeeAlso, Since, Version&lt;/em&gt;&lt;/p&gt;&lt;p&gt;General Notes &amp;amp; Exhortations: &lt;em&gt;Attention, Bug, Experiment, Note, Remark, ToDo&lt;/em&gt;&lt;/p&gt;&lt;p&gt;&lt;strong&gt;Code blocks&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;Indent code block by at least 4 spaces.&lt;/p&gt;&lt;p&gt;&lt;span style="font-size: 14px; font-family: Menlo; color: rgb(0, 0, 0);"&gt;/**&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0); font-family: Menlo; font-size: 14px; background-color: transparent;"&gt;    The area of the `Shape` instance.&lt;br /&gt;&lt;br /&gt;    Computation depends on the shape of the instance. &lt;br /&gt;    For a triangle, `area` will be equivalent to:&lt;br /&gt;&lt;br /&gt;        let height = triangle.calculateHeight()&lt;br /&gt;        let area = triangle.base * height / 2&lt;br /&gt;*/&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="background-color: transparent;"&gt;Fence unindented code blocks using triple backtick (```) or tilde (~):&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-size: 14px; font-family: Menlo; color: rgb(0, 0, 0);"&gt;```&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0); font-family: Menlo; font-size: 14px; background-color: transparent;"&gt;// Circles:&lt;br /&gt;let perimeter = circle.radius * 2 * CGFloat(M_PI)&lt;br /&gt;&lt;br /&gt;// Other shapes:&lt;br /&gt;let perimeter = shape.sides.map { $0.length }&lt;br /&gt;                           .reduce(0, combine: +)&lt;br /&gt;```&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="background-color: transparent;"&gt;For further reading:&lt;/span&gt;&lt;/p&gt;&lt;ul&gt;&lt;li&gt;&lt;span style="font-size: 1em;"&gt;&lt;a href="https://objcsharp.wordpress.com/2013/09/24/how-to-generate-beautiful-apple-style-documentation-in-xcode-5/" target="_blank"&gt;How to Generate Beautiful Apple-Style Documentation in Xcode 5&lt;/a&gt;&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;
					
				&lt;/div&gt;
			</description>
			<pubDate>Wed, 23 Nov 2016 16:54:40 -0500</pubDate>
			<guid>https://www.contracorner.com/stuff/making-code-documentation.html</guid>
            
			
		</item>
		<item>
			<title>There is no enumeration for all cases on an enum [Swift 3.0.1]</title>
			<link>https://www.contracorner.com/stuff/there-is-no-enumeration-for.html</link>
			<description>
				&lt;div class="article-summary"&gt;&lt;p&gt;&lt;span style="font-size: 1em;"&gt;My practice is to define a static constant array inside the enum which enumerates them. This is error prone when a case is added and the array isn’t updated.&lt;/span&gt;&lt;/p&gt;&lt;p&gt;#ThingsMomNeverToldMe&lt;span style="font-size: 1em;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/p&gt;&lt;/div&gt;
			</description>
			<pubDate>Sun, 13 Nov 2016 20:29:40 -0500</pubDate>
			<guid>https://www.contracorner.com/stuff/there-is-no-enumeration-for.html</guid>
            
			
		</item>
		<item>
			<title>Halting test after XCTAssert fails</title>
			<link>https://www.contracorner.com/stuff/halting-test-after-xctasser.html</link>
			<description>
				&lt;div class="article-summary"&gt;&lt;p&gt;While it can be useful to have a test continue after an assertion failure, if you have code following the XCTAssert then it mustn’t depend on the condition being successful.&lt;/p&gt;&lt;p&gt;If you need each test method to stop if there is a failure, set this flag to false:&lt;/p&gt;
					
					&lt;div class="first wide center"&gt;
						&lt;div style="" class="graphic"&gt;
							&lt;div class="figure-content HTMLElement"&gt;
								&lt;div style="text-align:end; font-style: italic"&gt;Swift 3.1&lt;/div&gt;
&lt;pre&gt;&lt;code class="language-swift"&gt;self.continueAfterFailure = false&lt;/code&gt;&lt;/pre&gt;
							&lt;/div&gt;
						&lt;/div&gt;
					&lt;/div&gt;&lt;p&gt;You can set this in any test and it will be reset between tests to true. That way it is in effect for that test only. You can set it for the whole test class in the setUp function:&lt;/p&gt;
					
					&lt;div class="not-first-item wide center"&gt;
						&lt;div style="" class="graphic"&gt;
							&lt;div class="figure-content HTMLElement"&gt;
								&lt;div style="text-align:end; font-style: italic"&gt;Swift 3.1&lt;/div&gt;
&lt;pre&gt;&lt;code class="language-swift"&gt;override func setUp() {
    super.setUp()
    self.continueAfterFailure = false
}&lt;/code&gt;&lt;/pre&gt;
							&lt;/div&gt;
						&lt;/div&gt;
					&lt;/div&gt;&lt;p style="font-style: italic;"&gt;&lt;span style="font-size: 1em;"&gt;h/t&lt;/span&gt;&lt;/p&gt;&lt;ul&gt;&lt;li&gt;Marek R on Stack Overflow: &lt;a href="http://stackoverflow.com/questions/23845923/xctasserttrue-doesnt-stop-routine"&gt;XCTAssertTrue doesn't stop routine&lt;/a&gt;.&lt;/li&gt;&lt;li&gt;Apple API Reference: &lt;a href="https://developer.apple.com/reference/xctest/xctestcase/1496260-continueafterfailure"&gt;XCTest &amp;gt; XCTestCase &amp;gt; continueAfterFailure&lt;/a&gt;.&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;
					
				&lt;/div&gt;
			</description>
			<pubDate>Sun, 06 Nov 2016 22:41:45 -0500</pubDate>
			<guid>https://www.contracorner.com/stuff/halting-test-after-xctasser.html</guid>
            
			
		</item>
		<item>
			<title>Swift protocol which requires a specific type of sequence</title>
			<link>https://www.contracorner.com/stuff/swift-protocol-which-requir.html</link>
			<description>
				&lt;div class="article-summary"&gt;&lt;p&gt;I really can’t explain it any better than the hat tip did! &lt;span style="font-size: 1em;"&gt;What you want:&lt;/span&gt;&lt;/p&gt;
					
					&lt;div class="first wide center"&gt;
						&lt;div style="" class="graphic"&gt;
							&lt;div class="figure-content HTMLElement"&gt;
								&lt;div style="text-align:end; font-style: italic"&gt;Swift 3.0&lt;/div&gt;
&lt;pre&gt;&lt;code class="language-swift"&gt;protocol HasSequenceOfInts {
    var seq : SequenceType&lt;Int&gt; { get }
}&lt;/code&gt;&lt;/pre&gt;
							&lt;/div&gt;
						&lt;/div&gt;
					&lt;/div&gt;&lt;p&gt;What you need to write:&lt;/p&gt;
					
					&lt;div class="not-first-item wide center"&gt;
						&lt;div style="" class="graphic"&gt;
							&lt;div class="figure-content HTMLElement"&gt;
								&lt;div style="text-align:end; font-style: italic"&gt;Swift 3.0&lt;/div&gt;
&lt;pre&gt;&lt;code class="language-swift"&gt;protocol HasSequenceOfInts {
    var seq : AnySequence&lt;Int&gt; { get }
}
class ArrayOfInts : HasSequenceOfInts {
    var _seq : [Int] = [0,1,2]
    var seq : AnySequence&lt;Int&gt; {
        get {
            return AnySequence(self._seq)
        }
    }
}&lt;/code&gt;&lt;/pre&gt;
							&lt;/div&gt;
						&lt;/div&gt;
					&lt;/div&gt;&lt;p style="font-style: italic;"&gt;&lt;span style="font-size: 1em;"&gt;h/t&lt;/span&gt;&lt;/p&gt;&lt;ul&gt;&lt;li&gt;Rob Napier on Stack Overflow: &lt;a href="http://stackoverflow.com/a/33845175/2297442"&gt;Define a Swift protocol which requires a specific type of sequence&lt;/a&gt;.&lt;/li&gt;&lt;/ul&gt;
					
				&lt;/div&gt;
			</description>
			<pubDate>Thu, 15 Sep 2016 20:02:53 -0400</pubDate>
			<guid>https://www.contracorner.com/stuff/swift-protocol-which-requir.html</guid>
            
			
		</item>
		<item>
			<title>Popping versus Unwinding</title>
			<link>https://www.contracorner.com/stuff/unwind-segues-in-swift.html</link>
			<description>
				&lt;div class="article-summary"&gt;&lt;p&gt;Use an unwind segue when a UINavigationController pop isn’t what you want. For example, when going back beyond the current &lt;span style="font-size: 1em;"&gt;UINavigationController stack. Note that you can pop one or more levels without needing an unwind when you are within the UINavigationController view stack. &lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-size: 1em;"&gt;When just using popping you will want to send the data model(s) to the child view controllers. They act on it directly.&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-size: 1em;"&gt;When unwinding you can be more abstract and only pass the essential data pieces, or the model if you still want. Unwinding can communicate back since the child view controller lingers long enough to be scoured for its data.&lt;/span&gt;&lt;/p&gt;&lt;p style="font-size: 16px;"&gt;&lt;span style="font-family: Georgia;"&gt;&lt;strong&gt;Unwind segues in Swift&lt;/strong&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;Two essential preparatory reading assignments are:&lt;/p&gt;&lt;p&gt;Apple &lt;a href="https://developer.apple.com/library/ios/technotes/tn2298/_index.html" target="_blank"&gt;&lt;span style="font-size: 12px; font-family: Helvetica; color: rgb(0, 0, 0);"&gt;Technical Note TN2298: &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px;"&gt;Using Unwind Segues&lt;/span&gt;&lt;/a&gt;&lt;/p&gt;&lt;p&gt;Stack Overflow: &lt;a href="http://stackoverflow.com/questions/12561735/what-are-unwind-segues-for-and-how-do-you-use-them" target="_blank"&gt;What are Unwind segues for and how do you use them?&lt;/a&gt;&lt;/p&gt;&lt;p&gt;In Swift 2.3 / iOS 10.0 (I don’t know which is the critical factor) the method signature must be:&lt;/p&gt;
					
					&lt;div class="first wide right"&gt;
						&lt;div style="" class="graphic"&gt;
							&lt;div class="figure-content HTMLElement"&gt;
								&lt;div style="text-align:end; font-style: italic"&gt;Swift 2.3&lt;/div&gt;
&lt;pre&gt;&lt;code class="language-swift"&gt;@IBAction func unwindToTeachView(withUnwindSegue unwindSegue: UIStoryboardSegue) {
        ...
    }&lt;/code&gt;&lt;/pre&gt;
							&lt;/div&gt;
						&lt;/div&gt;
					&lt;/div&gt;
					&lt;p&gt;The critical elements are &lt;/p&gt;&lt;ul&gt;&lt;li&gt;&lt;span style="font-family: 'Andale Mono';"&gt;@IBAction&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style="font-family: 'Andale Mono';"&gt;func&lt;/span&gt;&lt;/li&gt;&lt;li&gt;One parameter&lt;/li&gt;&lt;li&gt;&lt;span style="font-family: 'Andale Mono';"&gt;withUnwindSegue&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style="font-family: 'Andale Mono';"&gt;UIStoryBoardSegue&lt;/span&gt;&lt;/li&gt;&lt;li&gt;No return value&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;I found this out on my own. I contributed this answer to another Stack Overflow question, &lt;a href="http://stackoverflow.com/questions/24029586/xcode-6-storyboard-unwind-segue-with-swift-not-connecting-to-exit/38944645#38944645" target="_blank"&gt;Xcode 6 Storyboard Unwind Segue with Swift Not Connecting to Exit&lt;/a&gt;.&lt;/p&gt;
					
				&lt;/div&gt;
			</description>
			<pubDate>Sun, 14 Aug 2016 13:01:18 -0400</pubDate>
			<guid>https://www.contracorner.com/stuff/unwind-segues-in-swift.html</guid>
            
			
		</item>
		<item>
			<title>Dismiss keyboard</title>
			<link>https://www.contracorner.com/stuff/dismiss-keyboard.html</link>
			<description>
				&lt;div class="article-summary"&gt;&lt;p&gt;&lt;strong&gt;Dismiss keyboard after pressing Return&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;Set the view controller delegate to an object which implements the method &lt;span style="font-size: 12px; font-family: 'Andale Mono';"&gt;textFieldShouldReturn(_)&lt;/span&gt;&lt;span style="font-family: Helvetica; font-size: 12px;"&gt; in the protocol &lt;/span&gt;&lt;span style="font-size: 12px; font-family: 'Andale Mono';"&gt;UITextFieldDelegate&lt;/span&gt;&lt;span style="font-size: 12px; font-family: Helvetica;"&gt;.&lt;/span&gt;&lt;/p&gt;
					
					&lt;div class="first wide right"&gt;
						&lt;div style="" class="graphic"&gt;
							&lt;div class="figure-content HTMLElement"&gt;
								&lt;div style="text-align:end; font-style: italic"&gt;Swift 2.3&lt;/div&gt;
&lt;pre&gt;&lt;code class="language-swift"&gt;// MARK: UITextFieldDelegate

/**
    When the user hits the return or "Done" button on a text field, we want 
    the keyboard to go away.
*/
func textFieldShouldReturn(_ textField: UITextField) -&gt; Bool {
    textField.resignFirstResponder()
    return true
}&lt;/code&gt;&lt;/pre&gt;
							&lt;/div&gt;
						&lt;/div&gt;
					&lt;/div&gt;
					&lt;p&gt;h/t&lt;/p&gt;&lt;ul&gt;&lt;li&gt;Apple’s sample code “MyLife” &lt;a href="https://developer.apple.com/library/prerelease/content/samplecode/MyLife/Listings/MyLife_PersonViewController_swift.html#//apple_ref/doc/uid/TP40017272-MyLife_PersonViewController_swift-DontLinkElementID_6"&gt;PersonViewController.swift&lt;/a&gt;: &lt;/li&gt;&lt;/ul&gt;
					
				&lt;/div&gt;
			</description>
			<pubDate>Sun, 14 Aug 2016 09:58:12 -0400</pubDate>
			<guid>https://www.contracorner.com/stuff/dismiss-keyboard.html</guid>
            
			
		</item>
		<item>
			<title>Show master view first</title>
			<link>https://www.contracorner.com/stuff/show-master-view-first.html</link>
			<description>
				&lt;div class="article-summary"&gt;&lt;p&gt;On an iPhone (compact layout), UISplitViewController likes to show the detail view when the app starts. To make the master view the first one shown, do this:&lt;/p&gt;
					
					&lt;div class="first wide right"&gt;
						&lt;div style="" class="graphic"&gt;
							&lt;div class="figure-content HTMLElement"&gt;
								&lt;div style="text-align:end; font-style: italic"&gt;Swift 2.3&lt;/div&gt;
&lt;pre&gt;&lt;code class="language-swift"&gt;class MyMasterTableViewController: UITableViewController, UISplitViewControllerDelegate {
    private var collapseDetailViewController = true

    override func viewDidLoad() {
        super.viewDidLoad()

        splitViewController?.delegate = self
    }

    // MARK: - UITableViewDelegate

    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        collapseDetailViewController = false
    }

    // MARK: - UISplitViewControllerDelegate

    func splitViewController(splitViewController: UISplitViewController, collapseSecondaryViewController secondaryViewController: UIViewController, ontoPrimaryViewController primaryViewController: UIViewController) -&gt; Bool {
        return collapseDetailViewController
    }
}&lt;/code&gt;&lt;/pre&gt;
							&lt;/div&gt;
						&lt;/div&gt;
					&lt;/div&gt;&lt;p&gt;h/t&lt;/p&gt;&lt;ul&gt;&lt;li&gt;http://nshipster.com/uisplitviewcontroller/&lt;/li&gt;&lt;/ul&gt;
					
				&lt;/div&gt;
			</description>
			<pubDate>Sat, 13 Aug 2016 13:22:54 -0400</pubDate>
			<guid>https://www.contracorner.com/stuff/show-master-view-first.html</guid>
            
			
		</item>
		<item>
			<title>UITextView not scrolled to top</title>
			<link>https://www.contracorner.com/stuff/uitextview-not-scrolled-to.html</link>
			<description>
				&lt;div class="article-summary"&gt;&lt;p&gt;When populating a UITextView by code, which is typical when the text is not meant to be editable, very long text will not be scrolled to the top. Scrolling in fact seems to be kind of random, but that’s probably because I don’t perceive the rule pattern yet.&lt;/p&gt;&lt;p&gt;If the view is not overlapped by a navbar, then this should do the trick:&lt;/p&gt;
					
					&lt;div class="first wide right"&gt;
						&lt;div style="" class="graphic"&gt;
							&lt;div class="figure-content HTMLElement"&gt;
								&lt;div style="text-align:end; font-style: italic"&gt;Swift 2.3&lt;/div&gt;
&lt;pre&gt;&lt;code class="language-swift"&gt;override func viewDidLayoutSubviews() {  
    super.viewDidLayoutSubviews()  
    dispatch_async(dispatch_get_main_queue(), {  
        self.myTextView.scrollRangeToVisible(NSMakeRange(0, 0))  
    })  
}&lt;/code&gt;&lt;/pre&gt;
							&lt;/div&gt;
						&lt;/div&gt;
					&lt;/div&gt;&lt;p&gt;If the view is fitted under the navbar of a UINavigationController, then try this:&lt;/p&gt;
					
					&lt;div class="not-first-item wide right"&gt;
						&lt;div style="" class="graphic"&gt;
							&lt;div class="figure-content HTMLElement"&gt;
								&lt;div style="text-align:end; font-style: italic"&gt;Swift 2.3&lt;/div&gt;
&lt;pre&gt;&lt;code class="language-swift"&gt;override func viewDidLayoutSubviews() {  
    super.viewDidLayoutSubviews()  
    dispatch_async(dispatch_get_main_queue(), {  
        self.myTextView.contentOffset.y = -self.myTextView.contentInset.top  
    })  
}&lt;/code&gt;&lt;/pre&gt;
							&lt;/div&gt;
						&lt;/div&gt;
					&lt;/div&gt;&lt;p&gt;&lt;strong&gt;h/t&lt;/strong&gt;&lt;/p&gt;&lt;ul&gt;&lt;li&gt;https://forums.developer.apple.com/thread/18258&lt;/li&gt;&lt;/ul&gt;
					
				&lt;/div&gt;
			</description>
			<pubDate>Sat, 13 Aug 2016 12:30:01 -0400</pubDate>
			<guid>https://www.contracorner.com/stuff/uitextview-not-scrolled-to.html</guid>
            
			
		</item>
 	</channel>
</rss>