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

Posted

技术标签:

【中文标题】如何从目标C中的数组中获取当前字符串值【英文标题】:How to get the current String Value from array in objective C 【发布时间】:2017-02-28 04:36:43 【问题描述】:

我是 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
    ];

在这个委托中听到我需要获取字符串地址的当前名称。但是听到我得到最后一个值意味着字符串被覆盖。如何从数组中获取单击的值。提前致谢!

【问题讨论】:

对于像这样的每一行:TypeHSArray=[[NSMutableArray alloc] init]; TypeHSArray=[responseletetudedict valueForKey:@"Type"]; alloc/init 是无用的,通常它不是可变的。以小写字母开头命名您的 var。为什么要拥有多个数组?让它们保持同步更难。 @Larme 所有数组都包含不同的值,这就是我使用多个数组的原因。我的数组是动态的,这就是我使用可变数组的原因。 你不明白:如果[responseletetudedict valueForKey:@"Type"]NSArray 而不是NSMutableArray[TypeHSArray addObject:something] 会崩溃。然后你有多个数组,你可能只有一个“主”,因为如果经度和纬度数组不同步会发生什么?都是坏事。我认为您应该进行更智能的解析。 @Larme 我会这样做的。但是我如何从数组中访问字符串我需要在 UILabel 中显示它,就像这样 lblNamepopup.text=StringName; lblAddresspopup.text=StringAddress; 【参考方案1】:

你可以得到如下索引

-(void)mapView:(GMSMapView *)mapView didTapInfoWindowOfMarker:(GMSMarker *)marker

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

--- 编辑 ---

你也可以使用

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

注意:- 您可以通过 sn-p 传递数据,但 sn-p 将数据显示到信息窗口中。因此,您可以创建一个自定义信息窗口并根据需要显示数据。 像这样,

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;

【讨论】:

我还需要从数组到字符串中获取 PhoneNo、FaxNo、Facility 和 Type 的值。 我需要像这样在标签中显示字符串 lblNamepopup.text=StringName; lblAddresspopup.text=StringAddress; 你需要显示一个标记信息窗口的标题和地址。 我需要使用这个方法在 UILabel 上显示名称、地址、电话号码、传真号码、类型 -(void)mapView:(GMSMapView *)mapView didTapInfoWindowOfMarker:(GMSMarker *)marker all in the different label现在合二为一。 NSArray * userData = [NSArray alloc] initWithObjects:NameHSArray[i], AddressHSArray[i],FacilitiesHSArray[i], PhoneNoHSArray[i],FaxnoHSArray[i], TypeHSArray[i], nil] ; marker.userData = userData;

以上是关于如何从目标C中的数组中获取当前字符串值的主要内容,如果未能解决你的问题,请参考以下文章

如何从数组中的字符串数组中获取每个值

如何从返回的 plist 数组中获取字符串

如何分离从表行中获取的值并将其存储在雪花中的数组中

如何从目标 C 中的 NSDictionary 中检索字符串?

如何从 React 中的数组中获取值? [关闭]

C# JSON 反序列化:如何从 JSON 对象数组中获取值 [重复]