I'm working with the strings and i have a little problem that i'm not understanding. In fact, i have a string like this: "ALAsset - Type:Photo, URLs:assets-library://asset/asset.JPG?id=168BA548-9C81-4B08-B69C-B775E5DD9341&ext=JPG" and i need to find the string between "URLs:" and "?id=" . For doing this, i'm trying to create a new string using the NSRange. In this mode i give the first index and the last index that i need, but it seems not working. Here there is my code:

NSString *description = [asset description];
NSRange first = [description rangeOfString:@"URLs:"];
NSRange second = [description rangeOfString:@"?id="];
NSString *path = [description substringWithRange: NSMakeRange(first.location, second.location)];

It return to me this kind of string: "URLs:assets-library://asset/asset.JPG?id=168BA548-9C81-4B08-B69C-B775E5DD9341&ext=JPG". Is it correct? I'm expecting to obtain "assets-library://asset/asset.JPG" string. Where i'm doing wrong? Is there a better way to do this? I have followed this url for help: http://www.techotopia.com/index.php/Working_with_String_Objects_in_Objective-C

Thanks

有帮助吗?

解决方案 2

Don't parse the ALAsset description string! If the description ever changes your code breaks. Use the methods ALAsset and NSURL provide you. First, get the dictionary of URLs (mapped by asset type) through the valueForProperty: method. Then, for each URL, get the absoluteString and remove the query string from it. I got the string you were looking for by placing the following code in the application:didFinishLaunchingWithOptions: method from the single-view iPhone app template.

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) {

}];

其他提示

Try this range: NSMakeRange(first.location + first.length, second.location - (first.location + first.length))

NSRange first = [description rangeOfString:@"URLs:"];

gives you the position of the U, so you need to take first.location+5 to get the start position of assets-library.

NSRangeMake(loc,len) takes a starting location loc and a length, so you need to use second.location-first.location-5 to get the length you are looking for.

Adding it all up, replace the last line with:

NSRange r = NSMakeRange(first.location+5, second.location-first.location-5);
NSString *path = [description substringWithRange:r];
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top