使用 NSDictionary 中的 url 方案打开特定路径

发布于 2024-09-26 02:05:59 字数 2594 浏览 4 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

离去的眼神 2024-10-03 02:05:59

您可能需要提供更多有关您正在尝试执行的操作的信息。例如,您没有说明如何检索包含所有商店详细信息的字典以及 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
}

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:

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
}
静谧 2024-10-03 02:05:59

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

You could use a NSPredicate to select the shop(s) you are looking for:

    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];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文