“A superset of the C programming language and provides object- oriented capabilities and a dynamic runtime” • Inherits C syntax, primitives, flow control • Adds syntax for classes, methods, object literals • Dynamically typed, defers a lot to runtime
// Import individual classes, or in this case, a whole framework #import ! // Prefixed class name (no namespaces ) : superclass @interface BRYCalculator : NSObject ! // Instance method with two arguments - (NSInteger)add:(NSInteger)number1 toNumber:(NSInteger)number2; ! // Class method ("static" to you Java folk) + (void)initialize; ! @end
You don’t “invoke methods,” you “send messages.” ! It’s kind of the same thing except any message can be sent to any object. More on this later. Message sending
Properties are used to encapsulate your data. Accessor methods are created for you by the compiler. ! Should pretty much always use them instead of direct instance variables. Properties
Thankfully we no longer need to manually allocate and release memory (unless using C APIs) ! Still need to be careful to not create retain cycles though Memory management
Base class for almost all views ! • Has a “frame” (coordinates and size), determines where it shows up on screen • Frame can be set manually or via constraints (AutoLayout) • Has an array of “subviews” (forming a hierarchy) • Can be configured in code or in Interface Builder UIView
Generally one controller visible on screen at a time ! • Provides hooks like “viewWillAppear”, “viewWillDisappear”, rotation • View controllers can have children • Can implement “container view controllers” (e.g. tab bar, navigation bar) UIViewController
// `self` will be notified when the scroll view’s content offset changes [scrollView addObserver:self forKeyPath:@“contentOffset”]; ! // `[self wipeDatabase:]` will be called when notification is posted [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(wipeDatabase:) name:@"UserDidLogoutNotification"]; Observing changes
NSOperationQueue *queue = [[NSOperationQueue alloc] init]; queue.maxConcurrentOperationCount = 2; ! [queue addOperationWithBlock:^{ // Do something on a background thread [[NSOperationQueue mainQueue] addOperationWithBlock:^{ // Update the UI on the main thread }]; }]; Concurrency