简体   繁体   English

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

[英]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? 我想在Swift中使用NSArrayarrayWithContentsOfFile方法,因为我必须根据文件的内容创建一个Swift Array,但是如何在Swift代码中调用它呢?

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 . 由于arrayWithContentsOfFile:在实现一个类的方法Objective-C的内置NSArray类型,我不能把它叫这个帖子 ,它通过型坯连铸斯威夫特的调用一个实例方法Array以Objective-C的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") 可以像下面这样在Swift中调用该方法: NSArray(contentsOfFile: "PATH")

Using the method like so: NSArray.arrayWithContentsOfFile("PATH") is deprecated. 使用如下方法:不推荐使用NSArray.arrayWithContentsOfFile("PATH")

This is a constructor, and should be used in the following manner: 这是一个构造函数,应按以下方式使用:

var array = NSArray(contentsOfFile: "PATH")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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