UIPickerView starting value - objective-c

Here is where i specify how many rows uipicker should have
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
NSUInteger numRows = 15;
return numRows;
}
With that implementation pickers gives option for choose from 0 to 14. What to do so the picker to show the values from 2 to 14 (not to show 0, 1 and 2) ?
//EDIT
self.totalPlayers = [NSArray arrayWithObjects:
[NSNumber numberWithInt:2],
[NSNumber numberWithInt:3],
[NSNumber numberWithInt:4],
[NSNumber numberWithInt:5],
[NSNumber numberWithInt:6],
[NSNumber numberWithInt:7],
[NSNumber numberWithInt:8],
[NSNumber numberWithInt:9],
[NSNumber numberWithInt:10],
[NSNumber numberWithInt:11],
[NSNumber numberWithInt:12],
[NSNumber numberWithInt:13],
[NSNumber numberWithInt:14],
[NSNumber numberWithInt:15],
nil];

The ideal way to do this would be to use an Array.
instead of returning hardcoded length of 15, return array count
Make changes in the array to leave out 0,1,2 or any particular row for that matter.

Related

Convert NSarray to twodimensional array

I have a an one dimensional array which contains a vary numbers of object (depending on the userinput)
The NSArray is called homePlayersArray. This could example contain 2, 3, 5, 6, 4
The thing is i want to convert this to a two dimensional array where example.
{2,0}, {3,}, {5,0}, {6,0},{4,0}
the first value in the object will me by NSarray (called homepPlayersArray) and the second value will be 0.
What is the best way to obtain this?
//Your original array
NSArray *homePlayersArray = [[NSArray alloc] initWithObjects:
[NSNumber numberWithInt:2],
[NSNumber numberWithInt:3],
[NSNumber numberWithInt:5],
[NSNumber numberWithInt:6],
[NSNumber numberWithInt:4],nil];
//For your 2D array
NSMutableArray *secondArray = [[NSMutableArray alloc] initWithCapacity:[homePlayersArray count]];
//populate as required
for(int i=0;i<[homePlayersArray count];i++){
NSArray *tempArray = [[NSArray alloc] initWithObjects:[homePlayersArray objectAtIndex:i],[NSNumber numberWithInt:0], nil];
[secondArray addObject:tempArray];
}
//print out some results to show it worked
NSLog(#"%#%#",#"secondArray first object value 0: ",[[secondArray objectAtIndex:0] objectAtIndex:0] );
NSLog(#"%#%#",#"secondArray first object value 1: ",[[secondArray objectAtIndex:0] objectAtIndex:1] );
NSLog(#"%#%#",#"secondArray second object value 0: ",[[secondArray objectAtIndex:1] objectAtIndex:0] );
NSLog(#"%#%#",#"secondArray second object value 1: ",[[secondArray objectAtIndex:1] objectAtIndex:1] );

Finding the minimum value using KVC across n-keys

Stuck on KVCs in Obj-C again.
I am wanting to use KVC to find the minimum value across multiple keys.
Consider the following array:
NSArray *data = [[NSArray alloc] initWithObjects:
[NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithFloat:2.0], #"a", [NSNumber numberWithFloat:5.0], #"b", [NSNumber numberWithFloat:4.0], #"c", nil],
[NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithFloat:3.0], #"a", [NSNumber numberWithFloat:1.0], #"b", [NSNumber numberWithFloat:1.5], #"c", nil],
[NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithFloat:9.0], #"a", [NSNumber numberWithFloat:7.0], #"b", [NSNumber numberWithFloat:6.0], #"c", nil],
nil];
I can find the minimum value for 'a', 'b', or 'c' easily with:
float minKeyA = [[data valueForKeyPath:#"#min.a"] floatValue]; // value: 2.0
float minKeyB = [[data valueForKeyPath:#"#min.b"] floatValue]; // value: 1.0
float minKeyC = [[data valueForKeyPath:#"#min.c"] floatValue]; // value: 1.5
What I am wanting to achieve is to take a NSSet or NSArray of keys and find the minimum value across the pool of those keys.
NSSet *keySet1 = [NSSet setWithObjects:#"a", #"b", nil];
// use to find absolute minimum value across keys 'a' and 'b' --> desired value 1.0
NSSet *keySet2 = [NSSet setWithObjects:#"a", #"c", nil];
// use to find absolute minimum value across keys 'a' and 'c' --> desired value 1.5
NSSet *keySet3 = [NSSet setWithObjects:#"a", #"b", #"c", nil];
// use to find absolute minimum value across keys 'a', 'b', and 'c' --> desired value 1.0
Appreciate any pointers :)
A naive solution would be to first find the minimum value for each key and then find the minimum among those minimum values in a second step.
NSMutableSet *localMinima = [NSMutableSet setWithCapacity:[keySet1 count]];
for (NSString *key in keySet1) {
NSString *keyPath = [NSString stringWithFormat:#"#min.%#", key];
NSNumber *localMin = [data valueForKeyPath:keyPath];
[localMinima addObject:localMin];
}
NSNumber *globalMin = [localMinima valueForKeyPath:#"#min.self"];

Whats the best way to convert an NSString to an NSInteger based on an array of values?

I want to convert characters into integers based on predetermined values, for example:
a = 0
b = 1
c = 2
d = 3
etc...
Right now I'm doing it with an If/Else If, I just want to know if there is a faster/better way I should be doing it because the list of conversions may get quite long.
Here's what I'm using now:
-(NSInteger)ConvertToInt:(NSString *)thestring {
NSInteger theint;
if([thestring isEqualToString:#"a"] == YES){
theint = 0;
} else if ([thestring isEqualToString:#"b"] == YES){
theint = 1;
} //etc...
return theint;
}
This works fine, but as I said, if it makes more sense can I create an array with all the key/values then just run through that to return the integers?
Please provide examples as I'm a beginner with Objective C/iOS. I come from Web languages.
Thanks!
EDIT: Thanks for the help everyone. I used taskinoors answer but I replaced the NSDictionary which was giving error messages with this:
NSDictionary *dict;
dict = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt:0], #"a",
[NSNumber numberWithInt:1], #"b",
[NSNumber numberWithInt:2], #"c", nil];
unichar ch = [thestring characterAtIndex:0];
theint = ch - 'a';
Note that, 'a' with a single quote is character a, not string "a".
If the values are not regular like your example then you can store all predefined values into a dictionary. For example:
"a" = 5;
"b" = 1;
"c" = 102;
NSArray *values = [NSArray arrayWithObjects:[NSNumber numberWithInt:5],
[NSNumber numberWithInt:1], [NSNumber numberWithInt:102], nil];
NSArray *keys = [NSArray arrayWithObjects:#"a", #"b", #"c", nil];
NSDictionary *dic = [NSDictionary dictionaryWithObjects:values forKeys:keys];
theint = [[dic valueForKey:thestring] intValue];
If you wanted to keep some flexibility in what strings map to what integers, and your integers run from 0 to n-1 where you have n unique items in the array, you could do something like this:
-(NSInteger)ConvertToInt:(NSString *)thestring {
NSArray *arr = [NSArray arrayWithObjects:#"a", #"b", #"c", #"d", nil];
NSInteger theint = [arr indexOfObject:thestring];
return theint;
}
Now this will build the array each time, which would be very inefficient, the optimal way would be to build the array once in your class, and then just use a reference to that array with the indexOfObject method call.

Weird behaviour of NSMutableDictionary

Take a look at this piece of code
- (NSMutableDictionary *)getUsersFromServer
{
//here we're getting list of users from the server
NSMutableDictionary * users = [[[NSMutableDictionary alloc] init] autorelease];
userresults = [[NSMutableArray alloc] init];
//all this will be replaced with users taken from the server. it's needed just for testing
for (int i = 0;i < 19;i++)
{
int wins = i ; float f_wins = (float)wins;
int losses = i * 2 ; float f_losses = (float)losses;
int withdr = i * 3 ; float f_withdr = (float)withdr;
float win_per = f_wins / ((f_wins + f_losses + f_withdr) / 100.0);
[userresults setArray:[NSMutableArray arrayWithObjects:[NSNumber numberWithInt:wins],
[NSNumber numberWithInt:losses],
[NSNumber numberWithInt:withdr],
[NSNumber numberWithFloat:win_per],
nil]];
[users setObject:userresults forKey:[NSString stringWithFormat:#"user number %i",i]];
}
[userresults release];
return users;
}
in each loop iteration i fill array with numbers and set it as value into NSMutableDictionary. As a key for each array serves formatted string which is unique by number of iteration counter. So... the problem is - the dictionary is always filled with SAME arrays for DIFFERENT keys. There are 19 arrays in the dictionary and ALL THEY ARE LAST ONES!!!! That is from the last iteration. And each one has different key!!! How could it happen??? What's going on???
userresults points to the same object, and you're modifying that same array with the setArray: method. Create a new array in each loop iteration instead:
NSArray *userData = [NSArray arrayWithObjects:[NSNumber numberWithInt:wins],
[NSNumber numberWithInt:losses],
[NSNumber numberWithInt:withdr],
[NSNumber numberWithFloat:win_per],
nil];
[users setObject:userData forKey:[NSString stringWithFormat:#"user number %i",i]];
userresults is declared outside the loop unnecessarily. You can forego mutable arrays here and just create a new NSArray with each iteration. Your problem stems from reusing the array declared outside the loop and putting new values in each time. Try this:
[users setObject: [NSArray arrayWithObjects:[NSNumber numberWithInt:wins], [NSNumber numberWithInt:losses], [NSNumber numberWithInt:withdr], [NSNumber numberWithFloat:win_per], nil];
[users setObject:userData forKey:[NSString stringWithFormat:#"user number %i",i]]
forKey:[NSString stringWithFormat:#"user number %i",i]];;

How can I access this variable Globally in Objective C?

Here is the code im having issues with:
if(DriveInfoDict) {
NSLog(#"%#", DriveInfoDict);
//PrevSpeedsDict = [DriveInfoDict objectForKey: #"speed"];
//NSLog(#"Previous Speed Dict:%#", PrevSpeedsDict);
}
DriveInfoDict = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithDouble: CurrentLatitude], #"Lat",
[NSNumber numberWithDouble: CurrentLongitude], #"Long",
[NSNumber numberWithDouble:speedMPH], #"speed",
nil];
Here, I would like to set DriveInfoDict, so that the next time the function runs it will have the previous value. I have stripped the operators to simplify my problem.
The error I am getting is : EXC-BAD-ACCESS
I am new to Obj-C and I do not know how to make this object accessible here. Some code with explanation as to if it goes in the H or M file would be very helpful.
You need to retain the dictionary or use alloc/init (which returns a retained dictionary. So either:
DriveInfoDict = [[NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithDouble: CurrentLatitude], #"Lat",
[NSNumber numberWithDouble: CurrentLongitude], #"Long",
[NSNumber numberWithDouble:speedMPH], #"speed",
nil] retain];
or:
DriveInfoDict = [[NSDictionary alloc] initWithObjectsAndKeys:
[NSNumber numberWithDouble: CurrentLatitude], #"Lat",
[NSNumber numberWithDouble: CurrentLongitude], #"Long",
[NSNumber numberWithDouble:speedMPH], #"speed",
nil];
If you replace the content of DriveInfoDict (that is: assign a new dictionary) don't forget to first release it.