ios 将 NSURL 转换为 NSString

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8082719/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 15:19:27  来源:igfitidea点击:

Convert an NSURL to an NSString

iosobjective-cswiftnsstringnsurl

提问by Ali

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 NSStringproperty to save the imagePath.

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

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

现在,对于内置应用程序图像,我确实将文件名NSString保存在[occasion imagePath]. 但是在用户从照片库中选择图像的第二种情况下,我得到一个NSURL我想转换为一个NSString以便能够将其保存在[occasion imagePath] 中的图像。

Is it possible to convert the NSURLto an NSString?

是否可以将 转换NSURLNSString

回答by Randall

In Objective-C:

在 Objective-C 中:

NSString *myString = myURL.absoluteString;

In Swift:

在斯威夫特:

var myString = myURL.absoluteString

More info in the docs:

文档中的更多信息:

回答by viggio24

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 NSFileManagermethods for example):

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

[myUrl path];

回答by beryllium

Try this in Swift :

在 Swift 中试试这个:

var urlString = myUrl.absoluteString

Objective-C:

目标-C:

NSString *urlString = [myURL absoluteString];

回答by kmiklas

Swift update:

迅捷更新:

var myUrlStr : String = myUrl.absoluteString

回答by Speedz

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

我只是与这件事作斗争,但此更新不起作用。

This eventually did in Swift:

这最终在 Swift 中做到了:

let myUrlStr : String = myUrl!.relativePath!

回答by Shaik Thuphel

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

字符串:: file:///var/containers/Bundle/Application/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 :: file:///var/containers/Bundle/Application/E2D7570B-D5A6-45A0-8EAA-A1F7476071FE/RemoDuplicateMedia.app/loading_circle_animation.gif

回答by kangna sharma

In Swift :- var str_url = yourUrl.absoluteString

在 Swift 中:- var str_url = yourUrl.absoluteString

It will result a url in string.

它将导致字符串中的 url。