如何从Swift内调用Objective-C的NSArray类方法?

如何从Swift内调用Objective-C的NSArray类方法?

问题描述:

我想在Swift中使用 NSArray arrayWithContentsOfFile 方法,因为我必须从文件的内容,但是如何从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?

由于 arrayWithContentsOfFile:是一个类在Objective-C内置的 NSArray 类型中实现的方法,我不能像这篇文章,它通过类型转换Swift的 Array 到Objective-C的 NSArray

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?

可以在Swift中这样调用该方法:
NSArray(contentsOfFile: PATH)

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

使用这样的方法: NSArray.arrayWithContentsOfFile( 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")