You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: 2012-07-07-nsindexset.md
+22-2
Original file line number
Diff line number
Diff line change
@@ -3,15 +3,35 @@ title: NSIndexSet
3
3
author: Mattt Thompson
4
4
category: Cocoa
5
5
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
7
10
---
8
11
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.
10
13
11
14
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.
12
15
13
16
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:`.
14
17
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",
Here are a few more ideas to get you thinking in terms of index sets:
16
36
17
37
- 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`.
Copy file name to clipboardExpand all lines: 2012-07-14-nscache.md
+22
Original file line number
Diff line number
Diff line change
@@ -4,6 +4,9 @@ author: Mattt Thompson
4
4
category: Cocoa
5
5
tags: nshipster
6
6
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
7
10
---
8
11
9
12
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
37
40
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.
38
41
39
42
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:
Copy file name to clipboardExpand all lines: 2012-07-24-nssortdescriptor.md
+2
Original file line number
Diff line number
Diff line change
@@ -4,6 +4,8 @@ author: Mattt Thompson
4
4
category: Cocoa
5
5
tags: nshipster
6
6
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
7
9
---
8
10
9
11
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?
Copy file name to clipboardExpand all lines: 2012-07-31-nsdatecomponents.md
+2
Original file line number
Diff line number
Diff line change
@@ -3,6 +3,8 @@ title: NSDateComponents
3
3
author: Mattt Thompson
4
4
category: Cocoa
5
5
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
6
8
---
7
9
8
10
`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.
Copy file name to clipboardExpand all lines: 2012-08-06-cfstringtransform.md
+2
Original file line number
Diff line number
Diff line change
@@ -4,6 +4,8 @@ author: Mattt Thompson
4
4
category: Cocoa
5
5
tags: nshipster, popular
6
6
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
7
9
---
8
10
9
11
There are two indicators that tell you everything you need to know about how nice a language is to use:
Copy file name to clipboardExpand all lines: 2012-08-13-nsincrementalstore.md
+3
Original file line number
Diff line number
Diff line change
@@ -3,6 +3,9 @@ title: NSIncrementalStore
3
3
author: Mattt Thompson
4
4
category: Cocoa
5
5
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
6
9
---
7
10
8
11
Even for a blog dedicated to obscure APIs, `NSIncrementalStore` brings a new meaning to the word "obscure".
Copy file name to clipboardExpand all lines: 2012-08-27-cfbag.md
+2
Original file line number
Diff line number
Diff line change
@@ -4,6 +4,8 @@ author: Mattt Thompson
4
4
category: Cocoa
5
5
tags: nshipster
6
6
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
7
9
---
8
10
9
11
Objective-C is a language caught between two worlds.
Copy file name to clipboardExpand all lines: 2012-09-10-uiaccessibility.md
+2
Original file line number
Diff line number
Diff line change
@@ -4,6 +4,8 @@ author: Mattt Thompson
4
4
category: Cocoa
5
5
tags: nshipster
6
6
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
7
9
---
8
10
9
11
> We all want to help one another, human beings are like that.
Copy file name to clipboardExpand all lines: 2012-09-17-nscharacterset.md
+2
Original file line number
Diff line number
Diff line change
@@ -3,6 +3,8 @@ title: NSCharacterSet
3
3
author: Mattt Thompson
4
4
category: Cocoa
5
5
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
6
8
---
7
9
8
10
As mentioned [previously](http://nshipster.com/cfstringtransform/), Foundation boasts one of the best, most complete implementations of strings around.
Copy file name to clipboardExpand all lines: 2012-09-24-uicollectionview.md
+2
Original file line number
Diff line number
Diff line change
@@ -3,6 +3,8 @@ title: UICollectionView
3
3
author: Mattt Thompson
4
4
category: Cocoa
5
5
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
6
8
---
7
9
8
10
`UICollectionView` is the new `UITableView`. It's that important.
Copy file name to clipboardExpand all lines: 2012-10-01-pragma.md
+2
Original file line number
Diff line number
Diff line change
@@ -4,6 +4,8 @@ author: Mattt Thompson
4
4
category: Objective-C
5
5
tags: nshipster
6
6
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
7
9
---
8
10
9
11
`#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.
Copy file name to clipboardExpand all lines: 2012-10-08-at-compiler-directives.md
+2
Original file line number
Diff line number
Diff line change
@@ -4,6 +4,8 @@ author: Mattt Thompson
4
4
category: Objective-C
5
5
tags: nshipster
6
6
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
7
9
---
8
10
9
11
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.
Copy file name to clipboardExpand all lines: 2012-10-15-addressbookui.md
+2
Original file line number
Diff line number
Diff line change
@@ -3,6 +3,8 @@ title: AddressBookUI
3
3
author: Mattt Thompson
4
4
category: Cocoa
5
5
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
6
8
---
7
9
8
10
[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.
Copy file name to clipboardExpand all lines: 2012-10-22-nslinguistictagger.md
+19-3
Original file line number
Diff line number
Diff line change
@@ -4,6 +4,9 @@ author: Mattt Thompson
4
4
category: Cocoa
5
5
tags: nshipster
6
6
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
7
10
---
8
11
9
12
`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
22
25
23
26
~~~{swift}
24
27
let question = "What is the weather in San Francisco?"
25
-
let options: NSLinguisticTaggerOptions = .OmitWhitespace | .OmitPunctuation | .JoinNames
28
+
let options: NSLinguisticTaggerOptions = [.OmitWhitespace, .OmitPunctuation, .JoinNames]
26
29
let schemes = NSLinguisticTagger.availableTagSchemesForLanguage("en")
27
30
let tagger = NSLinguisticTagger(tagSchemes: schemes, options: Int(options.rawValue))
28
31
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
30
33
let token = (question as NSString).substringWithRange(tokenRange)
31
34
println("\(token): \(tag)")
32
35
}
33
36
~~~
34
-
35
37
~~~{objective-c}
36
38
NSString *question = @"What is the weather in San Francisco?";
@@ -153,6 +155,20 @@ By default, each token in a name is treated as separate instances. In many circu
153
155
154
156
---
155
157
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(
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.
157
173
158
174
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.
Copy file name to clipboardExpand all lines: 2012-10-29-uilocalizedindexedcollation.md
+12-11
Original file line number
Diff line number
Diff line change
@@ -4,6 +4,9 @@ author: Mattt Thompson
4
4
category: Cocoa
5
5
tags: nshipster
6
6
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
7
10
---
8
11
9
12
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
60
63
61
64
~~~{swift}
62
65
class ObjectTableViewController: UITableViewController {
63
-
let collation = UILocalizedIndexedCollation.currentCollation() as UILocalizedIndexedCollation
64
-
var sections: [[Object]] = []
65
-
var objects: [Object] {
66
+
let collation = UILocalizedIndexedCollation.currentCollation()
Copy file name to clipboardExpand all lines: 2012-11-05-nsurlprotocol.md
+2
Original file line number
Diff line number
Diff line change
@@ -3,6 +3,8 @@ title: NSURLProtocol
3
3
author: Mattt Thompson
4
4
category: Cocoa
5
5
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
6
8
---
7
9
8
10
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