如何获得此NSURL的ID部分?

时间:2013-10-19 10:41:10

标签: iphone ios nsurl

打印时,我有NSURL看起来像这样 控制台

<AVURLAsset: 0x170232800, URL = assets-library://asset/asset.MOV?id=426C5913-86CF-4476-2DB6-6A9AAC626AF4&ext=MOV>

获取此网址的ID部分的正确方法是什么?目标是使用426C5913-86CF-4476-2DB6-6A9AAC626AF4字符串。

2 个答案:

答案 0 :(得分:3)

您可以使用以下内容:

NSURL *url = [[NSURL alloc] initWithString:@"assets-library://asset/asset.MOV?id=426C5913-86CF-4476-2DB6-6A9AAC626AF4&ext=MOV"];
NSArray *components = [[url query] componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"=&"]];
NSMutableDictionary *params = [NSMutableDictionary dictionary];
for (NSUInteger idx = 1; idx < components.count; idx+=2) {
    params[components[idx - 1]] = components[idx];
}

NSLog(@"params[@id] = %@", params[@"id"]);

为此目的创建一个NSURL类别是个好主意。

答案 1 :(得分:0)

开始于:

NSURL *urlAddress = [NSURL URLWithString:yourAddressString];

然后:

[urlAddress scheme] gives the Scheme;
[urlAddress host] gives the host
[urlAddress path], [urlAddress relativePath], [urlAddress query], [urlAddress fragment] or [urlAddress parameterString] gives the correspondent parts of your urlAddress