Think Different: Objective-C for the .NET developer

Post on 26-Jun-2015

426 views 1 download

Tags:

description

One of the major stumbling blocks for new iOS developers is dealing with Objective-C. The fear of Objective-C has led some to avoid it completely and revert to Ruby Motion, Xamarin, Phonegap or Titanium. While there are valid reasons to use each of these frameworks, the fear of Objective-C shouldn’t be one of them. We’ll walk you through some of the more difficult concepts of Objective-C and compare them to concepts you might be familiar with from using .NET or Java.

Transcript of 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

Common ComplaintsToo verbose

Unmanaged memory

Complicated syntax

Lack of type safety

Unfriendly community

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

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

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

PropertiesAutosynthesized vs. Autoimplemented properties

Atomic vs nonatomic

Strong vs Weak references

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

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)

Why []?

C# Objective-c

Object Initializationalloc - allocates the memory for an object

init - initializes the attributes

Custom initializers are the equivalent of custom constructors

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

Interact with existing classes

Respond to events and requests for information

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

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

CategoriesSimilar to extension methods in C# .NET

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

Class InheritanceWorks almost exactly like .NET class inheritance

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

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>

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

BlocksChunks of code that can be passed as an argument

Similar to anonymous methods

Blocks cont...

Declaring a block reference

Invoke the block

Be careful to check if block is null

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)

Resources

http://developer.apple.com

http://www.raywenderlich.com/

@shawnprice84