Skip to content

Commit 2b79390

Browse files
committed
Swift 2 updates and post statuses
1 parent 1aca5ea commit 2b79390

File tree

139 files changed

+560
-97
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

139 files changed

+560
-97
lines changed

2012-07-07-nsindexset.md

+22-2
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,35 @@ title: NSIndexSet
33
author: Mattt Thompson
44
category: Cocoa
55
tags: nshipster
6-
excerpt: "NSIndexSet (and its mutable counterpart, NSMutableIndexSet) is a sorted collection of unique unsigned integers. Think of it like an NSRange that supports non-contiguous series. It has wicked fast operations for finding indexes in ranges or set intersections, and comes with all of the convenience methods you'd expect in a Foundation collection class."
6+
excerpt: "NSIndexSet (like its mutable counterpart, NSMutableIndexSet) is a sorted collection of unique unsigned integers. Think of it like an NSRange that supports non-contiguous series. It has wicked fast operations for finding indexes in ranges or set intersections, and comes with all of the convenience methods you'd expect in a Foundation collection class."
7+
status:
8+
swift: 2.0
9+
reviewed: September 8, 2015
710
---
811

9-
`NSIndexSet` (and its mutable counterpart, `NSMutableIndexSet`) is a sorted collection of unique unsigned integers. Think of it like an `NSRange` that supports non-contiguous series. It has wicked fast operations for finding indexes in ranges or set intersections, and comes with all of the convenience methods you'd expect in a Foundation collection class.
12+
`NSIndexSet` (like its mutable counterpart, `NSMutableIndexSet`) is a sorted collection of unique unsigned integers. Think of it like an `NSRange` that supports non-contiguous series. It has wicked fast operations for finding indexes in ranges or set intersections, and comes with all of the convenience methods you'd expect in a Foundation collection class.
1013

1114
You'll find `NSIndexSet` used throughout the Foundation framework. Anytime a method gets multiple elements from a sorted collection, such as an array or a table view's data source, you can be sure that an `NSIndexSet` parameter will be somewhere in the mix.
1215

1316
If you look hard enough, you may start to find aspects of your data model that could be represented with `NSIndexSet`. For example AFNetworking uses an index set to represent HTTP response status codes: the user defines a set of "acceptable" codes (in the `2XX` range, by default), and the response is checked by using `containsIndex:`.
1417

18+
The Swift standard library includes `PermutationGenerator`, an often-overlooked type that dovetails nicely with `NSIndexSet`. `PermutationGenerator` wraps a collection and a sequence of indexes (sound familiar?) to allow easy iteration:
19+
20+
```swift
21+
let streetscape = ["Ashmead", "Belmont", "Clifton", "Douglas", "Euclid", "Fairmont",
22+
"Girard", "Harvard", "Irving", "Kenyon", "Lamont", "Monroe",
23+
"Newton", "Otis", "Perry", "Quincy"]
24+
25+
let selectedIndices = NSMutableIndexSet(indexesInRange: NSRange(0...2))
26+
selectedIndices.addIndex(5)
27+
selectedIndices.addIndexesInRange(NSRange(11...13))
28+
29+
for street in PermutationGenerator(elements: streetscape, indices: selectedIndices) {
30+
print(street)
31+
}
32+
// Ashmead, Belmont, Clifton, Fairmont, Monroe, Newton, Otis
33+
```
34+
1535
Here are a few more ideas to get you thinking in terms of index sets:
1636

1737
- Have a list of user preferences, and want to store which ones are switched on or off? Use a single `NSIndexSet` in combination with an `enum` `typedef`.

2012-07-14-nscache.md

+22
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ author: Mattt Thompson
44
category: Cocoa
55
tags: nshipster
66
excerpt: "Poor NSCache, always being overshadowed by NSMutableDictionary. It's as if no one knew how it provides all of that garbage collection behavior that developers take great pains to re-implement themselves."
7+
status:
8+
swift: 2.0
9+
reviewed: September 8, 2015
710
---
811

912
Poor `NSCache`, always being overshadowed by `NSMutableDictionary`. It's as if no one knew how it provides all of that garbage collection behavior that developers take great pains to re-implement themselves.
@@ -37,3 +40,22 @@ Read: don't use this method unless you work at Apple and know the original autho
3740
There's also a whole part about controlling whether objects are automatically evicted with `evictsObjectsWithDiscardedContent` & `<NSDiscardableContent>`, but it will probably just cause you more problems.
3841

3942
Despite all of this, developers should be using `NSCache` a lot more than they currently are. Anything in your project that you call a "cache", but isn't `NSCache` would be prime candidates for replacement. But if you do, just be sure to stick to the classics: `objectForKey:`, `setObject:forKey:` & `removeObjectForKey:`.
43+
44+
Still not convinved? As a parting gift, we'll even make it easier, via a little subscripting majick:
45+
46+
```swift
47+
extension NSCache {
48+
subscript(key: AnyObject) -> AnyObject? {
49+
get {
50+
return objectForKey(key)
51+
}
52+
set {
53+
if let value: AnyObject = newValue {
54+
setObject(value, forKey: key)
55+
} else {
56+
removeObjectForKey(key)
57+
}
58+
}
59+
}
60+
}
61+
```

2012-07-24-nssortdescriptor.md

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ author: Mattt Thompson
44
category: Cocoa
55
tags: nshipster
66
excerpt: "Sorting: it's the mainstay of Computer Science 101 exams and whiteboarding interview questions. But when was the last time you actually needed to know how to implement Quicksort yourself?"
7+
status:
8+
swift: 1.1
79
---
810

911
Sorting: it's the mainstay of Computer Science 101 exams and whiteboarding interview questions. But when was the last time you actually needed to know how to implement Quicksort yourself?

2012-07-31-nsdatecomponents.md

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ title: NSDateComponents
33
author: Mattt Thompson
44
category: Cocoa
55
excerpt: "NSDateComponents serves an important role in Foundation's date and time APIs. By itself, it's nothing impressive—just a container for information about a date (its month, year, day of month, week of year, or whether that month is a leap month). However, combined with NSCalendar, NSDateComponents becomes a remarkably convenient interchange format for calendar calculations."
6+
status:
7+
swift: 1.1
68
---
79

810
`NSDateComponents` serves an important role in Foundation's date and time APIs. By itself, it's nothing impressive—just a container for information about a date (its month, year, day of month, week of year, or whether that month is a leap month). However, combined with `NSCalendar`, `NSDateComponents` becomes a remarkably convenient interchange format for calendar calculations.

2012-08-06-cfstringtransform.md

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ author: Mattt Thompson
44
category: Cocoa
55
tags: nshipster, popular
66
excerpt: "NSString is the crown jewel of Foundation. But as powerful as it is, one would be remiss not to mention its toll-free bridged cousin, CFMutableString—or more specifically, CFStringTransform."
7+
status:
8+
swift: 1.2
79
---
810

911
There are two indicators that tell you everything you need to know about how nice a language is to use:

2012-08-13-nsincrementalstore.md

+3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ title: NSIncrementalStore
33
author: Mattt Thompson
44
category: Cocoa
55
excerpt: Even for a blog dedicated to obscure APIs, `NSIncrementalStore` sets a new standard. It was introduced in iOS 5, with no more fanfare than the requisite entry in the SDK changelog. Ironically, it is arguably the most important thing to come out of iOS 5, which will completely change the way we build apps from here on out.
6+
status:
7+
swift: 1.1
8+
reviewed: September 8, 2015
69
---
710

811
Even for a blog dedicated to obscure APIs, `NSIncrementalStore` brings a new meaning to the word "obscure".

2012-08-27-cfbag.md

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ author: Mattt Thompson
44
category: Cocoa
55
tags: nshipster
66
excerpt: "In the pantheon of collection data types in computer science, bag doesn't really have the same clout as lists, sets, associative arrays, trees, graphs, or priority queues. In fact, it's pretty obscure. You've probably never heard of it."
7+
status:
8+
swift: t.b.c
79
---
810

911
Objective-C is a language caught between two worlds.

2012-09-03-nslocale.md

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ author: Mattt Thompson
44
category: Cocoa
55
tags: nshipster
66
excerpt: "Internationalization is like flossing: everyone knows they should do it, but probably don't."
7+
status:
8+
swift: 1.1
79
---
810

911
Internationalization is like flossing: everyone knows they should do it, but probably don't.

2012-09-10-uiaccessibility.md

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ author: Mattt Thompson
44
category: Cocoa
55
tags: nshipster
66
excerpt: "Accessibility, like internationalization, is one of those topics that's difficult to get developers excited about. But as you know, NSHipster is all about getting developers excited about this kind of stuff."
7+
status:
8+
swift: n/a
79
---
810

911
> We all want to help one another, human beings are like that.

2012-09-17-nscharacterset.md

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ title: NSCharacterSet
33
author: Mattt Thompson
44
category: Cocoa
55
excerpt: "Foundation boasts one of the best, most complete implementations of strings around. But a string implementation is only as good as the programmer who wields it. So this week, we're going to explore some common uses--and misuses--of an important part of the Foundation string ecosystem: NSCharacterSet."
6+
status:
7+
swift: 1.1
68
---
79

810
As mentioned [previously](http://nshipster.com/cfstringtransform/), Foundation boasts one of the best, most complete implementations of strings around.

2012-09-24-uicollectionview.md

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ title: UICollectionView
33
author: Mattt Thompson
44
category: Cocoa
55
excerpt: "UICollectionView single-handedly changes the way we will design and develop iOS apps from here on out. This is not to say that collection views are in any way unknown or obscure. But being an NSHipster isn't just about knowing obscure gems in the rough. Sometimes, it's about knowing about up-and-comers before they become popular and sell out."
6+
status:
7+
swift: 1.1
68
---
79

810
`UICollectionView` is the new `UITableView`. It's that important.

2012-10-01-pragma.md

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ author: Mattt Thompson
44
category: Objective-C
55
tags: nshipster
66
excerpt: "`#pragma` declarations are a mark of craftsmanship in Objective-C. Although originally purposed for compiling source code across many different compilers, the modern Xcode-savvy programmer uses #pragma declarations to very different ends."
7+
status:
8+
swift: n/a
79
---
810

911
`#pragma` declarations are a mark of craftsmanship in Objective-C. Although originally used to make source code compatible between different compilers, the Xcode-savvy coder uses `#pragma` declarations to very different ends.

2012-10-08-at-compiler-directives.md

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ author: Mattt Thompson
44
category: Objective-C
55
tags: nshipster
66
excerpt: "If we were to go code-watching for Objective-C, what would we look for? Square brackets, ridiculously-long method names, and `@`'s. \"at\" sign compiler directives are as central to understanding Objective-C's gestalt as its ancestry and underlying mechanisms. It's the sugary glue that allows Objective-C to be such a powerful, expressive language, and yet still compile all the way down to C."
7+
status:
8+
swift: n/a
79
---
810

911
Birdwatchers refer to it as (and I swear I'm not making this up) ["Jizz"](http://en.wikipedia.org/wiki/Jizz_%28birding%29): those indefinable characteristics unique to a particular kind of thing.

2012-10-15-addressbookui.md

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ title: AddressBookUI
33
author: Mattt Thompson
44
category: Cocoa
55
excerpt: "Address Book UI is an iOS framework for displaying, selecting, editing, and creating contacts in a user's Address Book. Similar to the Message UI framework, Address Book UI contains a number of controllers that can be presented modally, to provide common system functionality in a uniform interface."
6+
status:
7+
swift: 1.1
68
---
79

810
[Address Book UI](https://developer.apple.com/LIBRARY/ios/documentation/AddressBookUI/Reference/AddressBookUI_Framework/index.html) is an iOS framework for displaying, selecting, editing, and creating contacts in a user's Address Book. Similar to the [Message UI](https://developer.apple.com/library/IOs/documentation/MessageUI/Reference/MessageUI_Framework_Reference/index.html) framework, Address Book UI contains a number of controllers that can be presented modally, to provide common system functionality in a uniform interface.

2012-10-22-nslinguistictagger.md

+19-3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ author: Mattt Thompson
44
category: Cocoa
55
tags: nshipster
66
excerpt: "NSLinguisticTagger is a veritable Swiss Army Knife of linguistic functionality, with the ability to tokenize natural language strings into words, determine their part-of-speech & stem, extract names of people, places, & organizations, and tell you the languages & respective writing system used in the string."
7+
status:
8+
swift: 2.0
9+
reviewed: September 8, 2015
710
---
811

912
`NSLinguisticTagger` is a veritable Swiss Army Knife of linguistic functionality, with the ability to [tokenize](http://en.wikipedia.org/wiki/Tokenization) natural language strings into words, determine their part-of-speech & [stem](http://en.wikipedia.org/wiki/Word_stem), extract names of people, places, & organizations, and tell you the languages & respective [writing system](http://en.wikipedia.org/wiki/Writing_system) used in the string.
@@ -22,16 +25,15 @@ Computers are a long ways off from "understanding" this question literally, but
2225

2326
~~~{swift}
2427
let question = "What is the weather in San Francisco?"
25-
let options: NSLinguisticTaggerOptions = .OmitWhitespace | .OmitPunctuation | .JoinNames
28+
let options: NSLinguisticTaggerOptions = [.OmitWhitespace, .OmitPunctuation, .JoinNames]
2629
let schemes = NSLinguisticTagger.availableTagSchemesForLanguage("en")
2730
let tagger = NSLinguisticTagger(tagSchemes: schemes, options: Int(options.rawValue))
2831
tagger.string = question
29-
tagger.enumerateTagsInRange(NSMakeRange(0, (question as NSString).length), scheme: NSLinguisticTagSchemeNameTypeOrLexicalClass, options: options) { (tag, tokenRange, sentenceRange, _) in
32+
tagger.enumerateTagsInRange(NSMakeRange(0, (question as NSString).length), scheme: NSLinguisticTagSchemeNameTypeOrLexicalClass, options: options) { (tag, tokenRange, _, _) in
3033
let token = (question as NSString).substringWithRange(tokenRange)
3134
println("\(token): \(tag)")
3235
}
3336
~~~
34-
3537
~~~{objective-c}
3638
NSString *question = @"What is the weather in San Francisco?";
3739
NSLinguisticTaggerOptions options = NSLinguisticTaggerOmitWhitespace | NSLinguisticTaggerOmitPunctuation | NSLinguisticTaggerJoinNames;
@@ -153,6 +155,20 @@ By default, each token in a name is treated as separate instances. In many circu
153155

154156
---
155157

158+
Finally, NSString provides convenience methods that handle the setup and configuration of NSLinguisticTagger on your behalf. For one-off tokenizing, you can save a lot of boilerplate:
159+
160+
```swift
161+
var tokenRanges: NSArray?
162+
let tags = "Where in the world is Carmen San Diego?".linguisticTagsInRange(
163+
NSMakeRange(0, (question as NSString).length),
164+
scheme: NSLinguisticTagSchemeNameTypeOrLexicalClass,
165+
options: options, orthography: nil, tokenRanges: &tokenRanges
166+
)
167+
// tags: ["Pronoun", "Preposition", "Determiner", "Noun", "Verb", "PersonalName"]
168+
```
169+
170+
---
171+
156172
Natural language is woefully under-utilized in user interface design on mobile devices. When implemented effectively, a single utterance from the user can achieve the equivalent of a handful of touch interactions, in a fraction of the time.
157173

158174
Sure, it's not easy, but if we spent a fraction of the time we use to make our visual interfaces pixel-perfect, we could completely re-imagine how users best interact with apps and devices. And with `NSLinguisticTagger`, it's never been easier to get started.

2012-10-29-uilocalizedindexedcollation.md

+12-11
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ author: Mattt Thompson
44
category: Cocoa
55
tags: nshipster
66
excerpt: "UITableView starts to become unwieldy once it gets to a few hundred rows. If users are reduced to frantically scratching at the screen like a cat playing Fruit Ninja in order to get at what they want... you may want to rethink your UI approach."
7+
status:
8+
swift: 2.0
9+
reviewed: September 8, 2015
710
---
811

912
UITableView starts to become unwieldy once it gets to a few hundred rows. If users are reduced to frantically scratching at the screen like a [cat playing Fruit Ninja](http://www.youtube.com/watch?v=CdEBgZ5Y46U) in order to get at what they want... you may want to rethink your UI approach.
@@ -60,16 +63,14 @@ All told, here's what a typical table view data source implementation looks like
6063

6164
~~~{swift}
6265
class ObjectTableViewController: UITableViewController {
63-
let collation = UILocalizedIndexedCollation.currentCollation() as UILocalizedIndexedCollation
64-
var sections: [[Object]] = []
65-
var objects: [Object] {
66+
let collation = UILocalizedIndexedCollation.currentCollation()
67+
var sections: [[AnyObject]] = []
68+
var objects: [AnyObject] = [] {
6669
didSet {
6770
let selector: Selector = "localizedTitle"
71+
sections = Array(count: collation.sectionTitles.count, repeatedValue: [])
6872
69-
70-
sections = [[Object]](count: collation.sectionTitles.count, repeatedValue: [])
71-
72-
let sortedObjects = collation.sortedArrayFromArray(objects, collationStringSelector: selector) as [Object]
73+
let sortedObjects = collation.sortedArrayFromArray(objects, collationStringSelector: selector)
7374
for object in sortedObjects {
7475
let sectionNumber = collation.sectionForObject(object, collationStringSelector: selector)
7576
sections[sectionNumber].append(object)
@@ -81,15 +82,15 @@ class ObjectTableViewController: UITableViewController {
8182
8283
// MARK: UITableViewDelegate
8384
84-
override func tableView(tableView: UITableView!, titleForHeaderInSection section: Int) -> String! {
85-
return collation.sectionTitles![section] as String
85+
override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String {
86+
return collation.sectionTitles[section]
8687
}
8788
88-
override func sectionIndexTitlesForTableView(tableView: UITableView!) -> [AnyObject]! {
89+
override func sectionIndexTitlesForTableView(tableView: UITableView) -> [String] {
8990
return collation.sectionIndexTitles
9091
}
9192
92-
override func tableView(tableView: UITableView!, sectionForSectionIndexTitle title: String!, atIndex index: Int) -> Int {
93+
override func tableView(tableView: UITableView, sectionForSectionIndexTitle title: String, atIndex index: Int) -> Int {
9394
return collation.sectionForSectionIndexTitleAtIndex(index)
9495
}
9596
}

2012-11-05-nsurlprotocol.md

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ title: NSURLProtocol
33
author: Mattt Thompson
44
category: Cocoa
55
excerpt: "Foundation’s URL Loading System is something that every iOS developer would do well to buddy up with. And of all of networking classes and protocols of Foundation, NSURLProtocol is perhaps the most obscure and powerful."
6+
status:
7+
swift: n/a
68
---
79

810
iOS is all about networking--whether it's reading or writing state to and from the server, offloading computation to a distributed system, or loading remote images, audio, and video from the cloud.

0 commit comments

Comments
 (0)