Objective-C talk

Preview:

DESCRIPTION

A quick overview of Objective-C.

Citation preview

Getting Started with Objective-C and Xcode

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.

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

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

Java: Person Classpublic Class Person{ ! private String name; private int age; private Date birthday; public Person(){ name = “”; age = 0; birthday = null; } !!

! public Person(String n, int a, Date b){ this.name = n; this.age = a; this.birthday = b; } }

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

Objective-C Person Class (Implementation File)

@implementation Person !//properties have getters and setters synthizised for you, but I’ll show you one !- (void)setName:(NSString *)name{ //self.name = name will cause and infinite loop //why? _name = name; } !- (NSString *)name{ return _name;

} !- (id)init{ self.name = @“”; self.age = 0; self.birthday = nil; } !- (id)initWithName:(NSString *)name age:(int)age (NSDate *)birthday{ self.name = name; self.age = age; self.birthday = birthday; }

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)

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>

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.

Live Demo Time

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)

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/developing-ios-7-apps-for/id733644550 (recently updated)

• NSScreencast - http://nsscreencast.com/