Objective-C Programming by Aaron Hillegass

Objective-C Programming

6.9/1010 min read

10/22/19

  • Main is the function that is called when a program first starts

  • Commonly used C types: short, int, long, float, double, char, pointers, struct

  • A pointer holds a memory address. It is declared using the asterisk character. int * can hold a memory address where an int is stored

  • A struct is a type made up of other types

  • In C, 0 represents false, and anything that is not 0 is considered true

  • Boolean type is BOOL

  • When a function is called, its frame is created on top of the stack

  • Global variables are available in any file

  • Static variables are like global variables in that they are declared outside of a function but they are only accessible from the code in the same file they are declared

  • static float lastTemperature;

  • Cast operator: (float)3 => 3.0

  • printf: %s for strings, %d for ints, %f for floats, %p for addresses/pointers

  • %.2f for 2 decimal points

  • Use the & operator to get a variable's address

  • We pass address because they are much smaller than the underlying data

  • Use the *operator to get the data stored at an address/pointer

  • Pass-by-reference: you supply a function an address, and the function puts the data there

  • Pass NULL as an address to tell the function you don't need a particular value

  • You will usually use a structure declaration over and over so it is common to create a typedef for the structure type

  • Typedef struct Person Person;

  • Buffer = a long line of bytes of memory

  • Claim a buffer of memory using the function malloc()

  • When you are done using the buffer from the heap, you call the function free() to release your claim on that memory

  • -> means dereference the pointer to the structure and get me the member

  • All Objective-C programming is done with the Foundation framework

  • A framework is a library of classes that you use to write programs

  • An object is like a structure in that it holds data, but it also contains a set of functions that act upon that data

  • You trigger functions by sending a message to the object

  • A function that is triggered by a message is known as a method

  • A class describes a particular type of object

  • Descriptions include methods and instance variables where an object of this type stores its data

  • You ask a class to create an object of its type for you on the heap

  • Objects from a class are an instance of that class

  • NSLog is like printf but its format string always begins with an @ and it does not require a at the end

  • NSLog prefaces its output with the date, time, program name, and process ID

  • %p prints out the location of the object

  • %@ asks the object to describe itself as a string

  • A message send is always surrounded by square brackets, and it always has at least two parts: a pointer to the object that is receiving the message and the name of the method to be triggered

  • The pointer to the object that is receiving the message is known as the receiver

  • The method name is known as the selector

  • You pass an argument to a method after the : after the message name

  • Type id means a pointer to some kind of Objective-C object

  • id delegate;

  • id implies the asterisk

  • alloc returns a pointer to a new object that needs to be initialized

  • init returns a pointer to the new object (which is nearly always the pointer that came out of the alloc method), so we can use what's returned from init for the assignment

  • nil is the zero pointer, or the pointer to no object

  • You can send a method to nil

  • If you are sending messages and nothing is happening, make sure you aren't sending messages to a pointer that has been set to nil

  • If you send a message to nil, the return value doesn't mean anything

  • Literal instance of NSString: @"Why me!?";

  • You can use the length method to get the number of characters in a string

  • Check if two strings are equal using the isEqual: method

  • NSArray holds a list of pointers to other objects

  • You will use methods count and objectAtIndex all the time

  • Ordered and zero-based

  • Classes define methods and instance variables

  • The header or interface file, will contain the declarations of instance variables and methods

  • The implementation file implements each method

  • You declare instance variables inside of curly braces and you declare methods after the variables outside of the curly braces

  • Setter methods allows other methods to set the value of an instance method

  • Getter methods allow other methods to read the value of an instance variable

  • Setter and getter methods are also known as accessor methods, or just accessors

  • The name of the method for reading an instance variable is simply the same of that instance variable

  • Dot notation is actually sending messages

  • x.fido is the same as [x fido]

  • Properties are a simple way of writing accessor methods

  • Synthesize synthesizes default accessor methods based on each @property declaration

  • Inside any method, you have access to the implicit local variable self, which is a pointer to the object that is running the method

  • self is literally its address so it can be passed to other objects and methods

  • A subclass of another class gets all instance variables and methods

  • Every object's isa pointer points back to the class that created it

  • Declaring a method in the header file advertises the method so that instances of other classes can call it

  • The super directive says 'Run this method, but start the search for its implementation at my superclass.'

  • Object instance variables are all pointers

  • They come in three types: object-type attributes, to-one relationships, and to-many relationships

  • Objects don't live in other objects, they have named pointers which point to other objects

  • On a mobile device, the operating system will kill your program if its memory footprint gets too big

  • When an object has zero owners (no objects have that object as an object instance variable), it figures no one needs it around anymore and deallocates itself

  • The owner count of each object is handled by Automatic Reference Counting (ARC)

  • The strong modifier on the @property declaration says, 'This is a pointer to an object upon which I claim ownership.'

  • When an object is being deallocated, it is sent the message dealloc which every object inherits from NSObject

  • The %@ token in format strings is replaced with the result of sending the description message to the corresponding variable

  • @class <ClassName>; is used instead of #import and gives the compiler less information, but makes the processing of this particular file faster

  • When unnecessary objects don't get deallocated, you are said to have a memory leak

  • Retain cycles are when two object reference each other so neither one gets deallocated resulting in a memory leak

  • A weak reference is a pointer that does not imply ownership

  • In a parent-child relationship, the general rule for preventing a retain cycle is the parent owns the child, but the child should not own the parent

  • alloc makes you take ownership of the object

  • You take ownership of an object by sending it the message retain

  • When you own an object and no longer need it, send it the message release or autorelease

  • A sort descriptor has the name of a property of the objects contained in the array and whether that property should be sorted in ascending or descending order

  • The property you sort on can be any instance variable or the result of any method of the object

  • A predicate contains a statement that might be true

  • Sets are primarily useful for asking the question 'Is it in there?'

  • Dictionary is a collection of key-value pairs where the key is typically a string, and the value can be any sort of object. The key-value pairs are not kept in any particular order

  • #import ensures that the preprocessor only includes a file once. #include will allow you to include the same file many times

  • When importing a file, you may use quotes or angle brackets. Quotes indicate that the header is in your project director. Angle brackets indicate the header is in one of the standard locations

  • #define tells the preprocessor, 'Whenever you encounter A, replace it with B before the compiler sees it.'

  • When you use #define to do function-like stuff instead of simply substituting a value, you are creating a macro

  • Use global variables and enums for constants, not #define

  • File paths can be absolute or relative: absolute paths start with / that represents the top of the file system, whereas relative paths start at the working directory of the program

  • A protocol is a list of method declarations

  • Property lists are an array of dictionaries with string keys and date objects (plist)

  • Cocoa is the collection of frameworks written by Apple that you use to write applications on the Mac

  • Cocoa Touch is used to write iOS apps

  • The app delegate is the starting point of the application

  • Model-View-Controller (MVC)

  • And class that you create should fall into one of three job categories: model, view, or controller

  • Models are responsible for storing and making it available to other objects

  • Views are the visual elements of an application. They know how to draw themselves on the screen and how to respond to user input. They have no knowledge of the actual data that they display or how it is structured and stored

  • Controllers perform the logic necessary to connect and drive the different parts of your application. They process events and coordinate the other objects in your application. They receive messages from some objects and dispatch instructions to others

  • UIApplication controls your application's state

  • The AppDelegate is the delegate of the UIApplication instance

  • When the UIApplication instance is launched it sends its delegate the message application:didFinishLaunchingWithOptions:

  • This is where you put everything that needs to happen or needs to be in place before the user interacts with the application

  • Controls are views that the user can interact with. Ex: buttons, sliders, and text fields

  • First responder access means that the control gets dibs on handling any text input from the keyboard or any shake events

  • Alloc creates the space for an object, and init makes the object ready to work

  • init is an instance method that returns the address of the initialized object

  • When you create a class whose designated initializer has a different name than its superclass's designated initializer, you have a responsibility to document that in the header file

  • The designated initializer will call the superclass's designated initializer before initializing its instance variables

  • A property can be declared as readwrite or readonly with attributes. Readwrite is default

  • A property can also be declared unsafe_unretained, strong, weak, or copy

  • Unsafe_unretained is default for non-object properties

  • Strong is default for object properties

  • Weak does not imply ownership of the object pointed to. If the object is deallocated, the property will be set to nil

  • Copy forms a strong reference to a copy of the object passed in. It makes a copy of an object and then changes the pointer to refer to this copy

  • Nonatomic will make your setter method run a tiny bit faster

  • Key-value coding is the ability to read and set a property using its name

  • Categories let a programmer add methods to any existing class

  • Blocks are equivalent to anonymous functions, closures, or lambdas

  • They use a caret (^) instead of a function name

  • Typedefs belong at the top of the file or in a header, outside of any method implementations