简体   繁体   中英

How to call Objective-C's NSArray class method from within Swift?

I want to use NSArray 's arrayWithContentsOfFile method in Swift since I have to create an Swift Array from the contents of a file, but how can I call it from within Swift code?

Since arrayWithContentsOfFile: is a class method implemented in Objective-C's built-in NSArray type, I cannot call it as called in this post , which calls an instance method by type-casting the Swift's Array to Objective-C's NSArray .

So is there any way to call the method, or any equivalent method like that?

The method can be called in Swift like so: NSArray(contentsOfFile: "PATH")

Using the method like so: NSArray.arrayWithContentsOfFile("PATH") is deprecated.

This is a constructor, and should be used in the following manner:

var array = NSArray(contentsOfFile: "PATH")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM