Help with Obj-C functions and Manipulation

Need to pass a text box string to a function and get back a number

Page 1 of 1

2 Replies - 601 Views - Last Post: 11 March 2010 - 09:10 PM

#1 Brandon Smith  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 25-February 10

Help with Obj-C functions and Manipulation

Posted 25 February 2010 - 08:09 AM

I am looking for help with an objective C function for the iphone, I need to pass the value of a text box to a function, this would come as a string I believe. Then I need to associate specific numerical values for each letter. Then I would like to return that number for doing mathematical operations. here is what I have in my Viewcontroller.m
-(NSUInteger)getTheIntOfTheGrades:(NSString *)letterGrade{
	NSUInteger theReturnInt;
	if ([letterGrade isEqualToString:@"A"]) {
		theReturnInt = (int)4;
	} else if([letterGrade isEqualToString:@"B"]){
		theReturnInt = 3;
	}else if ([letterGrade isEqualToString:@"C"]) {
		theReturnInt = 2;
	}else {
		theReturnInt = 1;
	}
	return theReturnInt;
}
-(IBAction)calculate:(id)sender{
	NSLog(grade1.text);
	NSUInteger anInt = [aGPA getTheIntOfTheGrades:grade1.text];
	NSString *gradeInt = [[NSString alloc] initWithFormat:@"%d", anInt];
	GPA.text = gradeInt;
	[gradeInt release];
}



I am not sure if I am doing this all right or not. Any help would be appreciated. Thank you in advance.

Is This A Good Question/Topic? 0
  • +

Replies To: Help with Obj-C functions and Manipulation

#2 garfinkle  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 27
  • View blog
  • Posts: 87
  • Joined: 08-September 09

Re: Help with Obj-C functions and Manipulation

Posted 02 March 2010 - 05:35 PM

The way you are doing it should work,

I would do the following

-(IBAction)calculate:(id)sender{
        GPA.text = [NSString stringWithFormat:@"%d", ([grade1.text characterAtIndex:0] - 64)];
}



the characterAtIndex function returns the ascii of the character at the specified index, as you are only dealing with the first character, it will always be at index 0. I can't double check that the code is perfect as I am on my PC but it should work.

The reason for the -64 is that the ASCII Values are as follows
A = 65
B = 66
C = 67
etc...

-64 makes it run as

A = 1
B = 2
C = 3
etc...

Hope it helps

This post has been edited by garfinkle: 02 March 2010 - 05:36 PM

Was This Post Helpful? 1
  • +
  • -

#3 Brandon Smith  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 25-February 10

Re: Help with Obj-C functions and Manipulation

Posted 11 March 2010 - 09:10 PM

Thank you, I appreciate the help. I wasn't sure what that would return, I have used the -64 in another language to check for A,B,C,etc just didn't know what I could grab it with in object-C
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1