Objective-C-使用字符串,使用NSRange查找子字符串

Objective-C-使用字符串,使用NSRange查找子字符串,objective-c,string,nsstring,nsrange,Objective C,String,Nsstring,Nsrange,我正在处理字符串,我有一个小问题,我不明白。事实上,我有这样一个字符串:ALAsset-Type:Photo,url:assets-library://asset/asset.JPG?id=168BA548-9C81-4B08-B69C-B775E5DD9341&ext=JPG,我需要找到URL:和?id=”之间的字符串。为此,我尝试使用NSRange创建一个新字符串。在这种模式下,我给出了我需要的第一个索引和最后一个索引,但它似乎不起作用。 这是我的代码: NSString *descript

我正在处理字符串,我有一个小问题,我不明白。事实上,我有这样一个字符串:ALAsset-Type:Photo,url:assets-library://asset/asset.JPG?id=168BA548-9C81-4B08-B69C-B775E5DD9341&ext=JPG,我需要找到URL:和?id=”之间的字符串。为此,我尝试使用NSRange创建一个新字符串。在这种模式下,我给出了我需要的第一个索引和最后一个索引,但它似乎不起作用。 这是我的代码:

NSString *description = [asset description];
NSRange first = [description rangeOfString:@"URLs:"];
NSRange second = [description rangeOfString:@"?id="];
NSString *path = [description substringWithRange: NSMakeRange(first.location, second.location)];
它会向我返回这样的字符串:url:assets-library://asset/asset.JPG?id=168BA548-9C81-4B08-B69C-B775E5DD9341&ext=JPG。对吗?我希望获得资产-library://asset/asset.JPG 一串 我哪里做错了?有更好的方法吗? 我已按照此url寻求帮助:http://www.techotopia.com/index.php/Working_with_String_Objects_in_Objective-C

谢谢

为您提供U的位置,因此您需要先获取。位置+5以获取资源库的起始位置

NSRangeMakeloc,len获取起始位置loc和长度,因此需要使用second.location-first.location-5来获取所需的长度

加起来,将最后一行替换为:

NSRange r = NSMakeRange(first.location+5, second.location-first.location-5);
NSString *path = [description substringWithRange:r];
为您提供U的位置,因此您需要先获取。位置+5以获取资源库的起始位置

NSRangeMakeloc,len获取起始位置loc和长度,因此需要使用second.location-first.location-5来获取所需的长度

加起来,将最后一行替换为:

NSRange r = NSMakeRange(first.location+5, second.location-first.location-5);
NSString *path = [description substringWithRange:r];
尝试以下范围: NSMakeRangefirst.location+first.length,second.location-first.location+first.length

尝试以下范围: NSMakeRangefirst.location+first.length,second.location-first.location+first.length

不要解析ALAsset描述字符串!如果描述发生变化,代码将中断。使用ALAsset和NSURL提供的方法。首先,通过valueForProperty:方法获取按资产类型映射的URL字典。然后,对于每个URL,获取absoluteString并从中删除查询字符串。通过在应用程序中放置以下代码,我得到了您想要的字符串:来自单视图iPhone应用程序模板的didFinishLaunchingWithOptions:method

ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
[library enumerateGroupsWithTypes:ALAssetsGroupAlbum usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
   [group enumerateAssetsUsingBlock:^(ALAsset *asset, NSUInteger index, BOOL *stop) {
       NSDictionary *URLDictionary = [asset valueForProperty:ALAssetPropertyURLs];
       for (NSURL *URL in [URLDictionary allValues]) {
           NSString *URLString = [URL absoluteString];
           NSString *query = [URL query];
           if ([query length] > 0) {
               NSString *toRemove = [NSString stringWithFormat:@"?%@",query];
               URLString = [URLString stringByReplacingOccurrencesOfString:toRemove withString:@""];
               NSLog(@"URLString = %@", URLString);
           }
       }
   }];
} failureBlock:^(NSError *error) {

}];
不要解析ALAsset描述字符串!如果描述发生变化,代码将中断。使用ALAsset和NSURL提供的方法。首先,通过valueForProperty:方法获取按资产类型映射的URL字典。然后,对于每个URL,获取absoluteString并从中删除查询字符串。通过在应用程序中放置以下代码,我得到了您想要的字符串:来自单视图iPhone应用程序模板的didFinishLaunchingWithOptions:method

ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
[library enumerateGroupsWithTypes:ALAssetsGroupAlbum usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
   [group enumerateAssetsUsingBlock:^(ALAsset *asset, NSUInteger index, BOOL *stop) {
       NSDictionary *URLDictionary = [asset valueForProperty:ALAssetPropertyURLs];
       for (NSURL *URL in [URLDictionary allValues]) {
           NSString *URLString = [URL absoluteString];
           NSString *query = [URL query];
           if ([query length] > 0) {
               NSString *toRemove = [NSString stringWithFormat:@"?%@",query];
               URLString = [URLString stringByReplacingOccurrencesOfString:toRemove withString:@""];
               NSLog(@"URLString = %@", URLString);
           }
       }
   }];
} failureBlock:^(NSError *error) {

}];

你为什么要阅读techtopia这样的网站?您使用NSString所需的所有信息均可通过apple.com获得。它有大量关于如何使用字符串的信息。你为什么要阅读techtopia这样的网站?您使用NSString所需的所有信息均可通过apple.com获得。它有大量关于如何使用字符串的信息。