Muju Muju -4 years ago 179
iOS Question

How to get the current String Value from array in objective C

I am new in iOS and I am facing problem regarding to get current value of string from array.

My code is like this

loginStatusHS = [[NSString alloc] initWithBytes: [myNSMDatalatetudeFromServer mutableBytes] length:[myNSMDatalatetudeFromServer length] encoding:NSUTF8StringEncoding];
NSLog(@"loginStatus =%@",loginStatusHS);
NSError *parseError = nil;
NSDictionary *xmlDictionary = [XMLReader dictionaryForXMLString:loginStatusHS error:&parseError];
NSLog(@"JSON DICTIONARY = %@",xmlDictionary);
recordResultHS = [xmlDictionary[@"success"] integerValue];
NSLog(@"Success: %ld",(long)recordResultHS);
NSDictionary* Address=[xmlDictionary objectForKey:@"soap:Envelope"];
NSLog(@"Address Dict = %@",Address);
NSDictionary *new =[Address objectForKey:@"soap:Body"];
NSLog(@"NEW DICT =%@",new);
NSDictionary *LoginResponse=[new objectForKey:@"HS_GetResponse"];
NSLog(@"Login Response DICT =%@",LoginResponse);
NSDictionary *LoginResult=[LoginResponse objectForKey:@"HS_GetResult"];
NSLog(@"Login Result =%@",LoginResult);
if(LoginResult.count>0)
{
NSLog(@"Login Result = %@",LoginResult);
NSLog(@"Login Result Dict =%@",LoginResult);
NSString *teststr =[[NSString alloc] init];
teststr =[LoginResult objectForKey:@"text"];
NSLog(@"Test String Value =%@",teststr);
NSString *string = [LoginResult valueForKey:@"text"];
NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding];
responseletetudedict = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
NSLog(@"Latetude Dictionary =%@",responseletetudedict);
idlatetudearray=[[NSMutableArray alloc]init];
idlatetudearray=[responseletetudedict valueForKey:@"City"];

NameHSArray=[[NSMutableArray alloc] init];
NameHSArray=[responseletetudedict valueForKey:@"Name"];
AddressHSArray=[[NSMutableArray alloc] init];
AddressHSArray=[responseletetudedict valueForKey:@"Address"];
FacilitiesHSArray=[[NSMutableArray alloc] init];
FacilitiesHSArray=[responseletetudedict valueForKey:@"Facilities"];
PhoneNoHSArray=[[NSMutableArray alloc] init];
PhoneNoHSArray=[responseletetudedict valueForKey:@"Phoneno"];
FaxnoHSArray=[[NSMutableArray alloc] init];
FaxnoHSArray=[responseletetudedict valueForKey:@"Faxno"];
LatitudeHSArray=[[NSMutableArray alloc] init];
LatitudeHSArray=[responseletetudedict valueForKey:@"Latitude"];
LongitudeHSArray=[[NSMutableArray alloc] init];
LongitudeHSArray=[responseletetudedict valueForKey:@"Longitude"];
TypeHSArray=[[NSMutableArray alloc] init];
TypeHSArray=[responseletetudedict valueForKey:@"Type"];

for (int i=0; i<NameHSArray.count; i++) {
double LatitudeDouble = [LatitudeHSArray[i] doubleValue];
double LongitudeDouble = [LongitudeHSArray[i] doubleValue];
CLLocationCoordinate2D position = CLLocationCoordinate2DMake(LatitudeDouble, LongitudeDouble);
GMSMarker *marker = [GMSMarker markerWithPosition:position];
marker.title = NameHSArray[i];
marker.snippet=AddressHSArray[i];
userData = [[NSArray alloc] initWithObjects:NameHSArray[i], AddressHSArray[i],FacilitiesHSArray[i], PhoneNoHSArray[i],FaxnoHSArray[i], TypeHSArray[i], nil];
marker.userData = userData;

if([TypeHSArray[i] isEqualToString:@"ESIC"])
{
marker.icon = [UIImage imageNamed:@"mapicon2.png"];
}
else
{
marker.icon = [UIImage imageNamed:@"mapicon1.png"];
}
GMSCameraUpdate *zoomCamera = [GMSCameraUpdate zoomIn];
[mapView animateWithCameraUpdate:zoomCamera];

marker.map = mapView;

}


enter image description here

Add in the Image when I click on Nobel Hospital I call the delegate

- (void)mapView:(GMSMapView *)mapView didTapInfoWindowOfMarker:(GMSMarker *)marker {
// your click action

StringAddress = marker.snippet;
StringName = marker.title;

NSLog(@"Address=%@",StringAddress);
NSLog(@"Name= %@",StringName);

lblNamepopup.text=StringName;
lblAddresspopup.text=StringAddress;

NSLog(@"User Data Array = %@",userData);

viewpopup.hidden=NO;
viewpopup.transform = CGAffineTransformMakeScale(0.01, 0.01);
[UIView animateWithDuration:0.2 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
viewpopup.transform = CGAffineTransformIdentity;
} completion:^(BOOL finished){
// do something once the animation finishes, put it here
}];
}


Hear in this delegate I need to get the current name of string address.But hear I am getting the Last value means the string get override. How can I get the value which I have click from array. Thanks in Advance!

Answer Source

You can get index as follow

-(void)mapView:(GMSMapView *)mapView didTapInfoWindowOfMarker:(GMSMarker *)marker
{
    NSInteger index = [NameHSArray indexOfObject:marker.title];
    NSLog(@"%ld",(long)index);
}

--- EDIT ---

You can also use

  • i found some reference from library.
  • Note that userData should not hold any strong references to any Maps
  • objects, otherwise a loop may be created (preventing ARC from releasing objects).

NOTE :- You can pass data through snippet but snippet show data into info window. so you creates a custom info window and show data as you want.
like this,

NSArray * userData = [NSArray alloc] initWithObjects:FacilitiesHSArray[i], PhoneNoHSArray[i],FaxnoHSArray[i], nil];
NSString *userDataString = [userData componentsJoinedByString:@";"];
marker.snippet = userDataString;

retrive
like this,

 NSString *userDataString = marker.snippet;
 NSArray *array = [userDataString componentsSeparatedByString:@";"];
 NSLog(@"%@",array);

You can Do Like this

- (void)mapView:(GMSMapView *)mapView didTapInfoWindowOfMarker:(GMSMarker *)marker {
    // your click action

    StringAddress =  marker.snippet;
    StringName = marker.title;

    NSInteger indexCheck = [NameHSArray indexOfObject:marker.title];
    NSLog(@"Curret Index =%ld",(long)indexCheck);

    StringName=[NSString stringWithFormat:@"%@",[NameHSArray objectAtIndex:indexCheck]];
    StringAddress=[NSString stringWithFormat:@"%@",[AddressHSArray objectAtIndex:indexCheck]];
    StringPhoneNo=[NSString stringWithFormat:@"%@",[PhoneNoHSArray objectAtIndex:indexCheck]];


    NSLog(@"Address=%@",StringAddress);
    NSLog(@"Name= %@",StringName);
    NSLog(@"Phone No =%@",StringPhoneNo);

    lblNamepopup.text=StringName;
    lblAddresspopup.text=StringAddress;
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download