What are these NS prefixed types?

In many Swift examples out there, e.g. Stackoverflow, I find the usage of NS prefixed types. E.g. in this example:

func listFilesFromDocumentsFolder() -> [NSString]?{
    var theError = NSErrorPointer()
    let dirs = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.AllDomainsMask, true) as? [String]
    if dirs != nil {
        let dir = dirs![0] as NSString
        let fileList = NSFileManager.defaultManager().contentsOfDirectoryAtPath(dir, error: theError) as [NSString]
        return fileList
    }else{
        return nil
    }
}

What are these? Coming from a special framework?

Those are Types that come from Apple's Cocoa frameworks. They are basically "Legacy" for Swift, as Apple's frameworks are written in Objective-C. For many of them you will find a native Swift type without the NS-prefix, and some of them can be used interchangeably.

There used to be a free book about that topic by Apple as well, but it seems to have been abandoned :frowning:

See these links for more information:

https://developer.apple.com/documentation/swift/imported_c_and_objective-c_apis/working_with_foundation_types

Terms of Service

Privacy Policy

Cookie Policy