i am getting all chapters in my chapterDC class and also getting regions, i have a property of selectedRegion in my chapterDC class and want to pass my region data into regionDC through chapterDC,but my log message is always 0 and null, i don t know what mistake i am doing
-(NSMutableArray *)getAllChapters
{
NSMutableArray *returnArray = [[NSMutableArray alloc] init];
NSString *url = [NSString stringWithFormat:#"%#get_all_chapters",kWSURL];
NSDictionary * returnDict = (NSDictionary *) [self callWebService:url];
if([[returnDict objectForKey:#"status"] isEqualToString:#"success"])
{
NSDictionary * responseDict = (NSDictionary *) [returnDict objectForKey:#"response"];
NSArray *resultArray = [responseDict objectForKey:#"result"];
for(NSDictionary *chapterDict in resultArray)
{
ChapterDC * chapter = [[ChapterDC alloc] init];
chapter.ChapterDC_ChapterID=[[chapterDict objectForKey:#"ChapterID"]intValue];
chapter.ChapterDC_ChapterName = [NSString stringWithFormat:#"%#", [chapterDict objectForKey:#"ChapterName"]];
chapter.selectedRegion.RegionDC_RegionID=[ [chapterDict objectForKey:#"RegionID"]intValue];
NSLog(#"%d",chapter.selectedRegion.RegionDC_RegionID);
chapter.ChapterDC_AddressLine1 = [NSString stringWithFormat:#"%#", [chapterDict objectForKey:#"AddressLine1"]];
chapter.ChapterDC_AddressLine2 = [NSString stringWithFormat:#"%#", [chapterDict objectForKey:#"AddressLine2"]];
chapter.ChapterDC_AddressLine3 = [NSString stringWithFormat:#"%#", [chapterDict objectForKey:#"AddressLine3"]];
chapter.ChapterDC_Email = [NSString stringWithFormat:#"%#", [chapterDict objectForKey:#"Email"]];
chapter.ChapterDC_URL = [NSString stringWithFormat:#"%#", [chapterDict objectForKey:#"URL"]];
chapter.ChapterDC_FacebookURL =[ NSString stringWithFormat:#"%#", [chapterDict objectForKey:#"FacebookURL"]];
chapter.ChapterDC_LinkedInURL = [NSString stringWithFormat:#"%#", [chapterDict objectForKey:#"LinkedInURL"]];
chapter.ChapterDC_PhoneNumber =[ NSString stringWithFormat:#"%#", [chapterDict objectForKey:#"PhoneNumber"]];
chapter.ChapterDC_Postcode = [NSString stringWithFormat:#"%#", [chapterDict objectForKey:#"Postcode"]];
chapter.ChapterDC_TwitterURL = [NSString stringWithFormat:#"%#", [chapterDict objectForKey:#"TwitterURL"]];
NSDictionary * regionDict = (NSDictionary *) [returnDict objectForKey:#"region"];
{
chapter.selectedRegion = [[RegionDC alloc] init];
chapter.selectedRegion.RegionDC_RegionID = [[regionDict objectForKey:#"RegionID"]intValue];
NSLog(#"%d",chapter.selectedRegion.RegionDC_RegionID);
chapter.selectedRegion.RegionDC_RegionName = [NSString stringWithFormat:#"%#", [regionDict objectForKey:#"RegionName"]];
NSLog(#"%#",chapter.selectedRegion.RegionDC_RegionName);
chapter.selectedRegion.RegionDC_ContactName = [NSString stringWithFormat:#"%#", [regionDict objectForKey:#"ContactName"]];
chapter.selectedRegion.RegionDC_Mobile = [NSString stringWithFormat:#"%#", [regionDict objectForKey:#"Mobile"]];
chapter.selectedRegion.RegionDC_Landline = [NSString stringWithFormat:#"%#", [regionDict objectForKey:#"Landline"]];
chapter.selectedRegion.RegionDC_TrainingURL = [NSString stringWithFormat:#"%#", [regionDict objectForKey:#"TrainingURL"]];
}
[returnArray addObject:chapter];
}
}
return returnArray;
}
Replace NSDictionary * regionDict = (NSDictionary *) [returnDict objectForKey:#"region"];
with
NSDictionary * regionDict = (NSDictionary *) [chapterDict objectForKey:#"region"];
Related
I want to display the image(which comes from webservice) the format such that the output in the screen appears like below image:-
Now the logic i had implemented is,i had added the image in uiscrollview.
with the below logic:-
for (int j=0;j<9;j++) {
for (int i=0; i<[mainRestaurantArray count];i++) {
if ([[[mainRestaurantArray objectAtIndex:i] valueForKey:#"Image"] isKindOfClass:[UIImage class]]) {
[CombineArray addObject:[mainRestaurantArray objectAtIndex:i]];
//NSLog(#"cnt=>%d array==>%#",cnt,[CombineArray objectAtIndex:cnt]);
UIButton* btn = [[UIButton alloc]init];
btn.tag = cnt;
btn.frame = CGRectMake(15+(cnt%5)*60, 15+(cnt/5)*60,Width,Height);
btn.backgroundColor = [UIColor greenColor];
[btn setBackgroundImage:[[CombineArray objectAtIndex:cnt] valueForKey:#"Image"] forState:UIControlStateNormal];
[btn addTarget:self action:#selector(Buttonclick:) forControlEvents:UIControlEventTouchUpInside];
[ScrlPhotos addSubview:btn];
[btn release];
cnt++;
}
}
[mainRestaurantArray release];
counter++;
[self urlcalled];//The function which calls the webservice
}
//}
ScrlPhotos.contentSize = CGSizeMake(320, ([CombineArray count]/5.0)*60+25);
The function which does the webservice is below:-
-(void)urlcalled{
#try
{
if (rangeDistance == nil) {
rangeDistance =#"100";
}
urlstring[0]=[NSString stringWithFormat:#"http://www.google.com/maps?q=Gym&sll=%#,%#&radius=200000&output=json",AppDel.Latitude,AppDel.Longitude];
urlstring[1]=[NSString stringWithFormat:#"http://www.google.com/maps?q=resort&sll=%#,%#&radius=200000&output=json",AppDel.Latitude,AppDel.Longitude];
urlstring[2]=[NSString stringWithFormat:#"http://www.google.com/maps?q=Tourist place&sll=%#,%#&radius=200000&output=json",AppDel.Latitude,AppDel.Longitude];
urlstring[3]=[NSString stringWithFormat:#"http://www.google.com/maps?q=Hotels&sll=%#,%#&radius=200000&output=json",AppDel.Latitude,AppDel.Longitude];
urlstring[4]=[NSString stringWithFormat:#"http://www.google.com/maps?q=shopping Mall&sll=%#,%#&radius=200000&output=json",AppDel.Latitude,AppDel.Longitude];
urlstring[5]=[NSString stringWithFormat:#"http://www.google.com/maps?q=Industries&sll=%#,%#&radius=200000&output=json",AppDel.Latitude,AppDel.Longitude];
urlstring[6]=[NSString stringWithFormat:#"http://www.google.com/maps?q=Shopping_mall&sll=%#,%#&radius=200000&output=json",AppDel.Latitude,AppDel.Longitude];
urlstring[7]=[NSString stringWithFormat:#"http://www.google.com/maps?q=garden&sll=%#,%#&radius=200000&output=json",AppDel.Latitude,AppDel.Longitude];
urlstring[8]=[NSString stringWithFormat:#"http://www.google.com/maps?q=Religious Place&sll=%#,%#&radius=200000&output=json",AppDel.Latitude,AppDel.Longitude];
urlstring[9]=[NSString stringWithFormat:#"http://www.google.com/maps?q=Restaurants&sll=%#,%#&radius=200000&output=json",AppDel.Latitude,AppDel.Longitude];
NSLog(#"conte==>%d urlstring[counter]==>%#",counter,urlstring[counter]);
NSString *str = [NSString stringWithContentsOfURL:[NSURL URLWithString:urlstring[counter]]];
str1 = [str substringFromIndex:9];
//NSLog(#"===>%#",str1);
NSArray *array = [str1 componentsSeparatedByString:#"overlays:"];
NSString *str2 = [array objectAtIndex:1];
NSString *finalUpperStr = [str2 substringFromIndex:21];
array1 = [finalUpperStr componentsSeparatedByString:#"},panel:"];
NSString *strF = [array1 objectAtIndex:0];
NSArray *arrayF = [strF componentsSeparatedByString:#"{id:"];
///////....................this object will change as require in for loop .............................//
mainRestaurantArray = [[NSMutableArray alloc] init];
for (int i=1; i<[arrayF count]-5; i++) {
NSString *strF12 = [arrayF objectAtIndex:i];
NSMutableDictionary *dic = [[NSMutableDictionary alloc] init];
NSArray *aryF23 = [strF12 componentsSeparatedByString:#"latlng:{"];
//NSLog(#"%#",[aryF23 objectAtIndex:1]);
NSString *strF23 = [aryF23 objectAtIndex:1];
NSArray *ar = [strF23 componentsSeparatedByString:#"},image:"];
//NSLog(#"%#",[ar objectAtIndex:0]);
NSString *strLat = [ar objectAtIndex:0];
NSArray *arrayLat = [strLat componentsSeparatedByString:#","];
NSString *strFLat = [[arrayLat objectAtIndex:0] substringFromIndex:4];
NSString *strLong = [[arrayLat objectAtIndex:1] substringFromIndex:4];
//NSLog(#"Lati = %# Longi = %#" , strFLat , strLong);
[dic setValue:strFLat forKey:#"Latitude"];
[dic setValue:strLong forKey:#"Longitude"];
NSString *strLAdd = [ar objectAtIndex:1];
NSArray *arrayLAdd = [strLAdd componentsSeparatedByString:#"laddr:"];
//NSLog(#"%#",[arrayLAdd objectAtIndex:1]);
NSString *strLAdd1 = [arrayLAdd objectAtIndex:1];
NSArray *arrayLAdd1 = [strLAdd1 componentsSeparatedByString:#"geocode:"];
// NSLog(#"%#",[arrayLAdd1 objectAtIndex:0]);
NSString *strAddres = [[arrayLAdd1 objectAtIndex:0] stringByReplacingOccurrencesOfString:#"\"" withString:#""];
//NSLog(#"%#",strAddres);
[dic setValue:strAddres forKey:#"Address"];
NSString *strName = [arrayLAdd1 objectAtIndex:1];
NSArray *arrayName = [strName componentsSeparatedByString:#"name:"];
NSString *strName1 = [[arrayName objectAtIndex:1]stringByReplacingOccurrencesOfString:#"\"" withString:#""];
NSArray *arrayName1 = [strName1 componentsSeparatedByString:#"infoWindow:"];
[dic setValue:[arrayName1 objectAtIndex:0] forKey:#"Name"];
// for image load .. 07Dec..
NSString *strImage= [arrayF objectAtIndex:i];
NSRange range = [strImage rangeOfString:#"photoUrl"];
if(range.location == NSNotFound){
NSLog(#"Not found");
[dic setValue:#"" forKey:#"Image"];
}else{
NSArray *arrayImage = [strImage componentsSeparatedByString:#"photoUrl:"];
NSString *strImage1 = [arrayImage objectAtIndex:1];
NSArray *arrayImage1 = [strImage1 componentsSeparatedByString:#",photoType:"];
NSString *strfindImage = [[arrayImage1 objectAtIndex:0] stringByReplacingOccurrencesOfString:#"\"" withString:#""];
myImage = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:strfindImage]]];
[dic setValue:myImage forKey:#"Image"];
}
[mainRestaurantArray addObject:dic];
if (i==20) {
break;
}
}
}
#catch (NSException * e)
{
NSLog(#"NSException==>%#",e);
}
}
But the problem i am facing is the image is displayed after completing the loop at 10 times.
I want to display the image parallely as it comes from webservice,so that time taken should be less..
Is there any way out..
Please help me.
Use UIImageView+cacheWeb.. it makes lazy loading a lot easier
http://hackemist.com/SDWebImage/doc/Categories/UIImageView+WebCache.html
I need to extract a variable's value from a string, which happens to be a URL. The string/url is loaded as part of a separate php query, not the url in the browser.
The url's will look like:
http://gmail.com?access_token=ab8w4azq2xv3dr4ab37vvzmh&token_type=bearer&expires_in=3600
How can I capture the value of the access_token which in this example is ab8w4azq2xv3dr4ab37vvzmh?
This code should do it:
- (NSString *)extractToken:(NSURL *)URL
{
NSString *urlString = [URL absoluteString];
NSRange start = [urlString rangeOfString:#"access_token="];
if (start.location != NSNotFound)
{
NSString *token = [urlString substringFromIndex:start.location+start.length];
NSRange end = [token rangeOfString:#"&"];
if (end.location != NSNotFound)
{
//trim off other parameters
token = [token substringToIndex:end.location];
}
return token;
}
//not found
return nil;
}
Alternatively, here is a more general solution that will extract all the query parameters into a dictionary:
- (NSDictionary *)URLQueryParameters:(NSURL *)URL
{
NSString *queryString = [URL query];
NSMutableDictionary *result = [NSMutableDictionary dictionary];
NSArray *parameters = [queryString componentsSeparatedByString:#"&"];
for (NSString *parameter in parameters)
{
NSArray *parts = [parameter componentsSeparatedByString:#"="];
NSString *key = [[parts objectAtIndex:0] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
if ([parts count] > 1)
{
id value = [[parts objectAtIndex:1] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[result setObject:value forKey:key];
}
}
return result;
}
Good Category for NSDictionary:
#import "NSDictionary+URL.h"
#implementation NSDictionary (URL)
+ (NSDictionary *)dictionaryWithUrlString:(NSString *)urlString{
NSRange urlRange = [urlString rangeOfString:#"?"];
if(urlRange.length>0){
urlString = [urlString substringFromIndex:urlRange.length+urlRange.location];
}
NSArray *pairsArray = [urlString componentsSeparatedByString:#"&"];
NSMutableDictionary *parametersDictionary = [[NSMutableDictionary alloc] initWithCapacity:[pairsArray count]];
for(NSString *pairString in pairsArray){
NSArray *valuesArray = [pairString componentsSeparatedByString:#"="];
if([valuesArray count]==2){
[parametersDictionary setValue:[valuesArray objectAtIndex:1] forKey:[valuesArray objectAtIndex:0]];
}
}
return [parametersDictionary autorelease];
}
#end
NSMutableDictionary *querycomponent = [[NSMutableDictionary alloc] init];
if (![query isEqualToString:#""]){
NSArray *queryArray = [query componentsSeparatedByString:#"&"];
for (NSString *subquery in queryArray){
NSArray *subqueryArray = [subquery componentsSeparatedByString:#"="];
NSString *key = [subqueryArray objectAtIndex:0];
NSString *val = [subqueryArray objectAtIndex:1];
[querycomponent setObject:val forKey:key];
}
NSLog(#"querycomponent %#",querycomponent);
}
I have string like 123-123-1234 so I want to convert string into this format
(123) 123-1234 so any idea to develop this functionality.
Thanks in advance.
I think something like this:
NSString *list = #"123-123-1234";
NSArray *listItems = [list componentsSeparatedByString:#"-"];
NSString *result = [NSString stringWithFormat:#"(%#) %#-%#", [listItem objectAtIndex:0], [listItem objectAtIndex:1], [listItem objectAtIndex:2]];
NSString * original = #"123-123-1234";
NSArray * components = [original componentsSeparatedByString:#"-"];
NSString * first = [NSString stringWithFormat:#"(%#)",[components objectAtIndex:0] ];
NSString * second = [NSString stringWithFormat:#"%#-%#",[components objectAtIndex:1],[components objectAtIndex:2]];
NSString * finalString = [NSString stringWithFormat:#"%#%#",first,second];
NSLog(#"Final Result = %#",finalString);
NSString *str = #"123-123-1234";
NSArray *arrForDate = [str componentsSeparatedByString: #"-"];
NSString *str1 = [NSString stringWithFormat:#"(%#) %#-%#",[arrForDate objectAtIndex:0],[arrForDate
objectAtIndex:1],[arrForDate objectAtIndex:2]];
NSLog(#"str1 %#",str1);
TESTED CODE: 100% WORKS
NSString *inputString=#"123-123-1234";
NSArray *TotalString=[inputString componentsSeparatedByString:#"-"];
NSString *outputString = [NSString stringWithFormat:#"(%#) %#-%#",[TotalString objectAtIndex:0],[TotalString objectAtIndex:1],[TotalString objectAtIndex:2]];
NSLog(#"outputString is : %# \n\n",outputString);
OUTPUT:
outputString is : (123) 123-1234
For Dynamic Sollution:
NOTE: if u have more dashes with ur string then it is really hard code everything with objectAtIndex with number 0,1,2,....
moreover it will crash if it has unexpected length
so here is the solution for this
NSString *inputString=#"123-123-1234";
NSArray *TotalString=[inputString componentsSeparatedByString:#"-"];
NSMutableString *outputString=[NSMutableString string];
for (NSMutableString *obj in TotalString) {
if ([TotalString objectAtIndex:0] == obj && [outputString length]<=0) {
outputString=[[outputString stringByAppendingFormat:#"(%#)",obj] mutableCopy];
}
else if ([TotalString objectAtIndex:1] == obj) {
outputString=[[outputString stringByAppendingFormat:#"%#%#",#" ",obj] mutableCopy];
}
else {
outputString=[[outputString stringByAppendingFormat:#"-%#",obj] mutableCopy];
}
}
OUTPUT:
outputString is : (123) 123-1234
NSString *list = #"123-123-1234";
NSArray *listItems = [list componentsSeparatedByString:#"-"];
NSString *result = [NSString stringWithFormat:#"(%#) %#-%#", [listItem objectAtIndex:0], [listItem objectAtIndex:1], [listItem objectAtIndex:2]];
The following code gives the result of the methods which I am calling individually.
My actual requirement is that I want to call just a particular method, for example a Loan method, and it should display all the contents in this method.
So what changes should I make in the code below?
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
[connection release];
NSString *responseString = [[NSString alloc] initWithData:dataWebService encoding:NSUTF8StringEncoding];
self.dataWebService = nil;
NSArray* latestLoans = [(NSDictionary*) [responseString JSONValue] objectForKey:#"loans"];
[responseString release];
NSDictionary* loan = [latestLoans objectAtIndex:0];
//fetch the data
NSNumber* fundedAmount = [loan objectForKey:#"funded_amount"];
NSNumber* loanAmount = [loan objectForKey:#"loan_amount"];
float outstandingAmount = [loanAmount floatValue] - [fundedAmount floatValue];
NSString* name = [loan objectForKey:#"name"];
NSString* country = [(NSDictionary*)[loan objectForKey:#"location"] objectForKey:#"country"];
//set the text to the label
label.numberOfLines = 0;
label.text = [NSString stringWithFormat:#"Latest loan: %# \n \n country: %# \n \n amount $%.2f", name,country,outstandingAmout ];
}
I am not sure if I understand you right but if you want to just use a method such as
NSString * loanString = [self stringFromLoan:[latestLoans objectAtIndex:0]];
Do this:
-(NSString*)stringFromLoan:(NSDictionary*)loan{
NSNumber* fundedAmount = [loan objectForKey:#"funded_amount"];
NSNumber* loanAmount = [loan objectForKey:#"loan_amount"];
float outstandingAmount = [loanAmount floatValue] - [fundedAmount floatValue];
NSString* name = [loan objectForKey:#"name"];
NSString* country = [(NSDictionary*)[loan objectForKey:#"location"] objectForKey:#"country"];
return [NSString stringWithFormat:#"Latest loan: %# \n \n country: %# \n \n amount $%.2f", name,country,outstandingAmout];
}
So your code would look like this:
-(void)connectionDidFinishLoading:(NSURLConnection *)connection{
NSString *responseString = [[NSString alloc] initWithData:dataWebService encoding:NSUTF8StringEncoding];
self.dataWebService = nil;
NSArray* latestLoans = [(NSDictionary*) [responseString JSONValue] objectForKey:#"loans"];
[responseString release];
NSDictionary* loan = [latestLoans objectAtIndex:0];
label.numberOfLines = 0;
label.text = [self stringFromLoan:loan];
}
PS.
Don't release the connection it is autoreleased.
UITableviewController can be used for displaying the contents of the NSArray object.
-(void) readRestaurantFromXml {
SGAAppDelegate *app = (SGAAppDelegate *)[[UIApplication sharedApplication] delegate];
NSString *myRequestString = [NSString stringWithFormat:#"%#getXml.aspx",app.SERVER_URL];
NSURL *url = [NSURL URLWithString: myRequestString];
CXMLDocument *rssParser = [[[CXMLDocument alloc] initWithContentsOfURL:url options:0 error:nil] autorelease];
resultNodes = [rssParser nodesForXPath:#"//item" error:nil];
listOfItems = [[NSMutableArray alloc] init];
for (CXMLElement *resultElement in resultNodes) {
NSString *theatre_id = [[resultElement childAtIndex:0] stringValue];
NSString *theatre_name = [[resultElement childAtIndex:1] stringValue];
NSString *restaurant_id = [[resultElement childAtIndex:2] stringValue];
NSString *restaurant_name = [[resultElement childAtIndex:3] stringValue];
NSString *restaurant_desc = [[resultElement childAtIndex:4] stringValue];
NSString *deal_avail = [[resultElement childAtIndex:5] stringValue];
NSString *deal_details = [[resultElement childAtIndex:6] stringValue];
Restaurant *workVal = [[Restaurant alloc] initWithRestTitle:restaurant_name restDesc:restaurant_desc theatreId:theatre_id theatreName:theatre_name restId:restaurant_id restDeals:deal_avail restDealDesc:deal_details];
[listOfItems addObject:workVal];
[workVal release];
}
}
-(void)myTask{
NSLog(#"%d",[listOfItems count]);
}
The NSMutableArray listofItems has 3 values.So how can i extract the array value in another method?
Help me
Fast enumeration is the preferred method.
for (id object in listOfItems) {
//Do something...
}
If you want a single object at an index:
id object = [listOfItems objectAtIndex:someIndex];
If you just want an object (expecting the array to have a single object:
id object = [listOfItems lastObject];
for(int i=0; i < [listOfItems count]; ++i) {
Restaurant* r = [listofItems objectAtIndex:i];
}