Setter method not being called - iphone

I have this code:
.h
int cellsPerRow;
#property int cellsPerRow;
.m
#synthesize cellsPerRow;
-(void)init {
...
cellsPerRow = 4;
}
-(void)setCellsPerRow:(int)cellsPerRowLocal {
cellsPerRow = cellsPerRowLocal;
....
}
But the setter method isn't being called. Any ideas why?

You are assigning to the variable instead of the property. Instead of:
cellsPerRow = 4;
You need to do either:
self.cellsPerRow = 4;
or
[self setCellsPerRow:4];
The former is transformed by the compiler into the later, so they are equivalent.

The property and synthesize create the setter and getter methods for you. In essence what you are doing is actually override the methods that were created for you in the first place.
Don't do that. Just use dot notation, like so, self.cellsPerRow = cellsPerRowLocal. This will set your cellsPerRow ivar to whatever you want it to be in cellsPerRowLocal.
In you header file:
int cellsPerRow;
#property (nonatomic, retain) int cellsPerRow;
In your implementation file:
#synthesize cellsPerRow;
- (void) inThisMethodYouSetcellsPerRow {
cellsPerRowLocal = ... ;
self.cellsPerRow = cellsPerRowLocal;
}

Related

Variables in a class

#interface characterclass : CCSprite
{
bool alive;
int speed;
int jumpamount = 10; <--- error
}
#property bool alive;
#property int speed;
#property int jumpamount;
#end
how do i do this like in my code, I want to have a variable in my class that equals 10.
You need to assign these values in the initializer of your class's instances. Create an instance method called - (id)init:
- (id)init{
self=[super init];
if (self) {
jumpamount=10;
}
return self;
}
Note that you no longer need to declare your ivars like that. #property will create an ivar for you.

How do I increment an int value in an NSMutableArray?

I got:
#interface ViewController : UIViewController
{
int index_counter;
//NSMutableArray *logins;
}
#property (weak, nonatomic) IBOutlet UITextField *count;
#property (strong, nonatomic) NSMutableArray *logins;
- (IBAction)next_button:(id)sender;
#end
Which is an array that holds objects:
#interface THEOBJECT : NSObject
{
NSString *uname;
int counter;
}
-(void) SetUser: (NSString *) username;
-(void) SetCount: (int) value;
-(void) print;
#property (nonatomic, retain) NSString *uname;
#property (nonatomic,readwrite) int counter; //not sure if this is correct
#end
#implementation SiteValue
#synthesize uname;
#synthesize counter;
-(void) SetCount:(int) value
{
counter=counter+1;
}
#end
and my method should increment the count value in the object THEOBJECT in each index of the array:
- (IBAction)next_button:(id)sender
{
index_counter=index_counter-1;
if (index_counter<0)
{
index_counter=0;
}
username.text=[[logins objectAtIndex:index_counter] uname];
[[logins objectAtIndex:index_counter] counter]=[[logins objectAtIndex:index_counter] counter]+1; //ERROR HERE.
}
Where I wrote "ERROR HERE", it should increment the count value every time I push the next button and store +1 in the array. But it's giving me a readonly error. The exact error is "assigning to 'readonly' return result of an Objective-C message not allowed". I think the best thing to do is to call the setcount: method but it's not letting me call it since it's two different interfaces. Any ideas?
Easiest way to do this is to access the property counter, but to do that you need to cast the result of objectAtIndex: as it returns an id and while Objective-C allows you to call any method on an id instance you cannot call any property:
((THEOBJECT *)[logins objectAtIndex:index_counter]).counter++;
counter in this line is an accessor method:
[[logins objectAtIndex:index_counter] counter]
It returns a value to you so it's not something you can set (It's like saying 50 = 100 -- you cannot set a value like that).
If you want to set the variable, you need to use the setCounter method:
[[logins objectAtIndex:index_counter] setCounter:[[logins objectAtIndex:index_counter] counter]+1];
In the line:
[[logins objectAtIndex:index_counter] counter]=[[logins objectAtIndex:index_counter] counter]+1; //ERROR HERE.
You should be using the setter and not the getter of the property counteron the left side of the assignment. So you should change that line into:
[[logins objectAtIndex:index_counter] setCounter:[[logins objectAtIndex:index_counter] counter]+1];
To make this clearer, you can split that line into two lines:
int currentValue = [[logins objectAtIndex:index_counter] counter];
[[logins objectAtIndex:index_counter] setCounter:currentValue+1];
You can also use the dot notation and write that as:
THEOBJECT *myObject = [logins objectAtIndex:index_counter];
int currentValue = myObject.counter;
myObject.counter = currentValue + 1;
Or:
THEOBJECT *myObject = [logins objectAtIndex:index_counter];
myObject.counter = myObject.counter + 1;
Or:
THEOBJECT *myObject = [logins objectAtIndex:index_counter];
myObject.counter++;

Type of property 'shapeMatched' does not match type of accessor 'setShapeMatched:'

I'm developing an iPhone 4 application and I having problems with the class:
#interface Pattern : NSObject {
int maxNumShapes;
NSMutableArray* shapes;
NSMutableArray* shapeMatched;
CGSize bounds;
}
#property (nonatomic, retain, readonly) NSMutableArray* shapes;
#property (nonatomic, retain, readonly) NSMutableArray* shapeMatched;
...
- (void) setShapeMatched:(ShapeType)type;
#end
And its implementation:
- (void) setShapeMatched:(ShapeType)type {
int index = 0;
if (shapes != nil)
{
for(Object2D* obj in shapes)
{
if (obj.figure == type)
{
[shapeMatched replaceObjectAtIndex:index
withObject:[NSNumber numberWithBool:YES]];
obj.withFillColor = YES;
break;
}
else
index++;
}
}
}
I get the following warning:
Type of property 'shapeMatched' does not match type of accessor 'setShapeMatched:'
How can I fix this warning?
You method - (void)setShapeMatched:(ShapeType)type is probably intended to change something in the shapeMatched array, however, its name overwrites the setter of your property for that very same array.
It's unclear, but if you really want to overwrite the setter, the argument must be of type NSMutableArray * since that's the type of your property. Otherwise, if you method just do something else, rename it to avoid the collision. setShapeMatchedType:(ShapeType)t might be a good option.
You need to rename your custom setShapeMatched: to something else (like setMyShapeMatched). setShapeMatched: is already in use as the setter for your declared property.

Set object in another class?

I have one class, "DogViewController", where I declare a property and synthesize it:
#property (nonatomic, assign) NSInteger myInt;
#synthesize myInt;
I have another class, "CatViewController", in which I want to set the myInt variable, so this is what I did:
#property (nonatomic, retain) DogViewController *myDogViewController;
#synthesize myDogViewController;
myDogViewController.myInt = 5;
I then want to access myInt from the DogViewController class, which I do by:
someVar = self.myInt;
someVar should be == 5 after that command.
Is there a reason why that would not work?
You must instantiate DogViewController first. So in CatViewController:
DogViewController *dogViewController = [[DogViewController alloc] initWithNibName#"DogViewController" bundle:nil];
dogViewController.myInt = 5;
// If you are using a navigation controller push DogViewController like this:
[self.navigationController pushViewController:dogViewController animated:YES];
[dogViewController release];
If you are not using a navigation controller just display that view in whatever way you want
Try with not using retain with basic data type use assign instead .
#property (nonatomic, assign) NSInteger myInt;
I think you will not get value 5. Because it will be reset.
You can do it like below way.
inside your "CatViewController", create one method like "setParent"
take in variable in .h file:
id parent;
function in .m file:
-(void)setParent:(id)pidParent{
parent = pidParent;
}
import "DogViewController.h" in CatViewController.m file.
When you push to the DogViewController then also called it's setParent method.
Like
[objDogViewController setParent:self];
then you can set value by using "parent" variable.
Cheers.

How to override #synthesized getters?

how to override a property synthesized getter?
Just implement the method manually, for example:
- (BOOL)myBoolProperty
{
// do something else
...
return myBoolProperty;
}
The compiler will then not generate a getter method.
Inside of your property definition you can specify getter and setter methods as follows:
#property (nonatomic, retain, getter = getterMethodName, setter = setterMethodName) NSString *someString;
You can specify the getter only, the setter only, or both.
Just implement your own getter and the compiler will not generate one. The same goes for setter.
For example:
#property float value;
is equivalent to:
- (float)value;
- (void)setValue:(float)newValue;
I just want to add, I was not able to override BOOL property with getter/setter, until I add this :
#synthesize myBoolProperty = _myBoolProperty;
so the complete code is :
in header file :
#property BOOL myBoolProperty;
in implementation file :
#synthesize myBoolProperty = _myBoolProperty;
-(void)setMyBoolProperty:(BOOL) myBoolPropertyNewValue
{
_myBoolProperty = myBoolPropertyNewValue;
}
-(BOOL) myBoolProperty
{
return _myBoolProperty;
}