Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何将自定义初始化函数从ObjC转换为Swift_Ios_Swift - Fatal编程技术网

Ios 如何将自定义初始化函数从ObjC转换为Swift

Ios 如何将自定义初始化函数从ObjC转换为Swift,ios,swift,Ios,Swift,如何将此类的代码转换为Swift .h文件 .m文件 未测试,但类似于以下内容: var coordinate:CLLocationCoordinate2D func initWithCoordinate(coordinate: CLLocationCoordinate2D) -> instancetype{ super.init() self.coordinate = coordinate return self } 您可以这样

如何将此类的代码转换为Swift

.h文件 .m文件
未测试,但类似于以下内容:

var coordinate:CLLocationCoordinate2D 

    func initWithCoordinate(coordinate: CLLocationCoordinate2D) -> instancetype{
      super.init() 
      self.coordinate = coordinate
      return self
    }

您可以这样做:

var coordinate : CLLocationCoordinate2D
 // your init method
 init (2Dcoordinate : CLLocationCoordinate2D) {
  coordinate = 2Dcoordinate
 }
 //required init method 
 required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

下面的代码可能对您有所帮助

   import CoreLocation

    class YOURCLASSNAME: NSObject {
        var coordinate : CLLocationCoordinate2D
        init(coordinate: CLLocationCoordinate2D) {
            self.coordinate = coordinate
            super.init()
        }
        convenience override init() {
            self.init(coordinate: CLLocationCoordinate2DMake(100.00, 100)) // calls above mentioned controller with default name
        }
    }

请在链接上查找更多信息。

你所说的“转换”是什么意思?我想将此代码写入swift。为什么不发布您尝试的内容?nsobject类不使用CLLocationCoordinate2D对象。您应该清楚您尝试实现的目标,而不是要求人们转换代码。你所做的事情的目的是什么?这是问题的一个重要部分。你必须在swift中创建一个与我在问题中发布的上述内容相同的对象类。它对我不起作用。请检查nsobject类中的以下方法,它在第一行显示错误-“使用未声明的类型”它对我来说是很好的。你导入了基础,并且你能清楚地知道这个错误,因为我一直在使用它,请正确地导入CaleloCaskOffo关于返回值,而使用init创建对象,你可以使用LET或var存储它,因此你可以使用它Furthur.…
var coordinate : CLLocationCoordinate2D
 // your init method
 init (2Dcoordinate : CLLocationCoordinate2D) {
  coordinate = 2Dcoordinate
 }
 //required init method 
 required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}
   import CoreLocation

    class YOURCLASSNAME: NSObject {
        var coordinate : CLLocationCoordinate2D
        init(coordinate: CLLocationCoordinate2D) {
            self.coordinate = coordinate
            super.init()
        }
        convenience override init() {
            self.init(coordinate: CLLocationCoordinate2DMake(100.00, 100)) // calls above mentioned controller with default name
        }
    }