2010-10-03 51 views
1

我使用TouchJSON從http://enbr.co.cc/TrailsApp/shops.php中檢索JSON響應。在我的應用程序中,我使用此代碼來處理url方案。使用來自NSDictionary的網址方案打開特定路徑

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

我如何可以把從我的NSDictionary從JSON創建基於NSString的名字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"; 
     } 
    ); 
} 

回答

0

您可能需要提供一些關於您正在嘗試做什麼的更多信息。例如,您不會說如何檢索包含所有商店的詳細信息的字典以及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 
} 
0

你可以使用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];