Objective-C in a fast way — Part 1

Onur Hüseyin Çantay
5 min readJan 16, 2019

Is Objective-C still worth to spend time on it ?

For now I can only say that swift works together with Objective-C,
at the core of our apps we still have the clues of it.

The Basics

In Objective-C we have 2 types of file; header files and implementation files.
in header files we are going the explain what it should have at the end of coding and we don’t have to implement the implementation file to another implementation file just add the header and wolla it’s done 😬

I will start with the important part of coding before starting code you should be able to explain your code on a big team so here are some documentation rules in objective-c

/*!@brief It converts temperature degrees from Fahrenheit to Celsius scale.@discussion This method accepts a float value representing the temperature in <b>Fahrenheit</b> scale and it converts it to the <i>Celsius</i> scale.To use it, simply call @c[self toCelsius: 50];@param  fromFahrenheit The input value representing the degrees in the Fahrenheit scale.@codefloat f = [self toCelsius:80];@return float The degrees in the Celsius scale.*/

Output of the documentation code can be seen below;

In Objective-C base variables like NSString,NSNumber etc are objects and we can define an object at 2 ways with alloc and init or Literals

Conclusions

  1. With literals if you want to insert nil value to an array or an dictionary it will throw an exception so you can’t add nil value with literals
  2. Acces values with subscription methods
  3. Create objects with literal syntax rather than normal alloc and init functions so it would be more clearer and makes it much easier to read the code

Objective-C is a C based language because of that it will come with some C features like #define or typedef

But it’s important to know that #define uses a technique that just replaces all equivalent keys with the defined value its maybe helpful but it doesn’t give any type as example if we define an iOS App Status Bar Height as;

#define STATUS_BAR_HEIGHT 5;

İt will check the code and replace all keys ‘STATUS_BAR_HEIGHT’ with 5 but what if my function gets an double value or an float value then our compiler couldn’t be able to compile the code so we will jump over this problem with the compiler feature,

static const NSDouble * statusBarHeight = 5.0;

With const we are saying that the value will never change and with the static we are saying that the variable is local to the translation unit in which it is defined

Translation Unit : is the input the compiler receives to generate an object file

Objects

Objects in Objective-C will usually contain a set of instance variables to store the data they need to work.

A simple declaration of a class would be like;

When we are declaring properties the compiler will generate auto references on compile time like _firstName to access the variables from that reference but it creates also getter setter functions

The default values for variables are with underscore so if you don’t like underscore you can use

@synthesize to declare a naming style for variables or

If you don’t want that compiler generates code for you, you can just add @dynamic but this time if you want to access the variables you will get an compiler error

Property Attributes

there are 4 attributes that can be applied to the property these are;

  1. Atomicity
  2. Read / Write
  3. Memory Management Semantics
  4. Method Names

1.Atomicity

Defining a property as atomic means that it guarantees that it will return a value “looks like optional in swift :)”

If you don’t specify anything your property will be atomic.

Nonatomic properties doesn’t give a guarantee that it will return a value but this comes with an enhanced speed

of accessing these properties so make your choice Performance ? / Safety ?

2.Read / Write

readwrite if the property is synthesized the compiler will generate both getter and setter for the property
readonly the property would be readonly and would have a getter 🙂

3.Memory Management Semantics

assign

  • assign is a property attribute that tells the compiler how to synthesize the property’s setter implementation

strong “retain”

  • strong is used with ARC and it basically helps you , by not having to worry about the retain count of an object. ARC automatically releases it for you when you are done with it.Using the keyword strong means that you own the object.

weak

  • Weak is essentially assign, a unretained property. Except when the object is deallocated the weak pointer is automatically set to nil

copy

  • This designates an owning relationship similar to strong however instead of retaining the value, it is copied.
  • This is often used when we are working with mutable variables like strings while we are changing the value on setter we should be sure that it won’t be change underneath the object.

4.Method Names

getter=<name>

  • Specify the name of the getter usually it has been used for booleans

setter=<name>

  • Speciefies the name of the setter usually its not used commonly

Object Equality

Like we said before the main components in objective-c are objects so if you are trying to compare objects,

If our objects haven’t the same hash values that means that we don’t have to iterate all variables or important variables for us to check if it is equal or not so as a summary; equal objects has to be the same hash value but objects that does have the same hash value mustn’t have to be equal.

Class Cluster Pattern

The Class Cluster Pattern is a kind of factory pattern which has a extra option that it will return different extensions of a class so as an example we have 3 type of posts on social media post with photos post with plain text and videos
So we have a base class which has 1 like action but with different animations
Each post type have specific utilities so if we are creating a post and we are specifying a type than that will be a class with class cluster pattern

Code is Coming !

You can Continue with Part — 2 :)
https://medium.com/@ohc3807/objective-c-in-a-fast-way-part-2-98655f2220fb

References

  1. https://developer.apple.com/library/archive/documentation/DeveloperTools/Conceptual/HeaderDoc/tags/tags.html
  2. https://www.appcoda.com/documenting-source-code-in-xcode/
  3. https://stackoverflow.com/a/15541801/9133783
  4. Matt Galloway — Effective Objective-C 2.0 52 Specific Ways to Improve Your iOS and OS X Programs (2013, Addison Wesley)

--

--