Skip to content

Commit

Permalink
Merge pull request NSHipster#334 from 0x7fffffff/master
Browse files Browse the repository at this point in the history
Updates for NSOrderedSet, NSOperation, PHImageManager, NSRange, NSDateComponents, NSPredicate and Swift & the Objective-C Runtime
  • Loading branch information
natecook1000 committed Sep 25, 2015
2 parents 71a18f4 + 6809937 commit ae4cd07
Show file tree
Hide file tree
Showing 7 changed files with 379 additions and 112 deletions.
43 changes: 22 additions & 21 deletions 2012-07-31-nsdatecomponents.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ author: Mattt Thompson
category: Cocoa
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."
status:
swift: 1.1
swift: 2.0
reviewed: September 19, 2015
---

`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.
Expand All @@ -18,8 +19,8 @@ Whereas dates represent a particular moment in time, date components depend on w
~~~{swift}
let calendar = NSCalendar.currentCalendar()
let date = NSDate()
let components = calendar.components(.CalendarUnitMonth | .CalendarUnitDay, fromDate: date)
~~~
let components = calendar.components([.Month, .Day], fromDate: date)
~~~

~~~{objective-c}
NSCalendar *calendar = [NSCalendar currentCalendar];
Expand All @@ -30,21 +31,21 @@ NSDate *date = [NSDate date];
The `components` parameter is a [bitmask](http://en.wikipedia.org/wiki/Bitmask) of the date component values to retrieve, with many to choose from:

~~~{swift}
.CalendarUnitEra
.CalendarUnitYear
.CalendarUnitMonth
.CalendarUnitDay
.CalendarUnitHour
.CalendarUnitMinute
.CalendarUnitSecond
.CalendarUnitWeekday
.CalendarUnitWeekdayOrdinal
.CalendarUnitQuarter
.CalendarUnitWeekOfMonth
.CalendarUnitWeekOfYear
.CalendarUnitYearForWeekOfYear
.CalendarUnitCalendar
.CalendarUnitTimeZone
NSCalendarUnit.Era
NSCalendarUnit.Year
NSCalendarUnit.Month
NSCalendarUnit.Day
NSCalendarUnit.Hour
NSCalendarUnit.Minute
NSCalendarUnit.Second
NSCalendarUnit.Weekday
NSCalendarUnit.WeekdayOrdinal
NSCalendarUnit.Quarter
NSCalendarUnit.WeekOfMonth
NSCalendarUnit.WeekOfYear
NSCalendarUnit.YearForWeekOfYear
NSCalendarUnit.Calendar
NSCalendarUnit.TimeZone
~~~

~~~{objective-c}
Expand Down Expand Up @@ -79,7 +80,7 @@ let components = NSDateComponents()
components.weekOfYear = 1
components.hour = 12
println("1 week and 12 hours from now: \(calendar.dateByAddingComponents(components, toDate: date, options: nil))")
print("1 week and 12 hours from now: \(calendar.dateByAddingComponents(components, toDate: date, options: []))")
~~~

~~~{objective-c}
Expand All @@ -98,7 +99,7 @@ NSLog(@"1 week and twelve hours from now: %@", [calendar dateByAddingComponents:
Perhaps the most powerful feature of `NSDateComponents`, however, is the ability to go the opposite direction—creating an `NSDate` object from components. `NSCalendar -dateFromComponents:` is the method used for this purpose:

~~~{swift}
let calendar = NSCalendar(identifier: NSGregorianCalendar)
let calendar = NSCalendar(identifier: NSCalendarIdentifierGregorian)
let components = NSDateComponents()
components.year = 1987
Expand All @@ -108,7 +109,7 @@ components.hour = 14
components.minute = 20
components.second = 0
let date = calendar.dateFromComponents(components)
let date = calendar?.dateFromComponents(components)
~~~

~~~{objective-c}
Expand Down
15 changes: 8 additions & 7 deletions 2012-11-26-nsorderedset.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ category: Cocoa
tags: nshipster
excerpt: "Why isn't NSOrderedSet a subclass of NSSet? The answer may surprise you."
status:
swift: 1.1
swift: 2.0
reviewed: September 15, 2015
---

Here's a question: why isn't `NSOrderedSet` a subclass of `NSSet`?
Expand All @@ -27,8 +28,8 @@ As expertly demonstrated by [Tom Dalling](http://tomdalling.com) in [this Stack
To start, let's look at how `-mutableCopy` is supposed to work in a class cluster:

~~~{swift}
let immutable: NSSet = NSSet()
var mutable: NSMutableSet = immutable.mutableCopy() as NSMutableSet
let immutable = NSSet()
let mutable = immutable.mutableCopy() as! NSMutableSet
mutable.isKindOfClass(NSSet.self) // true
mutable.isKindOfClass(NSMutableSet.self) // true
Expand All @@ -47,8 +48,8 @@ Now let's suppose that `NSOrderedSet` was indeed a subclass of `NSSet`:
~~~{swift}
// class NSOrderedSet: NSSet {...}
let immutable: NSOrderedSet = NSOrderedSet()
var mutable: NSMutableOrderedSet = immutable.mutableCopy() as NSMutableOrderedSet
let immutable = NSOrderedSet()
let mutable = immutable.mutableCopy() as! NSMutableOrderedSet
mutable.isKindOfClass(NSSet.self) // true
mutable.isKindOfClass(NSMutableSet.self) // false (!)
Expand All @@ -72,8 +73,8 @@ That's no good... since `NSMutableOrderedSet` couldn't be used as a method param
// class NSOrderedSet: NSSet {...}
// class NSMutableOrderedSet: NSMutableSet {...}
let immutable: NSOrderedSet = NSOrderedSet()
var mutable: NSMutableOrderedSet = immutable.mutableCopy() as NSMutableOrderedSet
let immutable = NSOrderedSet()
let mutable = immutable.mutableCopy() as! NSMutableOrderedSet
mutable.isKindOfClass(NSSet.self) // true
mutable.isKindOfClass(NSMutableSet.self) // true
Expand Down
7 changes: 4 additions & 3 deletions 2013-07-15-nspredicate.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ category: Cocoa
tags: nshipster, popular
excerpt: "NSPredicate is a Foundation class that specifies how data should be fetched or filtered. Its query language, which is like a cross between a SQL WHERE clause and a regular expression, provides an expressive, natural language interface to define logical conditions on which a collection is searched."
status:
swift: 1.1
swift: 2.0
reviewed: September 19, 2015
---

`NSPredicate` is a Foundation class that specifies how data should be fetched or filtered. Its query language, which is like a cross between a SQL `WHERE` clause and a regular expression, provides an expressive, natural language interface to define logical conditions on which a collection is searched.
Expand Down Expand Up @@ -244,7 +245,7 @@ Analyzing its class constructor provides a glimpse into the way `NSPredicate` fo
### `NSComparisonPredicate` Types

~~~{swift}
enum NSPredicateOperatorType : UInt {
public enum NSPredicateOperatorType : UInt {
case LessThanPredicateOperatorType
case LessThanOrEqualToPredicateOperatorType
case GreaterThanPredicateOperatorType
Expand Down Expand Up @@ -296,7 +297,7 @@ Finally, if you just can't be bothered to learn the `NSPredicate` format syntax,

~~~{swift}
let shortNamePredicate = NSPredicate { (evaluatedObject, _) in
return (evaluatedObject as Person).firstName.utf16Count <= 5
return (evaluatedObject as! Person).firstName.utf16.count <= 5
}
(people as NSArray).filteredArrayUsingPredicate(shortNamePredicate)
Expand Down
Loading

0 comments on commit ae4cd07

Please sign in to comment.