2010-10-03 11 views
1

J'utilise TouchJSON pour récupérer la réponse JSON de http://enbr.co.cc/TrailsApp/shops.php. Dans mon application, j'utilise ce code pour gérer un schéma d'URL.Ouvrir un chemin spécifique avec un schéma d'URL de 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; 
} 

Comment puis-je pousser l'entrée correcte de mon NSDictionary créé à partir du JSON au ShopViewController en fonction du nom NSString? Voici mon dictionnaire imprimé par NSLog avec NSLog (@ "% @", myObj) ;. Merci d'avance.

{ 
    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"; 
     } 
    ); 
} 

Répondre

0

Vous avez probablement besoin de donner un peu plus d'informations sur ce que vous essayez de faire. Par exemple, vous ne dites pas comment vous récupérez le dictionnaire contenant les détails de toutes les boutiques et comment ShopViewController a accès à ce dictionnaire. Mais la sélection d'un magasin par nom du dictionnaire peut être fait avec quelque chose comme ceci:

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

Vous pouvez utiliser un NSPredicate pour sélectionner que vous cherchez le magasin (s):

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]; 
Questions connexes