Objective-C – Extra Features



Objective-C – Extra Features

0 3


Random-ObjC-Features

Slides from my presentation at The Brooklyn iOS Meetup on 9/17

On Github jmburges / Random-ObjC-Features

Objective-C

Extra Features

Created by Joe Burgess

Who Am I

Teacher at The Flatiron School

Preface

Quick word about CLang and LLVM

LLVM: Modern extensible compilerCLang: C language family frontend for LLVM

Literals

Why don't more people use these?

Numbers

NSNumber *fortytwo = @42
NSNumber *pi = @3.1415926535

Add in f for float and a l for long

NSNumber *fortytwoLong = @42L
NSNumber *pi = @3.1415926535F

One more thing

NSNumber *threeOverTwo = @(3 / 2);

Collections - Arrays

NSArray *array = @[ @"Hello", @"World"];

Collections - Dictionaries

NSArray *dictionary = @{
@"message" : @"Hello, World!"
@"sender"  : @"Joe Burgess"
};

These are all immutable

Object Substring

  • Custom Index Subscripting
array[0]=@"bar"
Custom Keyed Subscripting
dictionary[@"foo"]=@"bar"

Custom Index Subscripting

- (id)objectAtIndexedSubscript:(NSUInteger)idx;
- (void)setObject:(id)obj atIndexedSubscript:(NSUInteger)idx;

Custom Keyed Subscripting

- (id)objectForKeyedSubscript:(id )key;
- (void)setObject:(id)obj forKeyedSubscript:(id )key;

Dot Notation Is Important!

[[class[@"teachers"] lastObject] firstName]

Message Forwarding

just like methodMissing in Ruby

Methods

-(NSMethodSignature*)methodSignatureForSelector:(SEL)sel
-(void)forwardInvocation:(NSInvocation*)inv

NSMethodSignature

Tells Objective-C the method signature just like in your header files

NSInvocation

Actually does the method.

Objective-C is C

Structs

struct
{
double lat;
double long;
} point

Don't use them

@interface point : NSObject
{
@public
    @property double lat;
    @property double long;
} 
@end

#defines

#define MAX_USERS ((int) 42)

Don't Use Them

// in a header
extern const int MAX_USERS;
// in a C file
const int MAX_USERS = 42;

New Stuff

Introduced in recent versions of Clang

#includes suck

  • Recursive import means they are slow
  • If the file name changes... :(

Use @include!

  • It's more of an API to a library.
  • Supports autolinking!
  • Everything is cached!

It's Easy

@import MapKit.MKMapView

Implement your own

You can't. Only Apple can. Lame.

THE END

References