Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

greg1067

macrumors newbie
Original poster
Nov 30, 2009
17
0
Hello to all. I am still very new at the iPhone programming so bear with me. :eek: I am working on an app that uses the pickerView and am having some difficulties with it. I have the "wheels" populated and running correctly in the debugger. I can send the selected items to a string using NSLog, but what I want to do is, based on the users' selection in both components-do somehthing. Basically it is a measurement converter so in column 1 if teaspoon is selected and in column 2 tablespoon is selected, then convert the number of tsp to tbs(this number comes from a textbox that I do not have connected yet). Just trying to figure out how to get the selected index formatted correctly into an if statement. Then I will worry about the code for the conversion.

Here is my code for what is working and what I am trying to do with it. As stated above, I am able to get the selected items to show up in the NSLog, but to use the selected item in an if statement is my problem
Code:
-(IBAction) calc:(id)sender
{
	NSString* themessage = [NSString stringWithFormat:@"This %@ and this %@ are the measurements being converted.",
							[from objectAtIndex:[calcPicker selectedRowInComponent:0]],
							 [to objectAtIndex:[calcPicker selectedRowInComponent:1]]];
	
	//This is where I am trying to pull specific selections from the picker and then will do the conversions based on what is picked
	
	if ([from objectAtIndex: [calcPicker selectedRowInComponent:0]] == 1) {
		
//do some coversion math here.....

}
	
	

	
	NSLog(themessage);
	
}
 
Anyone have an idea or do you not understand what I am trying to do?
 
What is stored in from and to? Are you sure you can compare it to 1 with a simple ==?

I'm not sure if comparing the index number is correct or not. I have dug through the net trying to figure this out.

Here is the code for the 2 arrays, if that is what you are asking.
Code:
	from = [[NSArray alloc] initWithObjects:@"Teaspoon", @"Tablespoon", @"Ounce", @"Cup", @"Pint", @"Quart", @"Gallon", @"Milliliter", @"Liter", nil];
	
	to = [[NSArray alloc] initWithObjects:@"Teaspoon", @"Tablespoon", @"Ounce", @"Cup", @"Pint", @"Quart", @"Gallon", @"Milliliter", @"Liter", nil];
What I am trying to do is, based on the user's selections in the 2 arrays from the picker, to set an if statement. For example:

If teaspoon and tablespoon are selected then convert the number of teaspoons into tablespoons.

An integer will come from a textbox that I do not have connected yet to specify the number of "teaspoons, tablespoons, etc." , just trying to figure out how to extract the selected data from the picker first then I will add the textbox and the connections.
 
Ah, but your if statement is not comparing against the index number; it's comparing against the object at that index.

So how would I go about comparing it to the object? In this case I would assume it would be teaspoon at index:0. I tried this after your last reply, but get a cast exception.

Code:
	if ([from objectAtIndex:[calcPicker selectedRowInComponent:0]] == "Teaspoon") {
		//do some conversion math here....
}

The exception is "comparison of distinct pointer types lacks a cast".

I think I just got it!! I put @"Teaspoon" and it runs the if statement. I just have an NSLog just to see if it will run. Will let you know how things go. Thanks for the help!

Sorry for what might seem simple questions, but I am still very new to this.
 
So how would I go about comparing it to the object?
No need to (unless you need to ensure that what's at that index contains what you think it should contain, like in this case, @"Teaspoon"). If you feel comfortable knowing that index 0 is for teaspoon (remember arrays are zero-indexed), you can just compare your selectedRowInComponent: return value to your hard-coded index.

EDIT:
I think I just got it!! I put @"Teaspoon" and it runs the if statement.
If this is the approach you want to take, you don't want to compare an NSString instance using ==; you'll want to use isEqualToString:
 
If this is the approach you want to take, you don't want to compare an NSString instance using ==; you'll want to use isEqualToString:

I've heard that many times before, but I'm still curious. Whats wrong with comparing NSStrings using ==? I know it won't give the same results as isEqualToString, but what does it actually compare?
 
selectedRowInComponent reports null

I'm attempting to determine the selected index in a UIPicker that has only one 'spinner' in it. It was created in InterfaceBuilder and I connected it to this header definition:
Code:
@interface LibraryPicker : UIViewController <UIPickerViewDataSource, UIPickerViewDelegate> {
	IBOutlet UIPickerView *SQLPicker;
}
@property (nonatomic, retain) IBOutlet UIPickerView *SQLPicker;
@end

Here is the code:
Code:
NSLog(@"*********************");
NSLog(@"picked: %@",[SQLPicker selectedRowInComponent:0]);

however the output on the debug console reports (null), even if I manually change the selected row.
Code:
2010-03-05 14:19:45.432 MiSQL[3460:20b] *********************
2010-03-05 14:19:45.440 MiSQL[3460:20b] picked: (null)

What am I missing?
 
Ok, well first of all, selectedRowInComponent returns an NSInteger, and the %@ symbol is for a string. Try the %i symbol and also converting the return value into an int, looking something like this:
Code:
NSLog(@"%i",[[Picker selectedRowInComponent:0] intValue]);
 
Ok, well first of all, selectedRowInComponent returns an NSInteger, and the %@ symbol is for a string. Try the %i symbol and also converting the return value into an int, looking something like this:
Code:
NSLog(@"%i",[[Picker selectedRowInComponent:0] intValue]);

Yes, thank you for your response, i did eventually figure this out independently. Debugging in Xcode is quite a far cry from Visual Studio or even many of the other open source IDE's. Breakpoints don't really work and the best error's one can get are generally crash reports.

If anyone has figured out a decent procedure for debugging with Xcode or any tips that would be great!
 
Debugging in Xcode is quite a far cry from Visual Studio or even many of the other open source IDE's. Breakpoints don't really work and the best error's one can get are generally crash reports.
I'll chalk it up as your newness to Xcode that you make claims like this. It simply isn't true.

If anyone has figured out a decent procedure for debugging with Xcode or any tips that would be great!
There are books with whole chapters on debugging for the iPhone, including "More iPhone 3 Development" by Mark/LaMarche. Plenty of tips can also be found simply by googling.
 
I'll chalk it up as your newness to Xcode that you make claims like this. It simply isn't true.
Debuggers are pretty standard across the board. Especially if the debugger in question (gdb) isn't an apple creation. I'm pretty sure it's Xcode. I did read somewhere that breakpoints that are blue with golden centers wont stop execution because of some trickery that the Release Pools do with memory management. One person reported that recreating the break point after execution started would resolve the issue, but he was using a different version of Xcode then I (3.1.4) and I have not gotten it to work yet. Any clue why breakpoints wouldn't break at a specific point?
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.