使用 NSDictionary 中的 url 方案打开特定路径
我正在使用 TouchJSON 从 http://enbr.co.cc/TrailsApp/shops 检索 JSON 响应.php。在我的应用程序中,我使用此代码来处理 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";
}
);
}
I am using TouchJSON to retrieve the JSON response from http://enbr.co.cc/TrailsApp/shops.php. In my app I use this code to handle a url scheme.
- (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;
}
How can I push the correct entry from my NSDictionary created from the JSON to the ShopViewController based on the NSString name? Here is my dictionary printed out by NSLog with NSLog(@"%@", myObj);. Thanks in advance.
{
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";
}
);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可能需要提供更多有关您正在尝试执行的操作的信息。例如,您没有说明如何检索包含所有商店详细信息的字典以及 ShopViewController 如何访问该字典。但是可以通过以下方式从字典中按名称选择一家商店:
You probably need to give a bit more information about what you are trying to do. For instance you don't say how you retrieve the dictionary containing the details of all the shops and how ShopViewController has access to this dictionary. But selecting one shop by name from the dictionary can be done with something like this:
您可以使用 NSPredicate 来选择您正在寻找的商店:
You could use a NSPredicate to select the shop(s) you are looking for: