objective c int not declared - objective-c

I am creating a very simple calculator. To save the first user inputed float, i save it as a string in the addition action as to save it when they click on the addition button. Then later I call upon it again to add it to the second user inputed float. However when i call upon it again it gives the error: Use of undefined identifier num1. The same thing happens with the operation integer. Here is the relevant code:
- (IBAction)addition {
NSString *number1 = total.text;
float num1 = [number1 floatValue];
int operation = 1;
total.text = #"";
}
- (IBAction)equal {
NSString *number2 = total.text;
float num2 = [number2 floatValue];
if (operation == 1) {
int num3 = num1 + num2;
NSString *znumber1 = [NSString stringWithFormat:#"%f", num1];
}

You need to declare num1 as an "instance variable" so it's accessible by both functions (usually declared within your #interface block).
The way you're declaring it only allows for the code within that function's scope to access it.
I would reccomend reading up on variable scope before continuing.

num1 is only in the scope of (IBAction)addition so you can't use it in (IBAction)equal.

num1 is not scoped correctly. It is local to addition. You want to put in the interface in your .m file of your class. That will give a private variable for your implementation. If you put in your .h file you will expose to the rest of your program.

Related

How to add 1 to Variable with Button Click?

I want to add 1 to my variable each time the button is clicked, but instead a 10 digit number appears. What am I doing wrong with the code below?
-(IBAction)recordData:(id)sender {
int randomNumber;
randomNumber = randomNumber + 1;
NSString *myRandomNumber = [NSString stringWithFormat:#"%i", randomNumber];
NSString *CompleteData = [[NSString alloc] initWithFormat:DataView.text];
CompleteData = [CompleteData stringByAppendingString: #"\n"];
CompleteData = [CompleteData stringByAppendingString:myRandomNumber];
DataView.text = CompleteData;
}
Make
int randomNumber
either static or declare it as an instance variable.
What you are currently doing is creating a new varialbe each time when recordData is invoked. Plus you do not initialize it. Local variables are not initialized. Instance variables are initialized with 0/nil. As a result your variable has some random content (as its name suggests anyway :). To that random value you add 1.
Your variable is not initialised: int randomNumber = 0 would fix that.
randomNumber's scope is the recordData: method. You could make it a static variable a suggested by holex, or even better, make it a property of your class.
Have a look at Encapsulating Data in Apple's Objecitve-c Documentation to learn more about iVars and properties.

set ivars from NSDictionnary

I'm currently working on a project where the user defines some parameters in a NSDictionnary, that I'm using to setup some objects.
For example, you can ask to create a Sound object with parameters param1=xxx, param2=yyy, gain=3.5 ... Then an Enemi object with parameters speed=10, active=YES, name=zzz ...
{
active = NO;
looping = YES;
soundList = "FINAL_PSS_imoverhere_all";
speed = 100.0;
}
I then instantiate my classes, and would like to set the ivars automatically from this dictionnary.
I've actually wrote some code to check that this parameter exists, but I'm having trouble in actually setting the parameter value, especially when the parameter is non object (float or bool).
Here's what I'm doing so far :
//aKey is the name of the ivar
for (NSString *aKey in [properties allKeys]){
//create the name of the setter function from the key (parameter -> setParameter)
NSString *setterName = [aKey stringByReplacingCharactersInRange:NSMakeRange(0,1) withString:[[aKey substringToIndex:1] uppercaseString]];
setterName = [NSString stringWithFormat:#"set%#:",setterName];
SEL setterSelector = NSSelectorFromString(setterName);
//Check if the parameter exists
if ([pge_object respondsToSelector:setterSelector]){
//TODO : automatically set the parameter
}
else{
[[PSMessagesChecker sharedInstance]logMessage:[NSString stringWithFormat:#"Cannot find %# on %#", aKey, [dict objectForKey:#"type"]] inColor:#"red"];
NSLog(#"Cannot find %# on %#", aKey, [dict objectForKey:#"type"]);
}
}
}
As you can see, I don't know what to do once I've found that the parameter exists on the object. I tried to use "performSelector... withObject..., but my problem is that some of the parameters are non-objects (float or bool).
I also tried to get the class of the parameter, by using the setter, but it didn't help.
Did anyone manage to do something like that?
Jack Lawrence's comment is spot on.
What you are looking for is called Key Value Coding, or just KVC.
This fundamental part of Cocoa lets you get and set any instance variable using its name as a String and a new value.
It will automatically handle coercing Objects to primitive values, so you can use it for int and float properties too.
There is also support for validating values and handling unknown properties.
see the docs
your code, without validation, could be written
for( id eachKey in props ) {
[anOb setValue:props[eachKey] forKey:eachKey];
}
or just
[anOb setValuesForKeysWithDictionary:props];
as Jack said.
For the non-object parameters you have to put them into an object, for example NSNumber or NSValue. You can then add these objects into your dictionary.
For Example:
float f = 0.5;
NSNumber f_obj = [NSNumber numberWithFloat:f];

Pointer that can reference a UITextView or a UITextField

I want to print a form that contains a mixture of UITextFields and UITextViews, doing exactly the same thing with each one regardless of its actual type. To keep the code clean I'd like to assign each item in the views array to the same variable in order to retrieve its printing parameters. I thought I could do this with a variable of type id, but I haven't hit on anything that will compile. The code below doesn't compile but it shows what I want to do. My thanks to anyone who tells me how to do this correctly.
id theField;
//for each field on the page
for (j = offsetToFirstFormField; j < [self.fields count]; j++) {
theField = [self.fields objectAtIndex: j];
printStr = theField.text;
if ([printStr length] > 0) {
theFont = [theField font];
maxSize = CGSizeMake(theField.frame.size.width, theField.frame.size.height);
printStrSize = [printStr sizeWithFont:theFont constrainedToSize:maxSize lineBreakMode:UILineBreakModeClip];
printRect = CGRectMake((theField.frame.origin.x * xScale) + xOffset, (theField.frame.origin.y * yScale) + yOffset, printStrSize.width, printStrSize.height);
[printStr drawInRect:printRect withFont:theFont];
}
}
You can't use dot-syntax with id variables. You have to stick with message-sending syntax. For example, you have to change this:
printStr = theField.text;
to this:
printStr = [theField text];
If that doesn't fix it, edit your question and paste in the actual error messages you're getting.
Encapsulate your printable functionality in a protocol. So you will be using id<Printable> rather than id. Your protocol will have methods like getText getFont. Or you can use category to extend the existing classes.

Objective C Function Question

Hey guys, check this out. I have a function that treats for me a string. No matter what it does, i just want to knwo if is possible to this function return the result for the place that it was executed. I mean, check this:
[self priceFormat:#"1"];
priceLabel.text = price;
-(void) priceFormat:(NSString*)price {
price = #"2";
}
I just want to my function treats the string and return it to the same place that it was executed.
Thanks!
Three ways to do this
Way one, using a pointer
- (void)priceFormat:(NSString **)price {
*price = #"2";
}
Wat two, using an instance variable
What you might want instead is an ivar. In the interface (most often the h file) of your class:
NSString *price;
and in the implementation (the m or mm file):
- (void)priceFormat:(NSString *)price {
price = #"2";
}
I have created an example of this here.
If you want the price to be available to other objects as well (not just self), you might want to create a property for it and synthesize it. Then use self.price = #"2"; instead. More on this here: http://MacDeveloperTips.com/objective-c/objective-c-properties-setters-and-dot-syntax.html
Just make sure you make it a copy property (NSString in use)!
Way three using return
Note, that you can also return directly from a method:
- (NSString *)priceFormat:(NSString *)price {
return #"2";
}
priceLabel.text = [self priceFormat:#"1"];

object_getInstanceVariable works for float, int, bool, but not for double?

I've got object_getInstanceVariable to work as here however it seems to only work for floats, bools and ints not doubles. I do suspect I'm doing something wrong but I've been going in circles with this.
float myFloatValue;
float someFloat = 2.123f;
object_getInstanceVariable(self, "someFloat", (void*)&myFloatValue);
works, and myFloatValue = 2.123
but when I try
double myDoubleValue;
double someDouble = 2.123f;
object_getInstanceVariable(self, "someDouble", (void*)&myDoubleValue);
I get myDoubleValue = 0. If I try to set myDoubleValue before the function eg. double myDoubleValue = 1.2f, the value is unchanged when I read it after the object_getInstanceVariable call. Setting myIntValue to some other value before the getinstancevar function above returns 2 as it should, ie. it has been changed.
then I tried
Ivar tmpIvar = object_getInstanceVariable(self, "someDouble", (void*)&myDoubleValue);
If I do ivar_getName(tmpIvar) I get "someDouble", but myDoubuleValue = 0 still! Then I try ivar_getTypeEncoding(tmpIvar) and I get "d" as it should be.
So to summarize, if typeEncoding = float, it works, if it is a double, the result is not set but it correctly reads the variable and the return value (Ivar) is also correct.
I must be doing something basic wrong that I cant see so I'd appreciate if someone could point it out.
object_getInstanceVariable is a confused little function. It is documented that the last parameter is a void ** parameter—that is, you pass the address of a void * variable and get a pointer to the instance variable—but it is implemented as if it was a void * parameter—that is, you pass the address of the variable that you want to hold a copy of the instance variable. The problem is that the implementation ignores the size of the instance variable and just does a pointer copy. So anything that's the same size as a pointer will work perfectly. If you're running on a 32-bit architecture, only the high 32 bits will be copied. (You should witness the same behavior with a long long instance variable as well.)
The solution is to use the primary API, key-value coding, using -valueForKey:.
The other solution: If you wanted to write a fixed version, say as a category for NSObject, it would look something like this:
#implementation NSObject (InstanceVariableForKey)
- (void *)instanceVariableForKey:(NSString *)aKey {
if (aKey) {
Ivar ivar = object_getInstanceVariable(self, [aKey UTF8String], NULL);
if (ivar) {
return (void *)((char *)self + ivar_getOffset(ivar));
}
}
return NULL;
}
#end
Then your code would look like this:
double myDoubleValue = *(double *)[self instanceVariableForKey:#"someDouble"];
What about using valueForKey:?
NSNumber * value = [self valueForKey:[NSString stringWithUTF8String:ivar_getName(tmpIvar)]];
NSLog(#"Double value: %f", [value doubleValue];
Note: this requires you to have a "someFloat" method. If you want to use setValue:forKey:, you'll also need the "setSomeFloat:" method. This is easily implemented by declaring the ivar as an #property and synthesizing it.