This deck provides an overview of key concepts in Objective-C including MVC architecture, classes, instances, methods, properties, delegates and protocols, Xcode IDE, and common classes and terms. It explains that MVC separates applications into modular and replaceable models, views, and controllers. Classes define properties and methods while instances contain property values. Methods are messages sent to objects. Properties use accessor methods. Header files define public interfaces while implementation files contain private code. Delegates and protocols allow communication between decoupled components.
How Recreation Management Software Can Streamline Your Operations.pptx
Intro to iOS Development • Made by Many
1.
2. In This Deck
MVC Architecture
Objective-C Syntax
Classes
Instances
Methods
Properties
Setters + Getters
Header + Implementation
Delegates + Protocols
Xcode IDE
Key Classes + Terms In Objective-C
This deck is shared with you under a Creative Commons license.
3. Cloud Services User Input
Model
Coordinates between model and view
Contains all application logic and data
View
Your application’s graphical user interface
View Controller
The MVC architecture is useful because it lets you abstract apart and organize your app into functional,
replaceable modules.
NSNotification
MVC Architecture
(Model + View + Controller)
4. MVC Architecture
(Model + View + Controller)
Model View Result
+ =
Tic Tac Toe Visual
Tic Tac Toe Textual
X played: (2, 3)
O played: (3, 1)
X played: (1, 3)
SMS Textual
Chris said: CoD?
Adam said: nm, u?
HJ said: sup
same model • different view
different model • same view
5. Objective-C
Syntax
[[Vehicle alloc] initWithMake:@“Toyota” model:@“Prius” color:[UIColor blackColor]];
Objective-C is a verbose language.
Method names and variable names tend to be very long.
This is supposed to make it easier to understand your code and how to use methods.
6. Objective-C
Syntax
Objective-C is an object-oriented language.
Methods are really “messages” send to objects or classes telling them to do things.
For example:
[myCar setSpeed:50.0];
is really saying:
“Tell myCar to set its speed to 50.0 mph.”
50.0 mph,
please!
7. Objective-C
Syntax
Xcode will attempt to autocomplete whatever you’re typing.
This not only helps you save time (you can hit Tab to autocomplete) but also helps you discover relevant
methods and properties.
8. Objective-C
Classes
Vehicle myCar yourCar
> make @“Toyota” @“Lamborghini”
> model @“Prius” @“Aventador”
> speed 50.0 95.0
Think of a class as a category, e.g., Vehicle.
A class has no data but can perform some basic methods.
A class also specifies what properties instances of that class should have.
It’s kind of like a form that hasn’t been filled out.
E.g., Vehicle might have properties for make, model, and speed.
9. Objective-C
Classes
Vehicle
Car Truck Motorcycle
All classes in Objective-C inherit some methods and properties from a superclass.
As a result, each class is a subclass of another class.
E.g., Vehicle might be a subclass of NSObject and might have its own subclasses Car, Truck, and
Motorcycle that have all the properties and methods of Vehicles but also add their own properties and
methods.
10. Objective-C
Instances
An instance is a specific instance of a class.
E.g., myCar might be an instance of Car.
An instance has data and may have some methods that you can perform on that data.
E.g., For myCar, you would have the following values for your properties:
myCar.make = @“Toyota”;
myCar.model = @“Prius”;
myCar.speed = 55.0;
myCar.gas = 10.0;
myCar.owner = @“Ken M. Haggerty”;
Instances are often created using alloc and init, e.g.:
[[Car alloc] init];
or using a “convenience” method, e.g.:
[Car carWithOwner:@“Ken M. Haggerty”];
11. Objective-C
Methods
A class method is a method that is performed on a class rather than a specific instance of a class.
Class methods are often useful for instantiation and general methods not specific to instances.
E.g., [NSArray arrayWithObject:firstObject] creates a new array with one object in it,
or, [TicTacToe howManyInARowToWin] might return 3.
An instance method is a method that is performed on an instance of a class.
Instance methods are useful for changing or manipulating instances of your class.
E.g., If myString is set to @“filename”,
[myString stringbyAppendingString:@“.png”] would return the string @“filename.png”.
12. Objective-C
Properties
Properties are how data are stored in instances of your class.
They are like fields on a form.
Each property defines what kind of data it should hold, but only instances of your class actually hold that
data.
E.g., Car might have the property
@property (nonatomic) BOOL isNewCar;
while myCar, an instance of Car, might have isNewCar set to NO.
13. Objective-C
Setters + Getters
When you create a property, Objective-C will automatically create helper methods for accessing and setting
values for your property.
I.e., When you create:
@property (nonatomic, strong) NSString *owner;
Objective-C will create the following two instance methods for you:
- (void)setOwner:(NSString *)owner;
- (NSString *)owner;
Both of these methods are accessible to you as soon as you define your property.
However, you can always write your own custom implementation for your helper and/or getter.
14. Objective-C
Setters + Getters
Setters are only called when that property is being set explicitly, e.g.:
[myCar setOwner:@“Ken M. Haggerty”];
myCar.owner = @“Ken M. Haggerty”;
Getters can be called in either of the following ways:
[usa name];
usa.name;
The second syntax in each of the above examples is called dot notation and is usually only used for
properties in Objective-C.
15. Objective-C
Header + Implementation Files
“Burger, please!”
(returns burger)
<YourClass>.h
Dictates public interface for this class; i.e., how other
classes can and should interact with this class.
● Imports (#import)
● Protocols (@protocol)
● Public API (@interface)
<YourClass>.m
Dictates private interface for this class + contains all
method implementations for this class.
● Imports (#import)
● Private API (@interface)
● Implementation (@implementation)
In Objective-C, the header (.h) file is “public” while the implementation file (.m) is “private.”
The header file should be organized, clear, and state what the class does and how to use it.
16. Objective-C
Delegates + Protocols
In well-written Objective-C, your model and your view are completely agnostic of each other.
I.e., Your model should not be in any way dependent on how it is ultimately presented to the user, and your
view should not care what kind of data it is representing.
Instead, your model and view should communicate using the following relationships:
● Your view controller can change your model.
● Your model can change itself.
● Your model can send out notifications when it is changed.
● Your view controller can listen for notifications and act accordingly.
● Your view controller can change your view.
To do this, we often used delegates to delegate methods and properties from view controllers to models.
E.g., UITableViewController has two delegates: delegate and dataSource.
When assigning delegates, we can ensure that our delegates implement the necessary properties and
methods by specifying protocols that they should follow.
We can dictate what protocols a property should follow when defining them. E.g.:
@property (nonatomic, weak) id <MyProtocol> delegate;
17. Xcode IDE
(Integrated Development Environment)
Demo
There’s a lot going on in Xcode, so we’ll explore by clicking around.
If you have trouble using Xcode or have any questions, don’t hesitate! Ken will be happy to help.
18. Objective-C
Key Classes + Terms
NSObject or id: Top-level object in Objective-C. Almost all objects in Objective-C inherit
from NSObject.
UIViewController: A basic view controller in Objective-C. All view controllers inherit from
UIViewController.
UIView: A basic view in Objective-C. Views are always rectangles. All views inherit from
UIView. Views can be placed as subviews within another view.
UIButton: A basic button in Objective-C. Buttons can respond to touch events.
UIImage: An image in Objective-C.
UIImageView: A subclass of UIView designed for displaying an image.
UIScrollView: A subclass of UIView designed for scrolling content. Scrollviews are tricky
but incredibly useful.
IBOutlet / IBAction: Designates that a property / method should be connected to a view
via Interface Builder.
nonatomic / atomic: You will always use nonatomic. This has to do with thread safety
regarding setters and getters for properties.
strong / weak: Determines whether a property’s value should be held even if no other
object is pointing to that value. Strong = yes, weak = no.
self: Used within an instance method to refer to the instance the method was sent to.
Interface Builder / Storyboard: Visual interface within Xcode IDE for laying out and
creating your app’s GUI.
UITableViewController: A subclass of UIScrollView designed for presenting scrollable
tables.
UITableView: A subclass of UIScrollView designed for presenting scrollable tables.
UITableViewCell: A subclass of UIView designed for presenting content in tables.
UITableViewCells are reused as the table view is scrolled, increasing responsivity and
smoothness.
NSArray: A subclass of NSObject that contains an uneditable list of NSObjects.
NSMutableArray: A subclass of NSArray where its list of NSObjects are editable.
NSString: A subclass of NSObject for text.
BOOL: A binary type that’s actually just plain C. Can have value YES or NO.
int: A C class for storing integer values.
float: A C class for storing floats (i.s., decimal values).
#import: Add a library, framework, or another class to the current class.
Apple has very thorough documentation available from within Xcode at Help > Documentation and API Reference
and online at https://developer.apple.com/library/ios/navigation/