Swift - Casting String to NSString

Introduction

You can explicitly convert the String instance to an NSString instance using the as keyword:

Demo

var str1 = "This is a Swift string"
print((str1 as NSString).containsString("Swift"))  //true

Once you have converted the String instance to an NSString instance, you can call the containsString() method.

To get string length.

Demo

var str1 = "This is a Swift string"
print((str1 as NSString ).length)  //22

Related Topic