Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert NSURL to String in Swift

Tags:

swift

nsurl

How do you convert an NSURL to a String in Swift?

The following:

var directoryURL: NSURL var urlString: String = nsurlObject as String 

Throws the error:

Cannot convert value of type 'NSURL' to type 'String' in coercion

like image 733
JBaczuk Avatar asked Nov 03 '15 22:11

JBaczuk


Video Answer


1 Answers

It turns out there are properties of NSURL you can access (see Swift Reference):

var directoryURL: NSURL var urlString: String = directoryURL.absoluteString // OR var urlString: String = directoryURL.relativeString // OR var urlString: String = directoryURL.relativePath // OR var urlString: String = directoryURL.path // etc. 
like image 115
JBaczuk Avatar answered Sep 20 '22 06:09

JBaczuk