I am a newbie with Objective C programming.
My first steps to learn Objective C is to read the PDF "Programming with Objective C" from the Apple Developers site.
At the end of every chapter there are exercises to make. Chapter 1 has 4 questions. The first 3 questions are not so dificult to make. But the 4th question I can't figure it out what the anwser is.
The question is:
Add a declaration for a class factory method, called "Person". Don't worry about implementation this method until the next chapter.
I hope that anybody can and will help me.
Thank you
Carlos Wiesemann
Take another look at the section just before the exercises, Objective-C Classes Are also Objects. That section talks about how classes themselves are objects that can have methods separate from the methods that an instance of the class has. When the question asks for a "class factory" method, it means a class method that creates a new instance of that class, just like the string methods in the linked section.
Class method declarations look just like instance methods, except they are prefixed with a plus sign instead of a minus sign. For example,suppose you have a class named Car
let’s add the following class-level method to Car.h:
+ (void)setDefaultModel:(NSString *)aModel;
Related
can i have same method name with same params and params type in a single class? how about in a different class with same name?
In response to the first part is the ans overriding? what about the second part of the questions "how about in a different class with same name?"
I think there a few examples in which the answer to both your questions is yes, but as far as my knowledge in Java and Objective C is concerned, you cannot have two methods named exactly the same with same parameter names and types in the same class. I believe that you can in two different classes, but you generally shouldn't, just to keep your code simple and easily understandable, but you can do it all the same.
what is the difference between inheritance and category in objective-c
Both are used for the subclass! So what is difference between them
While Category is a nice way to add functionality to the base class, people like me who come from other object oriented technology such as Flash, will find a little difficult to understand as to how this thing relates to the inheritance chain. The same question came up to my mind and I did a quick research on the topic.
The final thing is Category does the same thing as it tells about itself. It adds functionality to the base class. If you remember this, then there would be no confusion at all.
Well, for that to understand, lets take an example. Suppose there is a Class A and Class B is a subclass of Class A. In the application Class B is used in a lot of places. Now, there is a need to add some more functionality to Class A, so a new category is written as "A+newRole". Once this category is written, the new functionality is added to the base class and in this case, Class A. That means, all those classes which are child classes of Class A such as Class B, automatically gets the functionality. Thats freaking cool. One can straight away go ahead and call the new methods added in the Category from the child classes. The only thing necessary here is to import the Category file to the appropriate place.
A category adds extra functionality to a class without generating a new class at all, you just extend it but it does not have polimorphism implied or anyting like it.
Inheritance on the other hand, generates a new class on its own right in which you can add new instance variables and override behavior from the parent class by polimorphism.
So lets say I made a variable in one class called 'number' and gave it a value of 3.
and I made a variable in another class called 'number2'
what functions could I use to pass the value of 'number' to 'number2' in that other class?
Thanks.
To get started with Objective-C, including learning how to pass values as method arguments, have a look at Apple's Learning Objective-C: A Primer, Object-Oriented Programming with Objective-C, and Cocoa Fundamentals Guide.
Look up accessor methods on google.
Learning basic Objective-C and have a few beginner questions.
How would I define and implement a method which takes two (or three) arguments within my class?
I find the syntax to pass multiple arguments into a method really confusing. I would really appreciate any help. thanks.
Apple's "The Objective-C Programming Language" document provides a nice overview of Object Messaging including an explanation of the syntax.
Here's an example of a simple 2-argument method implementation:
-(int)myMethodThatMultipiesThisNumber:(int)x byThisOne:(int)y
{
return x * y;
}
You would invoke it like:
int z = [myObject myMethodThatMultipliesThisNumber:6 byThisOne:9];
Is that what you're looking for?
Edit: Based on your comment below, it seems like you're missing a fundamental feature of Objective-C messaging - that the method name is interleaved with the arguments. Check out this page from The Objective-C Programming Language for all the detail you need.
Whether or not you are interested in iPhone programming, I would watch the first three classes of Paul Hegarty's CS193P class from Stanford.
Those first three classes have very little iPhone specific stuff, instead, classes 1 and three go over the features and syntax of Objective C, and class 2 goes over basic use of Xcode (which, if you want to do Objective C work, is likely the IDE you will be using). Other than the fact that it goes VERY fast (which may actually be what you are looking for) you would be hard pressed to find a better "quick overview" of Objective C.
I was wondering if anyone would be kind enough to define what a class, instance and method is in Objective-C or point me in the right direction or a good Objective-C learning resource.
Thanks!
For understanding the basic concepts, read the Wikipedia article on OOP.
Once you've understood that, the next step is to read up on Objective-C. For example, there's a nice document from Apple which you should read, and then there's the Objective-C Beginner's Guide. Apart from that, just search on Amazon and pick a book that has good rating/comments and that also covers topics you're interested in, like developing for the iPhone. Most iPhone development books start by giving a (short) introduction to Objective-C.
Once you have gotten your hands dirty with writing some Objective-C code, I recommend reading Cocoa Design Patterns. Do not read it as your first book, but do read it some day ! It explains why the Apple APIs (Cocoa) are the way they are, it explains the concepts and patterns you see in Cocoa. It's not a step-by-step guide but gives an understanding on how things work together.
As a start I'd use apple's Objective-c Primer: http://developer.apple.com/library/ios/#referencelibrary/GettingStarted/Learning_Objective-C_A_Primer/
I'd also adise you to look for tutorials about "Object Oriented Programing", to learn its basic concepts.
But to answer your question:
A class is like an Object type. You write the definition of your classes in your source code. In objective-c this happens in the #interface and #implementation parts. An example for a class would be "cars".
A class has class variables: usually of a simple type, like int or bool, or a pointer to another class. A class also can have methods, which are like methods, or functions in sequential programming (c, or similar). A class method for "car" could be "change tires", an class variable could be "license plate".
These are merely definitions of how your class looks like and how it behaves. Like the fact, that integer is a number. which number any int in your program is, would be the instacne. Or to use the "car"-example: "car" is the class and the silver-coloured, mercedes with the licence plate number 'xy-123' is an instance of the class.
Have fun coding!
Class = Car
Instance = Tire
Method = turnLeft
#interface Car: NSObject {
id tire;
}
#property id tire;
- (void)turnLeft;
#end