Swift - Data Type Using NSString

Introduction

You can declare a variable explicitly as an NSString type:

var str2:NSString = "This is a NSString in Objective-C. "

Here, str2 will now be an NSString instance.

The statement can also be rewritten as follows:

var str2 = "This is a NSString in Objective-C. " as NSString

You can call all the NSString methods directly via str2 :

Demo

var str2 = "This is a NSString in Objective-C. " as NSString

print(str2.length)                       
print(str2.containsString("NSString"))   
print(str2.hasPrefix("This"))            
print(str2.hasSuffix(". "))              
print(str2.uppercaseString)    // ww w .j a  va2  s  . c om
print(str2.lowercaseString)    
print(str2.capitalizedString)
print(str2.stringByAppendingString("Yeah!"))
print(str2.stringByAppendingFormat("This is a number: %d", 123))

Related Topic