Download - Think Different: Objective-C for the .NET developer

Transcript
Page 1: Think Different: Objective-C for the .NET developer

Objective-C for the .NET

(Or Java) developer

It’s not as scary as it looks, I promise...

@shawnprice84

Page 2: Think Different: Objective-C for the .NET developer

Common ComplaintsToo verbose

Unmanaged memory

Complicated syntax

Lack of type safety

Unfriendly community

Page 3: Think Different: Objective-C for the .NET developer

Classes and ObjectsObject-oriented, dynamic language (no type safety)

No Namespaces, Prefix class names with 2-3 letters to help differentiate your classes (NSObject)

Page 4: Think Different: Objective-C for the .NET developer

Interfaces and ImplementationsAn interface Provides a contract, but for a single implementation (not reusable)

Page 5: Think Different: Objective-C for the .NET developer

PropertiesAutosynthesized vs. Autoimplemented properties

Atomic vs nonatomic

Strong vs Weak references

Accessing a property with dot (“.”) notation uses the synthesized accessor methods

Page 6: Think Different: Objective-C for the .NET developer

Methods+ indicates a class method (static), - indicates an instance method

Each parameter should have a description of what the property indicates, the type and the parameter name

Call a selector (method) on nil will do nothing (no exception thrown)

Page 7: Think Different: Objective-C for the .NET developer

Why []?

C# Objective-c

Page 8: Think Different: Objective-C for the .NET developer

Object Initializationalloc - allocates the memory for an object

init - initializes the attributes

Custom initializers are the equivalent of custom constructors

Page 9: Think Different: Objective-C for the .NET developer

DelegatesNot just method stubs (like .NET) - full objects with multiple methods

Interact with existing classes

Respond to events and requests for information

Page 10: Think Different: Objective-C for the .NET developer

Automatic Reference Counting(ARC)

Provides ultra-simple memory management

Keeps track of active references to an object

When an object’s reference count reaches 0 it’s removed from memory immediately (unlike GC)

Only real danger is a circular reference

Page 11: Think Different: Objective-C for the .NET developer

Avoid a circular reference

Delegates should always be weak (or unsafe_unretained) and of type “id”

If a block is stored in a strongly held property then only pass in weak references to ivars and self

Page 12: Think Different: Objective-C for the .NET developer

CategoriesSimilar to extension methods in C# .NET

Adds methods to existing classes without the need to create a new object type

Page 13: Think Different: Objective-C for the .NET developer

Class InheritanceWorks almost exactly like .NET class inheritance

Used much less frequently in Objective-C. Categories and Delegates are preferred

Page 14: Think Different: Objective-C for the .NET developer

Protocols

Closest thing to interfaces in Objective-C

Defines a contract that an object must adhere to

Can also have optional methods

Declare an object adheres to a protocol using <ProtocolName>

Page 15: Think Different: Objective-C for the .NET developer

Collections

NSArray, NSSet, NSDictionary, NSHashTable

Collections are immutable unless specified (NSMutableArray)

Objective-C collections can’t store primitive types (NSNumber, NSString, NSValue)

To store primitive types you can use a C-style array

Page 16: Think Different: Objective-C for the .NET developer

BlocksChunks of code that can be passed as an argument

Similar to anonymous methods

Page 17: Think Different: Objective-C for the .NET developer

Blocks cont...

Declaring a block reference

Invoke the block

Be careful to check if block is null

Page 18: Think Different: Objective-C for the .NET developer

In Conclusion

Don’t be afraid to try it

Most of the hatred for Objective-c is based on outdated ideas (or poorly written code)

Page 19: Think Different: Objective-C for the .NET developer

Resources

http://developer.apple.com

http://www.raywenderlich.com/

@shawnprice84