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
}
}
Related
I am using this method to show the string of location using current location latitude and longitude but it is showing differently
NSString *urlString = [NSString stringWithFormat:#"http://maps.googleapis.com/maps/api/geocode/json?latlng=%f,%f&sensor=false",location.coordinate.latitude, location.coordinate.longitude];
NSError* error;
NSString *locationString = [NSString stringWithContentsOfURL:[NSURL URLWithString:urlString] encoding:NSASCIIStringEncoding error:&error];
NSData *data = [locationString dataUsingEncoding:NSUTF8StringEncoding];
id json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
NSDictionary *dic = [[json objectForKey:#"results"] objectAtIndex:0];
NSArray* arr = [dic objectForKey:#"address_components"];
//Iterate each result of address components - find locality and country
NSString *cityName;
NSString *countryName;
for (NSDictionary* d in arr)
{
NSArray* typesArr = [d objectForKey:#"types"];
NSString* firstType = [typesArr objectAtIndex:0];
if([firstType isEqualToString:#"locality"])
cityName = [d objectForKey:#"long_name"];
if([firstType isEqualToString:#"country"])
countryName = [d objectForKey:#"long_name"];
}
NSString* locationFinal = [NSString stringWithFormat:#"%#,%#",cityName,countryName];
NSLog(#"Final Location %# ",locationFinal);
but final location is showing this type :-
Final Location नठदिलà¥à¤²à¥,India
Why it is showing this type? Can anyone know about this.
Please supply the language with the API params. If language is not supplied, the geocoder attempts to use the preferred language as specified in the Accept-Language header, or the native language of the domain from which the request is sent.
So please replace the code as with the language parameter as like this.
NSString *urlString = [NSString stringWithFormat:#"http://maps.googleapis.com/maps/api/geocode/json?latlng=%f,%f&sensor=false&language=en",location.coordinate.latitude, location.coordinate.longitude];
and try again.
I believe that is an uninitialzed variable which is pointing into random memory.
Try:
NSString *cityName = nil;
NSString *countryName = nil;
Short-circuit your for loop:
for (NSDictionary* d in arr)
{
// Add this after the existing code:
if (cityName && countryName)
break;
}
and check for errors before presenting the results:
if (cityName && countryName) {
NSString* locationFinal = [NSString stringWithFormat:#"%#,%#",cityName,countryName];
NSLog(#"Final Location %# ",locationFinal);
} else {
NSLog(#"Failed to find location");
}
Finally your JSON-processing code does no error-checking at all. That's a mistake.
I've a NSMutableArray array having NSDictionary keys as:
NSMutableArray *arr=#[#{#"A":#{#"user":#"obj1",#"friend":#"obj2"}}];
No I want to add objects to this NSMutableArray.
My Code:
for (int i = 0; i < 10; i++)
{
NSString *user = #"A"+i;
for(int i=0;i<50;i++) {
NSString *friend1 = #"B"+i;
NSString *friend2 = #"C"+i;
NSString *friend3 = #"D"+i;
}
}
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:arr options:NSJSONWritingPrettyPrinted error:nil];
NSString *y= [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
NSLog(#"%#",y);
Desired Output:
{"A":[{"user":"A1","friend":"B1"},{"user":"A1","friend":"B2"},{"user":"A2","friend":"B1"}]}
How can I set my objects in order to achieve desired output.
NSString* singleObjectTemplate = [NSString stringWithFormat:#"{\"user\" : \"%#\",\"friend\" : \"%#\"}", user, friend];
NSString* validJsonTemplate = [NSString stringWithFormat:#"{\"A\":[%#]}", singleObjectTemplate];
Something like this. You can modify it as per your requirement to add multiple entries.
I hope this helps you in some way. Cheers!! :)
EDIT:
Suppose you want to add 3 objects to it.
NSString *str = #"";
for (int i = 0; i<3; i++)
{
NSString* singleObjectTemplate = [NSString stringWithFormat:#"{\"user\" : \"%#\",\"friend\" : \"%#\"}", user, friend];
str = [str stringByAppendingString:singleObjectTemplate];
if(i<2)
str = [str stringByAppendingString:#", "];
}
NSString* validJsonTemplate = [NSString stringWithFormat:#"{\"A\":[%#]}", str];
I am fetching data from SQLite and want to get only the last value of column id in XCode.The code is
NSString *selquery = #"select id from watchlists";
if (self.uid != nil) {
self.uid = nil;
}
self.uid = [[NSArray alloc] initWithArray:[self.dbManager loadDataFromDB:selquery]];
NSString *valvar;
valvar = [_uid lastObject];
NSNumber *custval = [_uid valueForKey: #"#lastObject"];
NSString *imgval1 = [NSString stringWithFormat:#"%#_%s",custval,"1"];
NSLog(#"%#", imgval1);
Please tell me how can I get only the value because by using the above code I am getting array with last value of id.
I think this your case, try this it maybe help you
NSArray *temp=[NSArray arrayWithObjects:#"1",#"2",#"3", nil];
NSArray *temp0ne=[[NSArray alloc]initWithArray:temp];
// NSString *tmmp=[temp0ne lastObject];
NSArray *finalStr=[uid lastObject];
NSLog(#"Dictionary is---->%#",[finalStr lastObject]);
Output:
3_1
EDIT
NSArray *temp=[NSArray arrayWithObjects:#"(1)",#"(2)",#"(3)", nil];
NSArray *temp0ne=[[NSArray alloc]initWithArray:temp];
NSString *tmmp=[temp0ne lastObject];
NSString *final=[tmmp stringByReplacingOccurrencesOfString:#"(" withString:#""];
final=[final stringByReplacingOccurrencesOfString:#")" withString:#""];
NSString *imgval1 = [NSString stringWithFormat:#"%#_%s",final,"1"];
NSLog(#"%#", imgval1);
I don't know is this correct way or not try this....otherwise have look this link
I don't fully understand your code structure hehe. Try this:
NSString *selquery = #"select id from watchlists";
if (self.uid != nil) {
self.uid = nil;
}
self.uid = [[NSArray alloc] initWithArray:[self.dbManager loadDataFromDB:selquery]];
NSNumber *custval = [_uid objectAtIndex:[_uid count]-1];
*
NSString *str = [NSString stringWithFormat#"%#",custval];
str = [str stringByReplacingOccurrencesOfString:#"("
withString:#""];
NSString *finalCustval = [NSString stringWithFormat#"%#",str];
finalCustval = [finalCustval stringByReplacingOccurrencesOfString:#")"
withString:#""];
*
NSString *imgval1 = [NSString stringWithFormat:#"%#_%s",finalCustval ,"1"];
NSLog(#"%#", imgval1);
UPDATE
try adding the ones with *.
I am trying to filter words using this code
-(BOOL)isBadWord:(NSString*)string{
NSString* path = [[NSBundle mainBundle] pathForResource:#"wordlist"
ofType:#"txt"];
NSString* content = [NSString stringWithContentsOfFile:path
encoding:NSUTF8StringEncoding
error:NULL];
NSString *delimiter = #"\n";
NSArray *items = [content componentsSeparatedByString:delimiter];
NSString *character = #" ";
NSArray *searchItems = [string componentsSeparatedByString:character];
BOOL isContain = false;
for (int i = 0; i < searchItems.count; i++) {
if (![[searchItems objectAtIndex:i] isEqual:#""]) {
NSUInteger indexOfTheObject =[items containsObject:[searchItems objectAtIndex:i]];
if (indexOfTheObject > 0) {
isContain = true;
}
}
}
return isContain;
}
This is ok for single words, but if combination of words in the text file it not works. eg:
string = word1 {space} word2
What you basically need to do is to iterate an array of bad words/combinations and for each of these steps you should search for this combination on your string like this:
BOOL isContain = NO;
for (NSString *badWord in items) {
if ([string rangeOfString:badWord].location != NSNotFound) {
isContain = YES;
break;
}
}
return isContain;
Please note that BOOL can be YES and NO, but not true and false — it is a special scalar type you should use in Objectve-C when working with Cocoa/CocoaTouch.
Cheers! :)
P.S. it seems you do a lot of work with strings, it may be useful for you to see this String Programming Guide's chapter by Apple.
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.