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.

85 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 blog for really good contents and highly informative blog. This blog is useful for who are seeking the outsourced products. Mobile app Development

    ReplyDelete
  7. 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
  8. Thanks for sharing this blog with us. Get the best developers delivering custom & mobile application development services from app development Denver

    ReplyDelete
  9. 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
  10. This comment has been removed by the author.

    ReplyDelete
  11. I am really happy to say it’s an interesting post to read . I learn new information from your blog.
    Hybrid App Development Company in USA

    ReplyDelete
  12. bachelor developer software company
    hello brother's your bachelor developer software company provide high qualty software provider ricently create a school menegment system provide a very low bujet for school's with a androide application.

    SEO
    hello brother's your software company provide a high quality seo websites ranking on page and off page.

    graphic design
    hello brother's our software company provide graphic designing service by bachelor developer.

    software company in hyderabad
    software company in pakistan

    hello brother's your bachelor developer software company provide high qualty software provider ricently create a school menegment system provide a very low bujet for school's with a androide application.
    Web design company

    bachelor developer software company provide high quality software and websites and more services.
    cheap website design pakistan

    software house in pakistan

    software house in sindh

    web development company pakistan

    software house in latifabad

    ReplyDelete
  13. Thanks for sharing this quality information about app development. I really enjoyed reading and knowing about it. And i will surely share this URL with my friends. Mobile App Development Service Provider Australia

    ReplyDelete
  14. Thanks for sharing such an amazing blog that helped me to understand the basic points of IOS app development. You can also hire professional developers such as NOTOSolutions to get your work done.

    ReplyDelete
  15. Great post I would like to thank you for the efforts you have made in writing this interesting and knowledgeable article. Thank you a lot! and if you need Graphic Design Company then contact us!

    ReplyDelete
  16. This comment has been removed by the author.

    ReplyDelete
  17. Very nicely explained. A2zfame.com is offering you the best quality of creative website development online at the affordable prices. You can visit on mobile application maintenance in USA to get more details.

    ReplyDelete
  18. This comment has been removed by the author.

    ReplyDelete
  19. Apps Geyser is a free specialist organization, which help you to change over your application and profit through it. It has more than 118,602,408 introduced application, more than 260,193 made application and more than 3,610,134,404 promotions served.
    https://appcloner.app
    https://appcloner.net

    ReplyDelete
  20. This post is really useful and helpful to know more about the things which you have shared. I appreciate you for such a great amount of information. I assure this would be beneficial for many people. app development companies in USA

    ReplyDelete
  21. 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. ios app developers

    ReplyDelete
  22. Thanks 3monthinstallmentloans.net for taking the time to discuss about easy loans for bad credit in Chandler, AZ

    ReplyDelete
  23. You have shared a nice article about the app development. Your service is very interesting and useful. I liked it. If anyone looking to app development service, then visit Custom App development Houston

    ReplyDelete
  24. I’m going to read this. I’ll be sure to come back. thanks for sharing. and also This article gives the light in which we can observe the reality. this is very nice one and gives indepth information. thanks for this nice article... IDLE MINER TYCOON MAX LEVEL

    ReplyDelete
  25. This comment has been removed by the author.

    ReplyDelete
  26. In a space pervaded with compact mobile phones, innovations drive mobile users towards a legal digital predilection. On an average, mobile users spend as little as 2 hours a day interacting with their mobile screens. Of this time, they tend to devote some 30 minutes fiddling with mobile apps.Top 10 Mobile app development Companies in India

    ReplyDelete
  27. This is very informative and interesting for those who are interested in App developement. Thank you for such useful informations about Apps. You can also checkout our best services of app developers australia.

    ReplyDelete
  28. This comment has been removed by the author.

    ReplyDelete
  29. Great post, it was nice to see this article. It was really appreciable. Thank you so much for sharing such an informative article. Get more info about mobile app design company

    ReplyDelete
  30. This comment has been removed by the author.

    ReplyDelete

  31. It's very nice of you to share your knowledge through posts. I love to read stories about your experiences. They're very useful and interesting. I am excited to read the next posts. I'm so grateful for all that you've done. Keep plugging. Many viewers like me fancy your writing. Thank you for sharing precious information with us.
    Get more info about web development company Saudi Arabia

    ReplyDelete

  32. Hey what a brilliant post I have come across and believe me I have been searching out for this similar kind of post for past a week and hardly came across this. Thank you very much and will look for more postings from you
    Get more info about mobile app development company

    ReplyDelete
  33. Android App Maker make simple to make your very own application. Bulk SMS Service Provider in Sharjah UAE

    ReplyDelete
  34. You have shared a nice article here ios app development Denver. Your article is very informative and useful. Thank you for sharing this article here.

    ReplyDelete
  35. The article you have shared here is great. I read your post with carefully, the points you mentioned can be very helpful. It is nice seeing your wonderful post.Get for more information best power bi dashboards

    ReplyDelete
  36. It’s great to come across a blog every once in a while that isn’t the same out of date rehashed material. Fantastic read.Get for more information android app development

    ReplyDelete
  37. You have written too well in your blog about app development. These are good pieces of knowledge for those who are looking for this information which you got here. Thank you. Mobile App Development with ERP Packages in Vadodara

    ReplyDelete
  38. Positive site, where did u come up with the information on this posting?I have read a few of the articles on your website now, and I really like your style. Thanks a million and please keep up the effective work.Get for more information android app development

    ReplyDelete
  39. It's a nice article, Which you have shared here about the iOS app development. Your article is very interesting and I really liked the way you expressed your views in this post. Thank you. iOS Apps Development Services in Sharjah

    ReplyDelete
  40. Very informative article, Which you have shared here about the IOS App development. After reading your article I got very much information and it is very useful for us. I am thankful to you for sharing this article here. mobile app developer Singapore

    ReplyDelete
  41. Do you know that there is an increase in the number of teenage mobile app developers? Several big companies are encouraging the efforts of these young developers irrespective of their age and education.Best Mobile App development Companies in UK

    ReplyDelete
  42. You can without apk mods much of a stretch associate your gadgets to 3D TV sets and make the most of your preferred motion pictures and games.

    ReplyDelete
  43. Very interesting,good job and thanks for sharing such a good blog. your article is so convincing that I never stop myself to say something about it. You’re doing a great job.Keep it up. Real Estate Agent Websites

    ReplyDelete
  44. Wow, impressive blog and its information. as we provide Employee identity theft protection at affordable prices. for more info visit our website.

    ReplyDelete
  45. I think this is an informative post and it is very useful and knowledgeable. therefore, I would like to thank you for the efforts you have made in writing this article.website designing Pakistan

    ReplyDelete
  46. Now that you are aware of the benefits of investing in the Android app, you can develop your app on this most promising platform to stay ahead in this competitive market.Smart AudioBook Player APK

    ReplyDelete
  47. In addition, to hold their conviction, android application developers should put effort on it, and they should ensure delivering a star app to their clients. However, developing an Android app is never so easy, as it sounds to be.weblink

    ReplyDelete
  48. Miguelcleon is a sharing site with Mod Apk game versions, free apps for Android. A site that I trust.

    ReplyDelete
  49. This chronicle record is known as Android Application APK, for example Android bundle unit. itools crack

    ReplyDelete
  50. I found so many interesting stuff in your blog especially its discussion. charbytes is offering the great deals on the artificial intelligence solutions in Glasgow, For more visit our website charbytes.com.


    ReplyDelete
  51. I have been exploring for a little for any high quality articles or blog posts in this kind of area . Exploring in Yahoo I ultimately stumbled upon this web site. Reading this information So i¡¦m glad to show that I have an incredibly good uncanny feeling I came upon exactly what I needed. I such a lot indubitably will make certain to don¡¦t forget this web site and give it a look a relentless basis. iphone psd

    ReplyDelete
  52. Thanks for giving me your time to share this article here about mobile app development in Your article is very attractive and I am affected by the details that you have shared in this post. mobile app developers denver

    ReplyDelete
  53. Recently, I didnt give lots of thought to leaving comments on site page articles and have placed comments even much less. Reading by means of your nice article, will help me to do so sometimes. device mockups

    ReplyDelete
  54. Most of the time I don’t make comments on websites, but I'd like to say that this article really forced me to do so. Really nice post. Best iOS App Development Service

    ReplyDelete
  55. I’ve read some good stuff here. Definitely worth bookmarking for revisiting. I surprise how much effort you put to create such a great informative website. buy instagram video views uk

    ReplyDelete
  56. This information is meaningful and magnificent which you have shared here about the Web Application Development Services. I am impressed by the details that you have shared in this post and It reveals how nicely you understand this subject. I would like to thanks for sharing this article here.

    ReplyDelete
  57. Great information to say the least. I really do appreciate everything so much from this great website.keep sharing more blogs like this.
    IELTS Coaching in chennai

    German Classes in Chennai

    GRE Coaching Classes in Chennai

    TOEFL Coaching in Chennai

    spoken english classes in chennai | Communication training


    ReplyDelete
  58. Hybrid App Development are developed using HTML5 and can be installed and used on any mobile device without too many compatibility issues.

    ReplyDelete
  59. Found this article is full of informative and helpful content, really love to read it. To know more you can visit here Restaurant app development company USA

    ReplyDelete
  60. You have shared nice information about IOS App Development which is also a good topic came under app development of various platforms.Thanks for such useful post. For best AI Based Hand Tracking Framework you can visit at dore.

    ReplyDelete
  61. Really, This is an impressive as well as Useful post for me. I got the most valuable and informative information from here. Thanks for sharing, I request you to keep sharing such blogs. Mobile Application Developer in USA

    ReplyDelete
  62. such a nice piece of article. Very good explanation. Thank you for sharing your knowledge. This is a very valuable thing you shared. good stuff!!Mobile App Development Company

    ReplyDelete
  63. Thanks to share this best information with us. Please visit here to get more latest updates about app development firms in India visit our website on Smvatech.com

    ReplyDelete
  64. Your blog is very useful for those who are searching for this kind of information, this information is a solution for those who are confused about it. Thanks for sharing this blog with us.Freelance website designer Calgary

    ReplyDelete
  65. Thank you for sharing your knowledge with all of us. I read your post and this blog is very good. You have good knowledge on this. This post really impressed me. Adwords Solutions

    ReplyDelete
  66. The representation of this article is actually superb. I think this is a genuinely beneficial and instructive article for everyone, I appreciate this kind of writing, Thankful to you for sharing an article like this.Web Application Development

    ReplyDelete
  67. This is the place where it can get precarious. Assuming it's a game where you can make a gathering that is welcome just, you'd simply welcome your companions or family. This is the place where it can get precarious. Assuming it's a game where you can make a gathering that is welcome just, you'd simply welcome your companions or family.

    ReplyDelete
  68. Usually, one mailing will out pull the other. You can do the same thing online. Actually, with modern technology, it's even easier. lmfdb org

    ReplyDelete
  69. I generally check this kind of article and I found your article which is related to my interest. Genuinely it is good and instructive information, Mobile App Development Company Dubai Thankful to you for sharing an article like this.

    ReplyDelete
  70. I am very thankful to you that you have shared this information with us. Read more info about App development company. I got some different kind of knowledge from your web page, and it is really helpful for everyone. Thanks for share it.

    ReplyDelete
  71. Download Setup + Crack. Sandra Lite 31.93 works over the lines of other Windows utilities. Nonetheless, it attempts to go you a lot more of.Sisoftware Crack Download

    ReplyDelete
  72. Happy Thursday wishes, Thursday Quotes, Thursday Graphics, Thursday Facebook, Have a great Thursday Wishes, Have an Awesome day, Thursday Inspiration Quotes

    ReplyDelete
  73. I would like to say this is a well-informed article and also beneficial article for us. Keep sharing this kind of articles, Thank you.website design service

    ReplyDelete
  74. I am attracted by the presentation of this article. It is a genuinely a gainful article for us. Keep posting, Thank you.Ecommerce App Development Company Singapore

    ReplyDelete
  75. I for the most part check this sort of caution post and I found your article which is associated with my benefit. This is a phenomenal strategy for extending data for us. Appreciation for sharing an article like this.Hire Online web Development in Canada

    ReplyDelete

  76. Professional Cooks Interview Questions are tailored inquiries designed to assess candidates' culinary skills, experience, creativity, and ability to work in fast-paced environments. These questions aim to identify candidates who possess the necessary expertise and passion for delivering high-quality dishes in professional kitchen settings.

    click here

    ReplyDelete