2. What is Objective-C?
•
Objective-C is an object-oriented language, built on top of C
•
Objective-C adds Smalltalk-style message passing to C
•
It is also the default language used to program for Mac OS X
and iOS devices.
3. Message Passing? What’s
that about?
•
You can think of message passing as calling a method… well
almost
•
When you pass a message to an object in Objective-C, you’re
asking that object to look up the implementation of a method
of the same name as your method.
•
When that object finds an implementation, it calls that method.
•
The receiver of the message is also not guaranteed to respond
to the message at all
4. Anything else I need to
know?
•
Yeah! Don’t get scared by the syntax!
•
At first, all the square brackets can freak people out.
•
Once you get used to reading the messages, you’ll wonder
how you ever understood a Java method call or C function
call in the first place. (Kidding, kindof)
•
Let’s look at some code
5. Java: Person Class
public Class Person{
!
!
public Person(String n,
int a, Date b){
this.name = n;
this.age = a;
this.birthday = b;
}
}
private String name;
private int age;
private Date birthday;
public Person(){
name = “”;
age = 0;
birthday = null;
}
!
!
6. Objective-C Person Class
(Header)
@interface Person : NSObject
@property (strong, nonatomic) NSString name;
@property (nonatomic) int age;
@property (strong, nonatomic) NSDate birthday;
- (id)init;
- (id)initWithName:(NSString *)name age:(int)age (NSDate *)birthday;
!
@end
7. Objective-C Person Class
(Implementation File)
@implementation Person
}
!
!
//properties have getters and
setters synthizised for you, but
I’ll show you one
- (id)init{
self.name = @“”;
self.age = 0;
self.birthday = nil;
}
!
- (void)setName:(NSString *)name{
//self.name = name will
cause and infinite loop
//why?
_name = name;
}
!
- (NSString *)name{
return _name;
!
- (id)initWithName:(NSString
*)name age:(int)age (NSDate
*)birthday{
self.name = name;
self.age = age;
self.birthday = birthday;
}
8. A few small notes
•
Do I have to manage my own memory with Objective-C? It is C after all...
Not anymore. You have the option of running a Garbage Collector or using Automatic
Reference Counting (which is way awesome). You could also manage your own memory
with Manual Reference Counting
•
What's that "id" thing on the last screen?
id holds any pointer to any object in Objective-C. Think about it like a void *
•
Every Functional Programmer's favorite question: Does it have closures?First class
functions?
Yes! (and no) Objective-C has blocks, which are part of LLVM and Clang. I'm sure
someone will fight me that these are real closures, but they're pretty close. You can also
pass them around to like first class functions (sort of)
9. Does @interface create a
Java-like interface?
•
Short answer is no.
•
@interface declares the methods/properties/variables that you’re going
to have available in your class. There is an option to include a private
@interface in your .m file
•
In Objective-C, Java-like interfaces are called Protocols
•
You define them with @protocol, and can specify optional and required
methods.
•
You declare that you implemente a protocol like this:
@interface Person : NSObject <MyProtocolHere>
10. One More Thing:
Categories
•
Categories provide a way for you to customize an existing class that you didn't write
•
This works pretty much the same way as C# Extension methods if you're familiar with those.
•
Why do you want to use this? What if you want to add a method to a built in class that adds
functionality without subclassing?
•
Here's what categories look like:
@interface UIColor : NSObject (HexColor)
+ (UIColor *)colorWithHex:(NSString *)hexCode;
@end
•
We usually put this in a file called UIColor+HexColor.h and anywhere you import that file,
colorWithHex: will be available. Anywhere we don't, we don't get that method.
12. Other Cool Things to
Research
•
Model View Controller
•
Delegation
•
Blocks
•
Key Value Observing / Key Value Coding
•
The Objective-C Runtime
•
Class Posing (advanced)
•
Method Swizzling (advanced)
13. Great Resources
•
Apple Developer Site - https://developer.apple.com
•
Big Nerd Ranch iOS Programming Book http://www.bignerdranch.com/book/
ios_programming_the_big_nerd_ranch_guide_rd_edition_
•
NSHipster - http://nshipster.com
•
Stanford iOS Course - https://itunes.apple.com/us/course/developingios-7-apps-for/id733644550 (recently updated)
•
NSScreencast - http://nsscreencast.com/