将NSURL转换为NSString。

时间:2022-02-21 00:14:57

I have an app where the user can choose an image either from the built-in app images or from the iphone photo library. I use an object Occasion that has an NSString property to save the imagePath.

我有一个应用程序,用户可以从内置的应用程序图像或iphone照片库中选择一个图像。我使用具有NSString属性的对象场景来保存imagePath。

Now in the case of the built-in app images I do get the file name as an NSString an save in the [occasion imagePath]. But in the 2nd case where the user picks an image form the photo library I get an NSURL which I want to convert to an NSString to be able to save it in [occasion imagePath].

现在,在内置的应用程序映像中,我确实将文件名作为NSString,保存在[场景imagePath]中。但是在第二种情况下,用户选择一个图像格式的照片库我得到一个NSURL,我想要转换成NSString,以便在[场景imagePath]中保存它。

Is it possible to convert the NSURL to an NSString?

是否可以将NSURL转换为NSString?

6 个解决方案

#1


639  

NSString *myString = myURL.absoluteString;

More info in the docs:

更多信息在文档:

#2


123  

If you're interested in the pure string:

如果你对纯字符串感兴趣:

[myUrl absoluteString];

If you're interested in the path represented by the URL (and to be used with NSFileManager methods for example):

如果您对URL表示的路径感兴趣(并使用NSFileManager方法):

[myUrl path];

#3


42  

Try this in Swift :

试试这个吧:

var urlString = myUrl.absoluteString

Objective-C:

objective - c:

NSString *urlString = [myURL absoluteString];

#4


16  

Swift update:

迅速更新:

var myUrlStr : String = myUrl.absoluteString

#5


1  

I just fought with this very thing and this update didn't work.

我只是和这个东西斗争,这个更新没用。

This eventually did in Swift:

这最终在斯威夫特:

let myUrlStr : String = myUrl!.relativePath!

#6


1  

You can use any one way

你可以用任何一种方法。

NSString *string=[NSString stringWithFormat:@"%@",url1];

or

NSString *str=[url1 absoluteString];

NSLog(@"string :: %@",string);

string :: file:///var/containers/Bundle/Application/E2D7570B-D5A6-45A0-8EAAA1F7476071FE/RemoDuplicateMedia.app/loading_circle_animation.gif

字符串::文件:/ / / var /集装箱/包/应用程序/ E2D7570B-D5A6-45A0-8EAAA1F7476071FE / RemoDuplicateMedia.app / loading_circle_animation.gif

NSLog(@"str :: %@", str);

str :: file:///var/containers/Bundle/Application/E2D7570B-D5A6-45A0-8EAA-A1F7476071FE/RemoDuplicateMedia.app/loading_circle_animation.gif

str::文件:/ / / var /集装箱/包/应用程序/ E2D7570B-D5A6-45A0-8EAA-A1F7476071FE / RemoDuplicateMedia.app / loading_circle_animation.gif

#1


639  

NSString *myString = myURL.absoluteString;

More info in the docs:

更多信息在文档:

#2


123  

If you're interested in the pure string:

如果你对纯字符串感兴趣:

[myUrl absoluteString];

If you're interested in the path represented by the URL (and to be used with NSFileManager methods for example):

如果您对URL表示的路径感兴趣(并使用NSFileManager方法):

[myUrl path];

#3


42  

Try this in Swift :

试试这个吧:

var urlString = myUrl.absoluteString

Objective-C:

objective - c:

NSString *urlString = [myURL absoluteString];

#4


16  

Swift update:

迅速更新:

var myUrlStr : String = myUrl.absoluteString

#5


1  

I just fought with this very thing and this update didn't work.

我只是和这个东西斗争,这个更新没用。

This eventually did in Swift:

这最终在斯威夫特:

let myUrlStr : String = myUrl!.relativePath!

#6


1  

You can use any one way

你可以用任何一种方法。

NSString *string=[NSString stringWithFormat:@"%@",url1];

or

NSString *str=[url1 absoluteString];

NSLog(@"string :: %@",string);

string :: file:///var/containers/Bundle/Application/E2D7570B-D5A6-45A0-8EAAA1F7476071FE/RemoDuplicateMedia.app/loading_circle_animation.gif

字符串::文件:/ / / var /集装箱/包/应用程序/ E2D7570B-D5A6-45A0-8EAAA1F7476071FE / RemoDuplicateMedia.app / loading_circle_animation.gif

NSLog(@"str :: %@", str);

str :: file:///var/containers/Bundle/Application/E2D7570B-D5A6-45A0-8EAA-A1F7476071FE/RemoDuplicateMedia.app/loading_circle_animation.gif

str::文件:/ / / var /集装箱/包/应用程序/ E2D7570B-D5A6-45A0-8EAA-A1F7476071FE / RemoDuplicateMedia.app / loading_circle_animation.gif