It would be nice and appreciate if you help me out with OS X. This blog awesome and i learn a lot about programming from here. The best thing about this blog is that you doing from beginning to experts level. Love from. I'm primarily a Java developer these days but I'm familiar with writing C code though I haven't written it in many years so this knowledge helped me quite a bit. The syntax for Objective-C was a bit strange at first but I got the hang of it after a bit and I'm continuing to learn.
Building the command line app was much more understandable for me because you are writing all the code by hand. Constructing the GUI app was a rather nebulous task mainly because you rely upon the IDE to handle many things for you. It provides a standard set of widgets for your apps and allows you to do visually design the UI. Interface Builder simplifies the creation of connections from the code to the GUI widgets via dialogs and selections in those dialogs.
Because the actual code to handle these things is hidden behind the IDE, the experience was rather cloudy in my mind. At some point, I plan to dig down into what is actually happening behind the curtain here to solidify it in my mind. I also used the XCode debugger and profiler which were both very nice tools and were both included in the IDE. I forgot to mention that I am using Objective-C 2. This is not only a new release of Objective-C but it's also a new release of XCode with a dramatically improved set of functionality. My prior experience was with XCode 3. Now that I've been through a couple of tutorials with XCode 4.
In fact, Eclipse could stand to learn a few things from XCode. Objective-C 2. They are both very minimal and not very useful overall, but it is invigorating to delve into new territory for a platform that I have known and loved for over 20 years already. I plan to keep at it as time allows and build some useful apps. Perhaps I will even develop some IOS apps. For those who are interested, here are the two tutorials that I followed: Coding in Objective-C 2.
Not only is this a record for myself, but hopefully it will help other folks as well. Posted by Bruce Snyder at PM. Labels: objective c xcode mac os x. Yawar Amin 12 February, Bruce Snyder 12 February, Bruce Snyder 13 February, Yawar Amin 13 February, Anonymous 11 March, Vaibhav Limbani 02 September, Newer Post Older Post Home. In Cocoa, this naming scheme is more than just a convention, it is used later to allow us to automatically read and write properties in all sorts of simple and elegant ways that are awkward or impossible in other programming languages.
Since age is just an integer, couldn't we just refer to it from elsewhere without having to go through all the overhead of calling a method? Yes we could, but we'd be breaking the rule about hiding the implementation. Suppose at a later stage you decided that this object should calculate the age by counting tree rings, instead of simply storing it.
If external code was peeking at the integer, it would stop working when we changed the code within the age method so it counted tree rings. So never, never peek at data members from outside of an object.
Even within the object, we should usually call our own methods to read our data member's values, since again, if later we change the way it works, we can minimise the number of places that need to be changed by making sure that there is only one main place that deals with that value. Note how we declare a method's parameter types. We put the type in parentheses following the colon.
We can also, as here, nominate a variable name for the parameter, though as in C this is optional. However, since the parameter name is built into the method, leaving it out doesn't leave us with an undocumented mystery, as it does in C.
In other words:. For contrast, in C you might have had something like:. What is the second parameter? Unless the programmer used a more descriptive name or added a comment to tell us, we can't tell. The return types of the method are also indicated using the same bracket notation. Here we import Cocoa so that we get the interface for NSView , of which this is a subclass. At the very least, a class interface must somehow make details of its superclass known, though where you are subclassing a Cocoa object, using import is best.
The implementation of an object is everything between implementation and end. The methods are coded just as a C function.
In contrast, most object-oriented systems at the time that it was created used large virtual machine runtimes. Objective C: Functions. Functions and data structures that comprise the Objective-C runtime, which provides support for the dynamic properties of the Objective-C language. October Objective-C: Xcode Programming.
Here we have our unique methods, setText:withSize: and so on, and also a couple of methods that we inherited from our superclass, initWithFrame: and drawRect: These last two don't appear in our interface file, as they are not a unique feature of this class. To override them, we simply redefine the methods in the implementation. It can get a bit tedious to write these all out if you have many properties, but don't be tempted to avoid this, because hiding the implementation is much more important in the long run.
The setText:withSize: method is a little more complex, since we need to retain and release objects. We'll be covering this in detail in the next chapter, so don't worry about it for now. However, this is required most times that you are using objects as properties of other objects, which will happen a lot. This signals to Cocoa that the view content has changed and should be redrawn. Cocoa makes a note of that and will cause our drawRect: method to get called in due course, so anyone who makes use of the setText:withSize: method of our view will see those new settings appear immediately.
The initWithFrame: method also sets these various data members to some default values, so that the object will work even if nobody ever comes along and sets the text, its size, etc. Every object has an init method whose purpose is to establish the object's default properties so that it will function properly right out of the box. Sometimes, objects will need parameters sent to the init method, so a different version of an init method is needed, as the way initWithFrame: was coded. Note that the init method's first job is to call the superclass's initWithFrame: method.
This happens all the way back up the chain of ancestor classes until everyone has initialized what it knows about. So there are two main rules to follow when writing your init method:. The init method must return 'self' as its result, provided that the super's initialization succeeded. If not, it will return nil and so should you. If your own initialization fails, but your super's succeeded, you should return nil also. This indicates to the caller that something went wrong while making your object. More about this later. This means two different things. In the first place, it means that you can have different methods that differ only according to their parameter types, and the correct one will be called according to what you're passing.
The right one is called depending on whether you pass an int or a float. While handy, this can get confusing, especially when automatic type conversion is taking place, as it frequently does between simple scalar values.
Objective-C doesn't support this type of potentially ambiguous polymorphism. Instead, because parameters are an inherent part of a method's name, you can make it much clearer, e. There is no ambiguity, since you have to name which one you mean explicitly. The second kind of polymorphism is dynamic polymorphism. We have already seen this in action in our simple program. It calls our object as if it were an NSView. The method call resolves to our custom object however, and so our code gets called, allowing it to do its thing. All object-oriented languages need to support this kind of polymorphism, so the fact that Objective-C does too is no surprise.
What is different is that Objective-C resolves the method at runtime. For the typical case, this runtime binding makes no difference. It safely allows us to use anonymous objects, and even null objects. It allows us to create a delayed method call called an invocation that can be used later, and it permits things like Undo to be implemented much more easily.
It greatly helps us create a graphical user interface for our code, because commands, actions and so on of various window controls can be made extremely general. Let's suppose we have an object that must respond to a click on a button in a window. Really, the button shouldn't need to care which sort of object it is controlling, only that when it is clicked, it should do something. Let's suppose a method exists called action: , and the button knows that it should call this on an object called 'target' when clicked.
In a traditional language, the target object's type must be known to the button, so that it can determine if a method called action: really exists.
Once that type is indicated to the button, it can only ever deal with objects of that type, or their subclasses. However, in Objective-C, we could hook up totally unrelated objects to the button, and as long as they implemented action: , they would respond to the button. This is a great improvement in flexibility.
An 'anonymous' object has a type of 'id'. Anywhere you see 'id' used as an object type, an anonymous object reference is created. In Objective-C, the value nil has a type of id. This means that nil is an object!