I am an OLD procedural language programmer, trying to learn Obj C. And after pulling out all my hair realize I am missing a fundamental understanding
When I alloc an instance of an object ( myObj), give it some data via methods, and the call a method/send a message to that instance, can the implementation routine assume that all the data in that instance is available?
i.e [myObj doSomeStuff]
can the implementation of doSomeStuff assume that all the instance data of myObj is available or do I need to send in as arguments of all the parts and pieces of myObj that I need?
Ditto on what Luis and rmaddy said. Some of this is best illustrated with an example. Lets consider a Circle class object:
Here is the .h file:
// Circle.h
#import <Foundation/Foundation.h>
#interface Circle : NSObject
{
double radius;
double pi;
}
#property double radius, pi;
-(double) area;
-(double) diameter;
-(double) circumference;
#end
And then the implementation file (note that I have defined instance methods as Luis points out):
// Circle.m
#import "Circle.h"
#implementation Circle
#synthesize radius, pi;
// Initialize with default radius:
- (instancetype)init {
self = [super init];
if (self) {
pi = 3.14159;
NSLog(#"Circle created.");
}
return self;
}
-(double) area {
return pi*radius*radius;
}
-(double) diameter {
return 2*radius;
}
-(double) circumference {
return 2*pi*radius;
}
#end
Now create a circle object in main and send messages to return several relevant quantities:
// main.m
#import <Foundation/Foundation.h>
#import "Circle.h"
int main(int argc, const char * argv[])
{
#autoreleasepool {
Circle *aCircle = [[Circle alloc] init];
[aCircle setRadius:2];
NSLog(#"Area = %f",[aCircle area]);
NSLog(#"Circumference = %f",[aCircle circumference]);
NSLog(#"Diameter = %f",[aCircle diameter]);
NSLog(#"Check pi = %f",[aCircle pi]);
}
return 0;
}
You can see from this example the value of pi is set when the object is created and then is stored as part of the object, available for other calculations.
Note: you wouldn't want to define pi this way in practice, this is just a simple example to illustrate the point.
Note also that I could have set other internal values of the object after it was created, then this data is also available from that point forward, subject to the qualifications pointed out in the post.
Does this make better sense? You should run this code and experiment with the idea until you feel more comfortable with it.
EDIT: At Andrew Madsen's suggestion, updated the accessor methods for area, diameter, and circumference; these should not be prefixed with 'get'
YES, if the method is an instance method (those who starts with a "-" not "+").
Related
I'm having trouble understanding a finer point of Writing and calling Classes. It's probably
easlier to grasp in Swift but it bothers me to start that study without
getting it right in obj_c first. Currently I do everything in the
ViewControllers with iVars and Globals. With two apps 18 months in the App
store its overdue to put them right.
I've formed a notion that properties are the Object's State, and any methods
within determine the Objects Behaviour but so far no-one is able to tell me.
here be a typical Class header:
#interface Math : NSObject
#property (nonatomic, assign) int a;
#property (nonatomic, assign) int b;
#property (nonatomic, assign) int c;
-(int)mathemagic:(int)a adding:(int)b;
#end
and the corresponding Class implementation:
#implementation Math
#synthesize a = _a;
#synthesize b = _b;
#synthesize c = _c;
- (instancetype)init {
self = [super init];
if (self) {
_a = 0;
_b = 0;
_c = 0;
}
return self;
}
-(int)mathemagic:(int)a adding:(int)b {
_c = (a + b);
return _c;
}
#end
and finally in the appropriate places in my ViewController
#import "Math"
- (void)viewDidLoad {
[super viewDidLoad];
Math *theMath = [Math alloc]; // makes no difference if I init[]
theMath.a = 10;
theMath.b = 20;
NSLog (#" answer is %i",theMath.c);
// but still outputs to:
// answer is 0
}
Now I know can make an iVar and do it this way,
int d = [self.theMath mathemagic:theMath.a adding:theMath.b];
NSLog (#" sum: %i",d);
But i shouldn't have to. Stanford CS193P seems to always make the Class a property of the ViewController, but then everything is again expressed as self.theMath.whatever and the Data Model is no longer encapsulated away from the VC ? Maybe Stanford leaves advanced distractions to Java graduates till later.
Well for this person who's read David Flanagan's "Java in A Nutshell" ,
and Niemeyer-Knudsen's "Learning Java", It's later Now.
I shouldn't have to touch theMath.c, just by assigning values to [ theMath.a ] and [ theMath.b ] should be enough.
Where am I wrong?
I think that is because you are setting a and b = 0 in alloc init . and you are not calling [self mathemagic:a adding:b] anywhere.
I think im Math.m you should change -(instancetype)init to
- (instancetype)initWith:(int)a andb:(int)b {
self = [super init];
if (self) {
_c = [self mathemagic:a adding:b];
}
return self;
}
and in viewDidLoad use
Math *theMath = [[Math alloc]initWith:10 andb:20];
Hope this helps :)
I think you have a misconception of how Objective-C classes work.
First of all, it takes two steps to create an object in Objective-C. You must both:
Dynamically allocate memory for the new object
Initialize the newly allocated memory to appropriate values
So your Math instance initialization should look like this:
Math *theMath = [[Math alloc] init];
Just calling alloc zeroes out all instance variables of the object. Although in your case it makes no difference using [Math alloc] or [[Math alloc] init], it's not good programming style.
Second, if by "automatically display behaviour" you mean logging the result of mathemagic:adding: method, then you should pass it as an argument to NSLog function instead of theMath.c
NSLog(#" should show the sum being %i", [theMath mathemagic:theMath.a adding:theMath.b]);
I am learning Objective-C inheritance and my program is getting lost in a recursive loop and won't come out. It gets hung up when calling a getter function.
I am using XCode version: Version 6.2 (6C101)
My program is given below
Vehicle.h
#ifndef exercise_2_Vehicle_h
#define exercise_2_Vehicle_h
#import <Foundation/Foundation.h>
#interface Vehicle : NSObject
#property float speed;
-(void) start;
-(void) stop;
-(void) park;
#end
#endif
Vehicle.m
#import "Vehicle.h"
#implementation Vehicle
-(void) setSpeed:(float)speed {
self.speed = speed;
}
-(float) speed {
return self.speed;
}
-(void) start {
NSLog(#"Starting the vehicle");
}
-(void) stop {
NSLog(#"Stopping the vehicle");
}
-(void) park {
NSLog(#"Parking the vehicle");
}
#end
Car.h
#ifndef exercise_2_Car_h
#define exercise_2_Car_h
#import "Vehicle.h"
#interface Car : Vehicle
#property (nonatomic) NSString* make;
-(Car*) initMake: (NSString*) make;
-(NSString*) make;
#end
#endif
Car.m
#import "Car.h"
#implementation Car
-(Car*) initMake:(NSString *)make {
self = [super init];
if (self) {
self.make = make;
}
return self;
}
-(NSString*) make {
return self.make;
}
#end
main.m
#import <Foundation/Foundation.h>
#import "Car.h"
#import "Vehicle.h"
int main(int argc, const char * argv[]) {
#autoreleasepool {
// insert code here...
Car* car = [[[Car alloc] init] initMake: #"Camry"];
//[car setSpeed:45];
NSLog(#"The model initialized is ");
[car make];
// [car speed];
}
return 0;
}
The issue you have is caused by creating the property for speed:
#property float speed;
and overriding setSpeed: method.
When you create #property compiler adds two methods for you, in your example setSpeed and speed.
This command:
self.speed = speed;
is equal to:
[self setSpeed: speed];
and inside setSpeed you have this command again which cause the loop. In your example you can remove both methods (setSpeed and speed) because compiler will add it for you. If you need it because you want to do some customisation you should use _speed instead self.speed.
_speed is backed variable added by compiler when using #property.
Change your method to:
-(void) setSpeed:(float)speed {
_speed = speed;
}
to remove the infinite loop.
In the
- (NSString*)make;
use
return _make
instead. The same with the speed.
If you return "self.x" in a getter method, then it's going to try and call the method again because you're requesting it on self. XCode will automatically convert the properties into variables that can be accessed with an '_' character, so you don't need to do any extra work.
You could also ignore our advice and remove both the "speed" and "make" getter methods you have made, because XCode automagically creates them for you.
I hope that makes sense. I'm trying to define radius, as something the user enters. How would I do this?
#import "circle.h"
#implementation circle
-(double) radius {
double radius =
}
-(double) pi {
double pi = 3.14159;
return pi;
}
#synthesize setRadius, getRadius;
-(double) getArea {
double area = pi * radius * radius;
return area;
}
-(double) getDiameter {
double diameter = 2 * radius;
return diameter;
}
-(double) getCircumcerence {
double circumference = 2 * pi * radius;
return circumference;
}
#end
Here is a sentence of nothing so it will let me post my question for not being mostly code.
Add this as a method in your circle class:
-(void)setRadius:(double)input {
radius = input;
}
Assuming you're not using #properties, which I highly recommend (and provided an example of in answer to your other question), this is how you properly create a setter in Objective-C. radius needs to be declared as a variable of the class, outside this method, so that it can be called by your other methods such as getCircumference etc.
In order to create a private instance variable, you need to tack this on to the end of your #implementation line:
#implementation circle {
double radius;
}
But again, doing this assumes you haven't gone the route of #properties, which again, I highly recommend.
To make use of this from main:
circle *myCircle = [[circle alloc] init];
[circle setRadius:1.0]; //the radius is now 1.0
double newRadius = 27.0;
[circle setRadius:newRadius]; //now the radius is 27.0
Updating my answer because I misread the original question. Let me state what I assume you are trying to do:
Set the value of a property called 'radius' on an Objective C class.
Interface (header) should have something like:
#property (nonatomic, assign) double radius;
You are synthesizing the property and I will assume you want/need to do that. But your syntax is wrong. It should be:
#synthesize radius;
The instance variable, by default will be: _radius
So, your setter would be:
- (void)setRadius:(double)radius
{
_radius = radius;
}
A getter might look like:
-(double)radius
{
return _radius;
}
You can set a different backing variable in the #synthesize statement if you like, by default you will get _radius
Keep in mind that unless you have some custom logic you need in your setter and getter that you don't need to define those methods at all. Nor, by default, do you even need to synthesize the property. The compiler will take care of all of that.
Further info:
http://useyourloaf.com/blog/2012/08/01/property-synthesis-with-xcode-4-dot-4.html
I have been struggling along with an online objective-c class for a few weeks now. I'm feeling very stupid..
My latest assignment is to write a program that demonstrates a class named Circle by asking the user for the circle's radius, creating a Circle object, and then reporting the circle's area, diameter, and circumference.
We should have the following member variables:
radius: a double
pi: a double initialized to 3.14159
and the following member functions:
setRadius - a mutator function for the radius variable
getRadius - an accessor function for the radius variable
getArea - returns the area of the circle, which is calculated as: area = pi * radius * radius
getDiameter - returns the diameter of the circle, which is calculated as: diameter = radius * 2
getCircumference - returns the circumference of the circle, which is calculated as: circumference = 2 * pi * radius
The member variables of the class should be set as private.
Here is my program so far:
Main:
int main(int argc, const char * argv[])
{
#autoreleasepool {
int radius;
NSLog(#"Enter the circles radius:");
scanf ("%d", &radius);
}
return 0;
}
Interface:
#import <Foundation/Foundation.h>
//circle class
#interface circle : NSObject
{ #private
-(double) radius;
-(double) pi;
}
#property int setRadius, getRadius;
-(double) getArea;
-(double) getDiameter;
-(double) getCircumcerence;
#end
Implementation:
#import "circle.h"
#implementation circle
#synthesize setRadius, getRadius;
-(double) pi
{
pi = 3.14159;
}
-(double) getArea
{
pi * radius * radius;
}
-(double) getDiameter
{
radius * 2;
}
-(double) getCircumcerence
{
2 * pi * radius;
}
#end
As you can see, I haven't gotten very far. I am confused as how to simply utilize my methods in my main, and am sure I have already made mistakes.
Any advice is appreciated! I really need help, and am short on time.
Also, this may be far-fetched but if anyone could maybe skype with me and help me through it?
Thanks!
As a starting point, you should set up your .h to something more like this:
#interface Circle : NSObject
#property double radius;
#property (readonly) double area;
#property (readonly) double diameter;
#property (readonly) double circumference;
#property (readonly) double pi;
-(id)initWithRadius:(double)r;
+(instancetype)circleWithRadius:(double)r;
#end
This will set up a setter and getter for radius as well as getters for area, diameter, and circumference. It also sets up an init and factory method for your circle which takes a double for the radius.
I will come back and edit in some modifications you need to make to your .m as well as your main file in order to make this work. As a note, at a minimum we'll override the getters for the 3 readonly properties. This will prevent the compiler from creating ivars (instance variables) for these properties (because we can just calculate and return the number we calculation when we call it).
In your .m:
#import Circle.h
#implementation Circle
-(id)initWithRadius:(double)r
{
self = [super init];
if(self) {
self.radius = r;
}
return self;
}
+(instancetype)circleWithRadius:(double)r
{
return [[Circle alloc] initWithRadius:r];
}
-(void)setRadius:(double)r //This method is automatically created by #property
{ //include any verification logic (make sure r>0 etc), then...
self.radius = r;
}
//we don't really need to override the radius getter
-(double)pi
{
return 3.14159; //or however much accuracy you want
}
-(double)area
{
return (self.pi * self.radius * self.radius);
}
-(double)diameter
{
return (2.0 * self.radius);
}
-(double)circumference
{
return (self.diameter * self.pi);
}
In main, you use this Circle class in just the same way you use any other object in Objective-C (think about NSString, NSArray, etc).
int main(int argc, const char * argv[])
{
#autoreleasepool {
double radius;
NSLog(#"Enter the circles radius:");
scanf ("%lf", &radius);
Circle *myCircle = [Circle circleWithRadius:radius]; //the factory method we set up
NSLog(#"myCircle radius: %lf", myCircle.radius);
NSLog(#"myCircle area: %lf", myCircle.area);
NSLog(#"myCircle diameter: %lf", myCircle.diameter);
NSLog(#"myCircle circumference: %lf", myCircle.circumference);
}
return 0;
}
There are of course many ways to set this up. I can remember being confused when starting out, below is an alternative example to give you something else to look at.
It is not intended to be fancy but just bare-bones so that you can see a minimal setup of the class with an initializer.
Note that the only value initialized is the const pi, of course, the radius can be initialized there as well, as nhgrif's example shows quite nicely.
Hope this helps!
// Circle.h
#import <Foundation/Foundation.h>
#interface Circle : NSObject
{
double radius;
double pi;
}
#property double radius, pi;
-(double) getArea;
-(double) getDiameter;
-(double) getCircumference;
#end
And then the implementation:
// Circle.m
#import "Circle.h"
#implementation Circle
#synthesize radius, pi;
// Initialize with const pi:
- (id)init {
self = [super init];
if (self) {
pi = 3.14159;
NSLog(#"Circle created.");
}
return self;
}
-(double) getArea {
return pi*radius*radius;
}
-(double) getDiameter {
return 2*radius;
}
-(double) getCircumference {
return 2*pi*radius;
}
#end
And then for main:
// main.m
#import <Foundation/Foundation.h>
#import "Circle.h"
int main(int argc, const char * argv[])
{
#autoreleasepool {
Circle *aCircle = [[Circle alloc] init];
// Use an arbitrary value:
[aCircle setRadius:2];
NSLog(#"Area = %f",[aCircle getArea]);
NSLog(#"Circumference = %f",[aCircle getCircumference]);
NSLog(#"Diameter = %f",[aCircle getDiameter]);
NSLog(#"Check pi = %f",[aCircle pi]);
}
return 0;
}
Sorry I couldn't be more descriptive in the title. I know C++ and C#, and a bit of Java, and I am doing some Objective C tutorials and can't explain what is going on here.
I will show you an example:
Here is the header/interface of the rectangle class:
#import <Foundation/Foundation.h>
#class XYPoint;
#interface Rectangle : NSObject
#property int width, height;
-(int) area;
-(int) perimeter;
-(void) setW:(int)w andH:(int)h;
-(XYPoint *) origin;
-(void) setOrigin:(XYPoint*)pt;
#end
Here is the implementation:
#import "Rectangle.h"
#implementation Rectangle{
XYPoint *origin;
}
#synthesize width, height;
-(void) setW:(int)w andH:(int)h
{
width = w;
height = h;
}
-(int) area{
return width*height;
}
-(int) perimeter{
return (width+height)*2;
}
-(XYPoint *) origin{
return origin;
}
-(void) setOrigin:(XYPoint*)pt{
origin = pt;
}
#end
And here is my main program:
#import <Foundation/Foundation.h>
#import "Rectangle.h"
#import "XYPoint.h"
int main(int argc, const char * argv[])
{
#autoreleasepool {
Rectangle *r = [[Rectangle alloc]init];
XYPoint *p = [[XYPoint alloc]init];
[p setX:100 andY:300];
[r setW:6 andH:8];
r.origin = p;
NSLog(#"Width and height is %i %i", r.width, r.height);
NSLog(#"Origin is at %i,%i", r.origin.x, r.origin.y);
NSLog(#"Arear and perimeter are %i and %i", [r area], [r perimeter]);
}
return 0;
}
The line r.origin = p; confuses me. Exactly which origin is being referred to here? The getter method as defined in the interface, or the actual member variable which is declared in the implementation?
Note origin is not a property.
To be honest I expected an error. The main program should only see the getter method, but it doesn't seem like a getter method that returns something could be on the left hand side of an assignment operator. Or is the system somehow mapping that syntax to a call to setOrigin?
I had expected the only possible way to set the origin would be something like [r setOrigin: p]
I can see how something like r.width = 5 would work, as width as declared in the header as being a property, but I cannot see how r.origin = p works.
Can someone explain what is going on here?
Thanks.
Your guess is right - that syntax, called dot notation, is translated into a call to [r setOrigin:p] by the compiler. The general idea is that just like most other languages, you have getters and setters for object properties; in Objective-C, these are usually written as:
- (type)variableName;
- (void)setVariableName:(type)aVariable;
So long as your own code conforms to these conventions, you can use dot notation to both get and set variables on your classes, and trust that your methods are called.
There's obviously a lot more going on behind the scenes, so I hope this brief explanation is enough for you for now. If you're interested, though, you can look further into:
Synthesizing properties and what actually happens - methods of the form shown above are generated by the compiler, along with instance variables for actual data storage
Key-value coding, which takes advantage of these principles and conventions
Special cases for some types (for example, BOOL variables' getters are usually written as -isVariableName rather than -variableName), and how you can manage them with #property attributes
Yes, property accessors invoked by the dot notation are automagically traansformed to method calls by the compiler; thus
variable = object.property;
is equivalent to
variable = [object property];
and
object.property = variable;
is equivalent to
[object setProperty:variable];