How retrireve array from dictionary - iphone

I have 2 columns in my Sqlite table 1.DetailsID & 2.Detailstype
i have stored values id: int and detailstype :varchar.
set the id with string in sqlite select query as
while(sqlite3_step(selectPrefer) == SQLITE_ROW)
{
NSString *detailsString = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectPrefer, 1)];
int detailsId = (int)sqlite3_column_int(selectPrefer, 0);
[detailsData setObject:detailsString forKey:[NSNumber numberWithInt:detailsId ]];
}
I have a NSmutable dictionary like this:
(
0 = "˚F";
12 = Activity;
11 = BM;
7 = "Heart Rate";
6 = "Nose Problem";
2 = Rx;
1 = BP;
10 = Food;
9 = "Stress Level";
8 = Glucose;
5 = "Pain Level";
4 = Weight;
3 = Events;
}
i can get arrays using allKeys & allValues but these are not in order
Now i want seperate arrays like
{
0
1
2
3
4
5
6
7
8
9
10
11
12
)
both values & keys In ascending order with respect to keys
(
"˚F";
Activity;
BM;
"Heart Rate";
"Nose Problem";
Rx;
BP;
Food;
"Stress Level";
Glucose;
"Pain Level";
Weight;
Events;
)
with out any modifications in sqlite query
- what to do thannks in advance

You should use the allKeys and allValues property of the dictionay :
allKeys Returns a new array containing the dictionary’s keys.
allValues Returns a new array containing the dictionary’s values.
Try this :
NSArray *keysArray = [yourDictionnay allKeys];
NSArray *valuesArray = [yourDictionnay allvalues];
Hope this helps,
Vincent

you need both allValues and allkeys.
NSArray *values = [dictionary allValues];
NSArray *keys = [dicitoary allKeys];

Related

handle JSON response in project

I am a new programmer. I get the following response from server. How can i get the value of 0 index "Mile High Motors of Butte" and "Mile High Motors of Dillion" from following
Thanks
{
dealer = (
{
0 = "Mile High Motors of Butte";
1 = "3883 Harrison";
2 = Butte;
3 = 59701;
4 = MT;
5 = "http://www.buttesmilehighchryslerjeepdodge.com";
6 = 2;
7 = 0;
address = "3883 Harrison";
city = Butte;
distance = 0;
id = 2;
name = "Mile High Motors of Butte";
state = MT;
url = "http://www.buttesmilehighchryslerjeepdodge.com";
zip = 59701;
},
{
0 = "Mile High Motors of Dillon";
1 = "790 N Montana St";
2 = Dillon;
3 = 59725;
4 = Montana;
5 = "http://www.MileHighDillon.com";
6 = 13;
7 = "60.1235269593172";
address = "790 N Montana St";
city = Dillon;
distance = "60.1235269593172";
id = 13;
name = "Mile High Motors of Dillon";
state = Montana;
url = "http://www.MileHighDillon.com";
zip = 59725;
}
);
success = 1;
}
Okay let's see your structure (assuming that you have already deserialized your JSON string).
You have an NSDictionary with two keys (dealer & success). Now dealer key is an NSArray with two NSDictionaries. So based on that we could do:
NSDictionary *myJson; // Assuming that this is what you have posted
NSArray *dealers = [myJson valueForKey:#"dealer"];
// Now just grab whatever you need
NSString *dealerOne = [[dealers objectAtIndex:0] valueForKey:#"0"]; //Mile High Motors of Butte
NSString *dealerTwo = [[dealers objectAtIndex:1] valueForKey:#"0"]; //Mile High Motors of Dillon
Or you could just iterate your dealers array like this:
for (NSDictionary *dealer in dealers)
{
NSString *dealerName = [dealer valueForKey:#"0"];
// Do something useful
}
NSMutableArray yourStringArray= [[NSMutableArray alloc] init]; //for getting texts what you want.
NSArray * array1 = [yourDictionary valueForKey:#"dealer"];// You will get array which has dictionary elements.
for (NSDictionary *dealer in array1)// Write loop for getting your string.
{
NSString *dealerName = [dealer valueForKey:#"0"];
[yourStringArray addObject:dealerName];
}
I think it will be helpful to you.

iOS: formatting decimal numbers

I have an NSDecimalNumber representing a money amount.
I want to print it as "999 999 999 999 999 999,00", regardless on the locale.
How do I do that?
NSNumberFormatter prints me 1 000 000 000 000 000 000,00 instead (it seems Apple engineers never designed the iPhone to be a platform for financial software).
[NSDecimalNumber description] and [NSDecimalNumber descriptionWithLocale] both print correct value. How can I format the result, with grouping separator set to #"\u2006", decimal separator to #"**,**", and exactly 2 decimal digits after the decimal seperator?
Thanks in advance!
Update:
Here's my solution, 10x to Sulthan:
#implementation NSDecimalNumber(MiscUtils)
-(NSString*)moneyToString
{
static NSDecimalNumberHandler* s_handler = nil;
if( !s_handler )
s_handler = [ [ NSDecimalNumberHandler decimalNumberHandlerWithRoundingMode:NSRoundPlain scale:2 raiseOnExactness:NO raiseOnOverflow:NO raiseOnUnderflow:NO raiseOnDivideByZero:NO ] retain ];
NSDecimalNumber *dec = [ self decimalNumberByRoundingAccordingToBehavior:s_handler ];
NSString* str = [ dec description ];
NSRange rDot = [ str rangeOfString:#"." ];
int nIntDigits = str.length;
int nFracDigits = 0;
if( rDot.length > 0 )
{
nIntDigits = rDot.location;
nFracDigits = str.length - ( rDot.location + 1 );
}
int nGroupSeparators = ( nIntDigits - 1 ) / 3;
NSMutableString* res = [ NSMutableString stringWithCapacity:nIntDigits + nGroupSeparators + 3 ];
NSString *groupingSeparator = #"\u2006";
int nFirstGroup = ( nIntDigits % 3 );
int nextInd = 0;
if( nFirstGroup )
{
[ res appendString:[ str substringToIndex:nFirstGroup ] ];
nextInd = nFirstGroup;
}
while( nextInd < nIntDigits )
{
if( res.length > 0 )
[ res appendString:groupingSeparator ];
[ res appendString:[ str substringWithRange:NSMakeRange( nextInd, 3 ) ] ];
nextInd += 3;
}
if( nFracDigits > 0 )
{
if( nFracDigits > 2 )
nFracDigits = 2;
[ res appendString:#"," ];
[ res appendString:[ str substringWithRange:NSMakeRange( rDot.location + 1, nFracDigits ) ] ];
while( nFracDigits < 2 )
{
[ res appendString:#"0" ];
nFracDigits++;
}
}
else
[ res appendString:#",00" ];
// DLog( "formatDecimal: %# -> %#", dec, res );
return res;
}
#end
Try this:
NSLocale *usLocale = [[NSLocale alloc] initWithLocaleIdentifier:#"US"];
NSNumberFormatter *frm = [[NSNumberFormatter alloc] init];
[frm setNumberStyle:NSNumberFormatterDecimalStyle];
[frm setMaximumFractionDigits:2];
[frm setMinimumFractionDigits:2];
[frm setLocale:usLocale];
NSString *formattedNumberStr = [frm stringFromNumber:[NSNumber numberWithFloat:floatToRound]];
[frm release];
[usLocale release];
return formattedNumberStr;
I had the same problem. I was told that NSNumberFormatter converts everything to a double first. How did I solve it? It's easy, just write your own formatter.
First round the decimal to 2 decimal digits.
Get its description as a string.
Find decimal point (.) in the string and replace it with the one you want.
After every three digits from the decimal point to the left, insert a grouping separator.

Sorting NSMutableArray?

I want to be sort NSMutableArray and its structure is something like that,
Object1:NSMutableDictionary
Affiliation = 2165;
CallLetters = abc;
Channel = "2.1";
ChannelLocation = "";
ChannelSchedules = (
);
DisplayName = abc;
DvbTriplets = (
);
FullName = "bc";
IconAvailable = 1;
Order = 1;
ParentNetworkId = 2;
ServiceType = Digital;
SourceAttributeTypes =
HD
SourceId = 11222;
SourceType = Broadcast;
TiVoSupported = 1;
Type = "24-Hours";
Object2:NSMutableDictionary
Affiliation = 1209;
CallLetters = "xyz";
Chann?el = "4.1";
ChannelLocation = "";
ChannelSchedules = (
);
DisplayName = "xyz";
DvbTriplets = (
);
FullName = "xyz";
IconAvailable = 1;
Order = 2;
ParentNetworkId = 5;
ServiceType = Digital;
SourceAttributeTypes = HD
SourceId = 111
SourceType = Broadcast;
TiVoSupported = 1;
Type = "24-Hours";
VirtualChannelNumber = "4.1";
...
..
.
.
.
./
The array contains many objects, and those objects contain many dictionaries. I want to be able to arrange the above array in ascending order using the NSMutableDictionary key "Channel" ?
How can I sort the array?
Use NSSortDescriptor it will work
NSSortDescriptor *aSortDescriptor = [[NSSortDescriptor alloc] initWithKey:#"Channel" ascending:YES];
[yourarrayobject sortUsingDescriptors:[NSArray arrayWithObject:aSortDescriptor]];
[aSortDescriptor release];
To sort your array of objects you:
setup NSSortDescriptor - use names of your variables as keys to
setup descriptor for sorting plus the selector to be executed on
those keys
get the array of descriptors using NSSortDescriptor that you've
setup
sort your array based on those descriptors
How to sort NSMutableArray using sortedArrayUsingDescriptors?

Array index is always 0, can't get it to iterate through the array in a forloop. Using in MapView

I have an array that I want to show on a mapvew, the forloop iterates fine but the index of thew array is always 0.
self.clientTable = [ClientDatabase database].clientTable;
ClientTable *info = nil;
[_nameLabel setText:info.name];
[_stateLabel setText:info.state];
int countArray = [self.clientTable count];
for (int i=0;i<countArray;i++) {
info.uniqueId=i;
NSLog(#" i = %d ; id = %d",i, info.uniqueId);
}
however the results are always
24
i = 0 ; id = 0
i = 1 ; id = 0
i = 2 ; id = 0
i = 3 ; id = 0
i = 4 ; id = 0
i = 5 ; id = 0
I know the array has data as it displays in the tableview fine.
Any ideas?
The reason for the above is displaying each item in a mapview.
Thankyou!
Before this line
info.uniqueId=i;
are you missing something like
info = [self.clientTable objectAtIndex:i]
?? In the code you've provided you set info to nil, but never to anything else.
Because you set info to nil, right up there.

#'Event$variable' iphone Object C

$variable =1;
How i can do it?
I need insert variable 1 2 3 4 5 ...
in #"Event" or #'Event'
Stabbing in the dark here, but is this what you want?
int variable = 1;
NSString *str = [NSString stringWithFormat:#"Event%d", variable];