Skip to content

Commit

Permalink
Merge remote-tracking branch 'NSHipster/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
mickmaccallum committed Sep 15, 2015
2 parents be51787 + 9fb42ea commit 008bd47
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 87 deletions.
19 changes: 14 additions & 5 deletions 2013-08-19-nshashtable-and-nsmaptable.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,8 @@ hashTable.addObject("foo")
hashTable.addObject("bar")
hashTable.addObject(42)
hashTable.removeObject("bar")
print(hashTable.allObjects)
print("Members: \(hashTable.allObjects)")
~~~

~~~{objective-c}
NSHashTable *hashTable = [NSHashTable hashTableWithOptions:NSPointerFunctionsCopyIn];
[hashTable addObject:@"foo"];
Expand Down Expand Up @@ -65,17 +64,19 @@ NSLog(@"Members: %@", [hashTable allObjects]);
- `NSMapTable` can `copy` its values on input.
- `NSMapTable` can contain arbitrary pointers, and use pointer identity for equality and hashing checks.

> *Note:* `NSMapTable`'s focus on strong and weak references means that Swift's prevalent value types are a no go—reference types only, please.
### Usage

Instances where one might use `NSMapTable` include non-copyable keys and storing weak references to keyed delegates or another kind of weak object.

~~~{swift}
let delegate: AnyObject = ...
let mapTable = NSMapTable(keyOptions: .StrongMemory, valueOptions: .WeakMemory)
mapTable.setObject(delegate, forKey: "foo")
print("Keys: \(mapTable.keyEnumerator().allObjects)")
~~~

~~~{objective-c}
id delegate = ...;
NSMapTable *mapTable = [NSMapTable mapTableWithKeyOptions:NSMapTableStrongMemory
Expand Down Expand Up @@ -105,7 +106,11 @@ extension NSMapTable {
}
set {
setObject(newValue, forKey: key)
if newValue != nil {
setObject(newValue, forKey: key)
} else {
removeObjectForKey(key)
}
}
}
}
Expand All @@ -121,7 +126,11 @@ extension NSMapTable {
- (void)setObject:(id)obj forKeyedSubscript:(id)key
{
[self setObject:obj forKey:key];
if (obj != nil) {
[self setObject:obj forKey:key];
} else {
[self removeObjectForKey:key];
}
}
@end
Expand Down
42 changes: 21 additions & 21 deletions 2013-09-23-ios7.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ author: Mattt Thompson
category: ""
excerpt: "With the NDA finally lifted, we can finally talk about all of the amazing new APIs in iOS 7."
status:
swift: 1.1
reviewed: May 20, 2015
swift: 2.0
reviewed: Sep 12, 2015
---

With the NDA finally lifted, we can finally talk about all of the amazing new APIs in iOS 7. And there are a _lot_ of them. "1500 new APIs", by Apple's count during the WWDC Keynote. (Granted, a good portion of that could just be all of the changes from `id` to `instancetype`, but that's a huge number, regardless).
Expand All @@ -25,9 +25,9 @@ But no longer! iOS 7 finally bakes-in Base64:
~~~{swift}
let string = "Lorem ipsum dolor sit amet."
if let data = string.dataUsingEncoding(NSUTF8StringEncoding) {
let base64EncodedString = data.base64EncodedStringWithOptions(NSDataBase64EncodingOptions.allZeros)
println(base64EncodedString) // TG9yZW0gaXBzdW0gZG9sYXIgc2l0IGFtZXQu
let base64EncodedString = data.base64EncodedStringWithOptions([])
print(base64EncodedString) // TG9yZW0gaXBzdW0gZG9sYXIgc2l0IGFtZXQu
}
~~~

Expand All @@ -49,8 +49,8 @@ if let components = NSURLComponents(string: "http://nshipster.com") {
components.path = "/iOS7"
components.query = "foo=bar"
println(components.scheme!) // http
println(components.URL!) // http://nshipster.com/iOS7?foo=bar
print(components.scheme!) // http
print(components.URL!) // http://nshipster.com/iOS7?foo=bar
}
~~~

Expand Down Expand Up @@ -86,7 +86,7 @@ Anything with a notion of completed and total units is a candidate for `NSProgre
let progress = NSProgress(totalUnitCount: 100)
progress.completedUnitCount = 42;
println(progress.localizedDescription) // 42% completed
print(progress.localizedDescription) // 42% completed
~~~

~~~{objective-c}
Expand Down Expand Up @@ -133,8 +133,8 @@ Behold!
~~~{swift}
let array = [1, 2, 3] as NSArray
println("First Object: \(array.firstObject)") // First Object: Optional(1)
println("Last Object: \(array.lastObject)") // Last Object: Optional(3)
print("First Object: \(array.firstObject)") // First Object: Optional(1)
print("Last Object: \(array.lastObject)") // Last Object: Optional(3)
~~~

~~~{objective-c}
Expand All @@ -161,8 +161,9 @@ let smileDetector = CIDetector(ofType: CIDetectorTypeFace, context: context,
options: [CIDetectorTracking: true, CIDetectorAccuracy: CIDetectorAccuracyLow])
var features = smileDetector.featuresInImage(ciImage, options: [CIDetectorSmile: true])
if let feature = features.first as? CIFaceFeature where feature.hasSmile {
UIImageWriteToSavedPhotosAlbum(UIImage(CIImage: ciImage)!, self, "didFinishWritingImage", &features)
UIImageWriteToSavedPhotosAlbum(UIImage(CIImage: ciImage), self, "didFinishWritingImage", &features)
} else {
label.text = "Say Cheese!"
}
Expand Down Expand Up @@ -194,11 +195,11 @@ let session = AVCaptureSession()
let device = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo)
var error: NSError?
let input = AVCaptureDeviceInput(device: device, error: &error)
if let error = error {
println("Error: \(error)")
} else {
do {
let input = try AVCaptureDeviceInput(device: device)
session.addInput(input)
} catch let error {
print("Error: \(error)")
}
let output = AVCaptureMetadataOutput()
Expand All @@ -214,7 +215,7 @@ func captureOutput(
captureOutput: AVCaptureOutput!,
didOutputMetadataObjects metadataObjects: [AnyObject]!,
fromConnection connection: AVCaptureConnection!) {
var QRCode: String?
for metadata in metadataObjects as! [AVMetadataObject] {
if metadata.type == AVMetadataObjectTypeQRCode {
Expand All @@ -223,7 +224,7 @@ func captureOutput(
}
}
println("QRCode: \(QRCode)")
print("QRCode: \(QRCode)")
}
~~~

Expand Down Expand Up @@ -290,9 +291,8 @@ Even though the number of people who have actually read something saved for late
~~~{swift}
import SafariServices
let url = NSURL(string: "http://nshipster.com/ios7")
SSReadingList.defaultReadingList().addReadingListItemWithURL(url, title: "NSHipster",
previewText: "...", error: nil)
let url = NSURL(string: "http://nshipster.com/ios7")!
try? SSReadingList.defaultReadingList()?.addReadingListItemWithURL(url, title: "NSHipster", previewText: "...")
~~~

~~~{objective-c}
Expand Down Expand Up @@ -345,7 +345,7 @@ let distance = portland.distanceFromLocation(sanFrancisco)
let formatter = MKDistanceFormatter()
formatter.units = .Imperial
println(formatter.stringFromDistance(distance)) // 535 miles
print(formatter.stringFromDistance(distance)) // 535 miles
~~~

~~~{objective-c}
Expand Down
1 change: 1 addition & 0 deletions 2013-12-02-nsnotification-and-nsnotificationcenter.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ Something that often slips up developers is how similar the method signatures fo
func addObserver(observer: NSObject, forKeyPath keyPath: String,
options: NSKeyValueObservingOptions,
context: UnsafeMutablePointer<Void>)
~~~
~~~{objective-c}
- (void)addObserver:(NSObject *)observer
forKeyPath:(NSString *)keyPath
Expand Down
Loading

0 comments on commit 008bd47

Please sign in to comment.