Skip to content

Instantly share code, notes, and snippets.

@rae
Last active August 29, 2015 14:14
Show Gist options
  • Save rae/523c26738ba92eea524e to your computer and use it in GitHub Desktop.
Save rae/523c26738ba92eea524e to your computer and use it in GitHub Desktop.
Reid's Swift Notes

Reid's Notes on Swift

Hey Cool!

  • you can directly manipulate CGRects. No more assigning to a "Frame" variable and assigning back to the view.frame: self.frame.size.height += 44 // woo!

Things to get used to

  • Implicit typing works. Learn to leave out the type of a variable as it can usually be inferred from the return type of the assignment: let foo = self.method()
  • don't forget the "override" prefix when you override a method -- you'll get a compiler error, but I'm mentioning it anyway :-)
  • all Swift code knows about all other Swift code; it's like everyone #imports everyone else
  • enums are much more powerful than in any other language; I'm still getting used to them. e.g.:
enum KnownVertical : String {
	case Movie		= "movies"
	case Restaurant	= "restaurants"
	case Place		= "bars & clubs"
	case Location	= "people"
	case Person		= "person"
	case Gas		= "gas"
	case Events		= "events"
	case YouTube	= "youtube"
	case Parking	= "parking"
}
  • properties have post- and pre-setting "methods", which can be handy
var foo: String {
	willSet { ... }
	set { ... }
	didSet { ... }
}

Huh, that's not obvious

  • To use Swift classes in Objective-C, you have to #import a generated header file that is NOT very well documented. It's called "<target>-Swift.h"; e.g. "Poynt-Swift.h"
  • You don't need the @objc prefix on classes that inherit from Objective-C classes.

Weird!

  • dealloc() is "unavailable" use a special deinit instead. It's not a func or anything. Just plain deinit { .. }

  • You can't use the recursiveDescription debugging method on UIView unless you add this to the bridging header (no implementation necessary)

@interface UIView (Debug)
- (id)recursiveDescription;
- (id)_autolayoutTrace;
@end

Not sure what _autolayoutTrace does, but someone said it was sweet.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment