如何从 objective C 中的数组获取当前字符串值

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

我是 iOS 的新手,我面临着从数组中获取字符串当前值的问题。

我的代码是这样的

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;

        }

在我点击诺贝尔医院时添加图像我打电话给代表

  - (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
    }];
}

听说这个委托我需要获取字符串的当前名称address.But听说我正在获取最后一个值意味着字符串被覆盖。我怎样才能得到我从数组中点击的值。提前致谢!

您可以得到索引如下

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

--- 编辑 ---

您也可以使用

  • 我从图书馆找到了一些参考资料。
  • 请注意,userData 不应包含对任何地图的任何强引用
  • objects,否则可能会造成循环(阻止ARC释放 对象)。

注意:- 您可以通过代码段传递数据,但代码段将数据显示为信息 window。所以你创建了一个自定义信息 window 并根据需要显示数据。
像这样,

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

检索
像这样,

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

你可以这样做

- (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;
}