Think Different: Objective-C for the .NET developer
-
Upload
shawn-price -
Category
Technology
-
view
426 -
download
1
description
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