How to pass URL Dynamically from xml using Attribute in Objective c - ios

How to pass URL Dynamically from xml using Attribute in Objective c and then i want to store url in matches string.
NSString * home= #"http://sports.xyz.rss/rss.aspx";
MSXMLParser * homenew=[[MSXMLParser alloc]init];
[homenew parseWithURLString:home FileName:#"file" xmlType:#"MATCHES"];
NSMutableDictionary * mainDict = [MSDataDictionary newsDictionary];
for (int i=0; i<=[mainDict count]; i++)
{
NSString * theKey = [NSString stringWithFormat:#"%d",i];
NSMutableDictionary * itemDict = [mainDict objectForKey:theKey];
matches = [itemDict objectForKey:#"title"];
if ([matches isEqualToString:#"cric"])
{
matches=[itemDict objectForKey:#"link"];
break;
}
}
NSString * home=matches;

What is MSDataDictionary? I think it should start with NS not MS.
I searched for MSDataDictionary but didn't get any thing.

Related

How to store values at key =0 into nsarray?

I want to store all the emails into an array. What do I need to do for the same ?
Use this code and change it according to your requirement
NSMutableDictionery * repsonseDict = [[NSMutableDictionery alloc]init];
repsonseDict = [reponseObj objectForKey#"response"];
NSArray * allKeys = [repsonseDict allKeys];
NSMutableArray * emailArr = [[NSMutableArray alloc]init];
for(int i = 0; i < [allKeys count]; i ++ ){
NSString * numberKey = [NSString stringWithFormat:#"%d",i];
[emailArr addObject:[[repsonseDict objectForKey:numberKey]objectForKey:#"email"]];
}

How to separate NSString with multiple commas?

Am having this nsstring
NSString * countryStr = #"6023117,159,en_US,Seychelles,SC,Seychelles,6023185,95,en_US,Kuwait,KW,Kuwait,6023182,172,en_US,Swaziland,SZ,Swaziland,6023185,157,en_US,Saudi Arabia,SA,Saudi Arabia,6023182,177,en_US,Tanzania,TZ,Tanzania,6023185,179,en_US,Togo,TG,Togo,6023185,87,en_US,Cote d'Ivoire,CI,Cote d'Ivoire";
now i want to display only the countries which are suffixed by "en_US".
can anybody tell me how to split that string to get the countries.
I did like this
NSError * error;
NSString* imageName = [[NSBundle mainBundle] pathForResource:#"CountryList" ofType:#"txt"];
NSString * countrStr = [NSString stringWithContentsOfFile:imageName encoding:NSStringEncodingConversionAllowLossy error:&error];
NSArray * dfd = [countrStr componentsSeparatedByString:#"en_US"];
for(int i=0;i<dfd.count;i++)
{
NSString * nama = [dfd objectAtIndex:1];
NSArray * djk = [nama componentsSeparatedByString:#","];
NSString * aksjd = [djk objectAtIndex:1];
}
You can do it like this;
NSString * countryStr = #"6023117,159,en_US,Seychelles,SC,Seychelles,6023185,95,en_US,Kuwait,KW,Kuwait,6023182,172,en_US,Swaziland,SZ,Swaziland,6023185,157,en_US,Saudi Arabia,SA,Saudi Arabia,6023182,177,en_US,Tanzania,TZ,Tanzania,6023185,179,en_US,Togo,TG,Togo,6023185,87,en_US,Cote d'Ivoire,CI,Cote d'Ivoire";
NSArray * arrData = [countryStr componentsSeparatedByString:#","];;//[countryStr componentsSeparatedByString:#"en_US"];
for(int i=0;i<arrData.count;i++)
{
NSString * str = [arrData objectAtIndex:i];
if ([str isEqualToString:#"en_US"] && i<arrData.count-1)
{
NSString* countryName = [arrData objectAtIndex:i+1];
NSLog(#"countryName %#", countryName);
}
}
But you should manage data in your file, loading from resource.
Best way to do it is
NSString *string = [NSString stringWithFormat: #"%#, %#, %#", var1, var2, var3];
Excuse the formatting/syntax errors. Typing this via iPhone. But if there is any errors, look up stringWithFormat: in iOS documents on the apple developer page for corrections.
Your string seems to have the following pattern:
A number,
An other number,
The country language code,
The name,
A short code,
An (other) name.
So what you can do is to use a loop like this:
for (int i = 0; i < dfd.count; i += 6) {
if ( dfd[i + 2] ) } // check the country code at index i + 2
// Do something
}
}

Efficiently create placeholder template NSString with NSArray of values

I am trying to make a utility method for FMDB which will take an NSArray of values and return a string of placeholders for use in an IN statement based on the number of values in the array.
I can't think of an elegant way of creating this string, am I missing some NSString utility method:
// The contents of the input aren't important.
NSArray *input = #[#(55), #(33), #(12)];
// Seems clumsy way to do things:
NSInteger size = [input count];
NSMutableArray *placeholderArray = [[NSMutableArray alloc] initWithCapacity:size];
for (NSInteger i = 0; i < size; i++) {
[placeholderArray addObject:#"?"];
}
NSString *output = [placeholderArray componentsJoinedByString:#","];
// Would return #"?,?,?" to be used with input
What about this?
NSArray *input = #[#(55), #(33), #(12)];
NSUInteger count = [input count];
NSString *output = [#"" stringByPaddingToLength:(2*count-1) withString:#"?," startingAtIndex:0];
// Result: ?,?,?
stringByPaddingToLength fills the given string (the empty string in this case)
to the given length by appending characters from the pattern #"?,".

How to retrieve values from two Mutablearrays and store them in MutableDictionary with keys as an array?

I have 3 NSMutableArrays k,names,numbers
k array contains {a,b,c,d,e,.....}
names array contains {apple,bag,banana,car,cat,dall,elephant,.....}
numbers array contains {100,200,300,400,500,600,700,...}
All the 3 arrays are dynamic here.
I want to add names[],numbers[] to NSMutableDictionary with k[] as key array..
My output should be like this when i pint that dictionary
a
apple 100
b
bag 200
banana 300
c
car 400
cat 500
d
dall 600
e
elephant 700
Could somebody help me.
thank you.
NSMutableDictionary *result = [NSMutableDictionary new];
for(int i = 0; i<k.count; i++){
unichar letter = [k objectAtIndex:i]; //I'm assuming you have chars in your k array,
// if not, you have to format it here
NSString* name = [names objectAtIndex:i];
if([[name characterAtIndex:0] isEqual:letter]){
NSString *objectToInsert = [NSString stringWithFormat:#"%#: %#", #"name", [numbers objectAtIndex:i]];
[result setObject:objectToInsert forKey:letter];
}
}
You can probably optimize this more, but it should work ;)
Try with following code
NSMutableDictionary *mydic = [[NSMutableDictionary alloc] init];
For(int k = 0; k < firstArray.count ; k++)
{
NSPredicate *pred =[NSPredicate predicateWithFormat:#"SELF beginswith[c] %#", [firstArray objectAtIndex:k]];
NSArray *filteredArr = [MySecodArray filteredArrayUsingPredicate:pred];
NSString *myString = #"";
for (int t = 0 ; t < filteredArr.count ; t ++)
{
myString = [MyThirdArray objectAtIndex:[MySecodArray indexOfObject:[filteredArr objectAtIndex:t]]];
myString = [[filteredArr objectAtIndex:t] stringByAppendingString:myString];
[mydic setValue: myString forKey:[firstArray objectAtIndex:k]];
myString = #"";
}
}
NSLog(#"%#", myDic);
You can do like This,I am assuming nameArry and NumberArray count is same-->
try This code
NSSMutableDictionary *finalDictionary = [[NSSmutableDictionary alloc]init];
for(int i=0;i<k.count;i++)
{
NSString *alphaBet = [k objectAtIndex:i];
//initialize array here
NSMutableArray *insideDictArray = [[NSMutableArray alloc]init];
for(int j=0;j<nameArray.count;j++)
{
NSString *name = [nameArray objectAtIndex:j];
if([name hasPreffix:alphabet])
{
//get Number String
NSString *numberForName = [numberArray objectAtIndex:j];
//Now Concate Number and Name
NSString *concateString = [name stringByAppendingFormat:#"
%#",numberForName];
//Put this in Array we just intialized outside of secondLoop
[insideDictArray addObject:concateString];
}
}
//Now Add array to finalDictionary
[finalDictionary addObject:insideDictArray forKey:alphabet];
}
OutPut Will Be like this i hope :
finalDictionary =
{
a = [aple 100];
b = [bag 200,banana 300];
.
.
}

Separate two strings from one array element

I was wondering if anyone could lend some assistance. Basically I am calling a web service and then trying to get the large hosted image url. The output from the web service is so:
images = (
{
hostedLargeUrl = "http://i.yummly.com/Crispy-roasted-chickpeas-_garbanzo-beans_-308444.l.jpg";
hostedSmallUrl = "http://i.yummly.com/Crispy-roasted-chickpeas-_garbanzo-beans_-308444.s.jpg";
}
);
The main problem is that the two strings are in only one of my array elements when I think they should be in 2. Also I'm not 100% but possibly they may be a dictionary :-S I'm just not sure. My code is as follows:
NSArray *imageArray = [[NSArray alloc]init];
imageArray = [self.detailedSearchYummlyRecipeResults objectForKey:#"images"];
NSLog(#"imageArray: %#", imageArray);
NSLog(#"count imageArray: %lu", (unsigned long)[imageArray count]);
NSString *hostedLargeurlString = [imageArray objectAtIndex:0];
NSLog(#"imageArrayString: %#", hostedLargeurlString);
The output (nslog's) from the above code is:
2013-04-28 18:59:52.265 CustomTableView[2635:11303] imageArray: (
{
hostedLargeUrl = "http://i.yummly.com/Crispy-roasted-chickpeas-_garbanzo-beans_-308444.l.jpg";
hostedSmallUrl = "http://i.yummly.com/Crispy-roasted-chickpeas-_garbanzo-beans_-308444.s.jpg";
}
)
2013-04-28 18:59:52.266 CustomTableView[2635:11303] count imageArray: 1
2013-04-28 18:59:52.266 CustomTableView[2635:11303] imageArrayString: {
hostedLargeUrl = "http://i.yummly.com/Crispy-roasted-chickpeas-_garbanzo-beans_-308444.l.jpg";
hostedSmallUrl = "http://i.yummly.com/Crispy-roasted-chickpeas-_garbanzo-beans_-308444.s.jpg";
}
Does anyone have any idea how I can seperate the one element into hostedlargeUrl and hostedsmallUrl respectively?
Any help you can provide is greatly appreciated!
Actually the images array contains a dictionary
images = (
{
hostedLargeUrl = "http://i.yummly.com/Crispy-roasted-chickpeas-_garbanzo-beans_-308444.l.jpg";
hostedSmallUrl = "http://i.yummly.com/Crispy-roasted-chickpeas-_garbanzo-beans_-308444.s.jpg";
}
);
so :
NSDictionary *d = [self.detailedSearchYummlyRecipeResults objectForKey:#"images"][0];
NSString *largeURL = d[#"hostedLargeUrl"];
NSString *smallURL = d[#"hostedSmallUrl"];
The value of [imageArray objectAtIndex:0] is a NSDictionary. You've incorrectly specified it as a NSString. You need the following:
NSDictionary *hostedLarguerDictionary =
(NSDictionary *) [imageArray objectAtIndex:0];
and then to access the 'large url' use:
hostedLarguerDictionary[#"hostedLargeUrl"]
or, equivalently
[hostedLarguerDictionary objectForKey: #"hostedLargeUrl"];
looks like an array with in an array so
NSArray* links = [self.detailedSearchYummlyRecipeResults objectForKey:#"images"];
NSString* bigLink = [links objectAtIndex:0];
NSString* smallLink = [links objectAtIndex:1];
or it could be a dictionary
NSDictionary* links = [self.detailedSearchYummlyRecipeResults objectForKey:#"images"];
NSString* bigLink = [links objectForKey:#"hostedLargeUrl "];
NSString* smallLink = [links objectForKey:#"hostedSmallUrl "];
you can see the class of the object by printing out the class name
NSLog(#"Class Type: %#", [[self.detailedSearchYummlyRecipeResults objectForKey:#"images"] class]);

Resources