【问题标题】:Open specific trail with a url scheme from NSDictionary使用来自 NSDictionary 的 url 方案打开特定路径
【发布时间】:2010-10-03 17:57:07
【问题描述】:

我正在使用 TouchJSON 从http://enbr.co.cc/TrailsApp/shops.php 检索 JSON 响应。在我的应用程序中,我使用此代码来处理 url 方案。

- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
{
    if (!url) {
        return NO;
    }
    NSString *urlString = [url absoluteString];
    NSString *urlStringDecoded = [urlString stringByReplacingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
    NSArray *list = [urlStringDecoded componentsSeparatedByString:@"="];
    NSString *urlPrefix = [list objectAtIndex:0];
    NSString *name = [list objectAtIndex:1];
    if ([urlPrefix isEqualToString:@"tridetrails://opentrail?name"]) {
        TrailViewController *trailViewController = [[TrailViewController alloc] initWithNibName:@"TrailViewController" bundle:[NSBundle mainBundle]];
        trailViewController.trailToGoto = name;
        [self.navigationController pushViewController:trailViewController animated:YES];
        [trailViewController release];
    }
    if ([urlPrefix isEqualToString:@"tridetrails://openshop?name"]) {
        ShopViewController *shopViewController = [[ShopViewController alloc] initWithNibName:@"ShopViewController" bundle:[NSBundle mainBundle]];
        shopViewController.shopToGoto = name;
        [self.navigationController pushViewController:shopViewController animated:YES];
        [shopViewController release];
    }
    return YES;
}

如何根据 NSString 名称将正确的条目从由 JSON 创建的 NSDictionary 推送到 ShopViewController?这是 NSLog 用 NSLog(@"%@", myObj); 打印出来的我的字典。提前致谢。

{
    shops =     (
                {
            blurb = "Bootdoctors blurb";
            image = bootdoctorslogo;
            locations = "Mountain Village";
            motto = "Bootdoctors shop motto";
            name = Bootdoctors;
        },
                {
            blurb = "Easy Rider blurb";
            image = easyriderlogo;
            locations = Telluride;
            motto = "Easy Rider shop motto";
            name = "Easy Rider";
        },
                {
            blurb = "Paragon Ski & Sport blurb";
            image = paragonskiandsportlogo;
            locations = Telluride;
            motto = "Paragon shop motto";
            name = "Paragon Ski & Sport";
        },
                {
            blurb = "Telluride Sports blurb";
            image = telluridesportslogo;
            locations = "Telluride and Mountain Village";
            motto = "Telluride Sports shop motto";
            name = "Telluride Sports";
        }
    );
}

【问题讨论】:

    标签: iphone json url nsdictionary


    【解决方案1】:

    您可能需要提供更多有关您正在尝试做的事情的信息。例如,您没有说明如何检索包含所有商店详细信息的字典以及 ShopViewController 如何访问该字典。但是从字典中按名称选择一家商店可以通过以下方式完成:

    NSDictionary *jsonResponse; // You don't say how the ShopViewController has access to
                                // the response so let's just assume a local variable here.
    
    NSDictionary *foundShop = nil; // This will be selected shop after the search below
    
    NSArray *shops = [jsonResponse objectForKey:@'shops'];
    
    for (NSDictionary *shop in shops) {
        if ([shop objectForKey:@'name'] isEqualToString:self.shopToGoto]) {
            foundShop = shop;
            break;
        }
    }
    
    if (foundShop) {
        // Do something with the dictionary keys and values in foundShop
    }
    else {
        // Error condition - shop with required name is not present
        // Handle error
    }
    

    【讨论】:

      【解决方案2】:

      您可以使用 NSPredicate 来选择您正在寻找的商店:

          NSString* shopName = ...;
      
          NSArray* shops = ...;  // this is your JSON-produced array of NSDictionary Shop objects
      
          NSPredicate* predicate = [NSPredicate predicateWithFormat: @"name == '%@'", shopName ];
      
          NSArray* matchingShops = [shops filteredArrayUsingPredicate: predicate];
      
          NSDictionary* firstMatchingShop = [matchingShops objectAtIndex: 0];
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-07-13
        • 2018-10-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-03-13
        相关资源
        最近更新 更多