Monday, March 18, 2013

iOS app development for C/C++/C# Developers : Defining default constructor in Objective-C

In this post I am going to share how we do whats called "constructors" in C++ or C# and for my C-only fellows, object initializers or <insert some term here>.

As I mentioned in my previous blog post that Objective-C has no such thing as constructors. We use the native non-instance method or what we call "static" method to allocate a new instance. That method is called alloc.


 MyClass* myObj = [MyClass alloc]

The above code just creates an object in a fashion similar to having no constructors defined in our C++ or C# class.

But consider the following code in C++.


 class Circle
  {
    private int m_radius;
    public Circle()
     {
       m_radius = 10; // C++ folks can also use the "initializer list" for this assignment
     }
  }

If we want to implement the same logic in Objective-C, we are going to have to define something what most Obj-C developers call "initializers"

Initializers are nothing but methods that may take arguments and meant to be called right after allocation of a new object. So we could do the following in Objective-C. Lets do a "default constructor" in this post. I will make another post for parameter-ized constructor


Circle.h file
 #import <foundation/foundation.h>

@interface Circle : NSObject {
  NSInteger m_radius;
}

 - (id) init;
@end

Circle.m file
 @implementation Circle
  - (id) init
  {
    self = [super init]; // calling base class's init in this case NSObject's init

    if (self) { // checking to see if an object has been returned by NSObject's init
            self.m_radius = 10; // set the default value to 10
      }
   return self; // In order for it to act like a constructor, it should return the object
  }
 @end

And this is how we create an object using the initializer defined above
 Circle* circleObj = [[Circle alloc] init];

By the way, 'super' is a Java term for base class too. Somebody gets a title of 'copy-cat' here.

That's all folks for this post. Please let me know your comments in the comment box below if you have any.

12 comments:

  1. The following pages teach you how to build a simple Android app. You’ll learn how to create a "Hello World" project with Android Studio and run it. Then, you'll create a new interface for the app that takes some user input and opens a second screen in the app. Best More learn about apps Apps Design and Development Company its guide a further about this services and gives you traning as well.

    ReplyDelete
  2. Wow i can say that this is another great article as expected of this blog.
    IOS Application Development Pakistan

    ReplyDelete
  3. This post is really very interesting and the programming part is very informative. Thank you so much for sharing this post. Visit here:  Rajendra Geda

    ReplyDelete
  4. This website and I conceive this internet site is really informative ! geniusupdates

    ReplyDelete
  5. If you're interested in outsourcing product development, I think this piece of content will help you find a product design and development company. for your next project.

    ReplyDelete
  6. I admire this article for the well-researched content and excellent wording. I got so involved in this material that I couldn’t stop reading. I am impressed with your work and skill. Thank you so much.
    android development company in chennai

    ReplyDelete
  7. I admire this blog for really good contents and highly informative blog. This blog is useful for who are seeking the outsourced products. Mobile app Development

    ReplyDelete
  8. Undertaking apps are mission-basic and intended to satisfy a particular exceptionally centered errand. Thusly, to be fruitful, https://iphoneappdevelopmentcompany.com/ organizations need to begin by characterizing the issue to be explained working with the end clients and different partners.

    ReplyDelete
  9. Thanks for sharing this blog with us. Get the best developers delivering custom & mobile application development services from app development Denver

    ReplyDelete
  10. The article was up to the point and described the information very effectively. Thanks to blog author for wonderful and informative post.
    IOS Development pakistan

    ReplyDelete