“自我”超级之前使用。init调用

时间:2021-04-24 04:27:55

I'm new to swift and I don't understand how to initialize a class.

我是swift的新手,我不知道如何初始化一个类。

Succeeded is initialized in the class definition as false

在类定义中,success被初始化为false

if (succeeded && (time>1000)){
   errormessage += ";connection slow"
 }

Time is initialized as

时间初始化

time = data[3].toInt()

Where data is

数据在哪里

var data = split(raw_data) {$0 == ","}

And raw_data is a string.

而raw_data是一个字符串。

Class Definition:

类定义:

class geocodeObject: NSObject {

init definition:

初始化定义:

init(lat: String,long:String,userstate:String) {

(no super init of any kind) EDIT: Full code with things cut way

(没有任何类型的超级init)编辑:完整的代码和东西剪切方式

class geocodeObject: NSObject {
//A type to store the data from the Reverse Geocoding API
//Not a retriever

//Options
let API_KEY_TEXAS = "9e4797c018164fdcb9a95edf3b10ccfc"
let DEV_MODE = true
//Loading status
var succeeded = false
var errormessage = "Not Initalized" //Not nesscarilly a failure, could be slow connection
var loadstate: String?
//Most important info
var street: String?; var housenumber: String?; var city: String?; var zip: String?
//Metadata
var time: IntegerLiteralType?; var statuscode: String?; var queryid: String?; var zip4: String?
//Other geographical data
var entirestreet: String?; var state: String?
init(lat: String,long:String,userstate:String) {
    //userstate: State provided by user
    //state: State provided by Reverse Geocoder

    var url: String?
    var extra: String?
    if DEV_MODE{
        extra = "&notStore=true"
    }
    else{
        extra = ""
    }
    url = "http://geoservices.tamu.edu/Services/ReverseGeocoding/WebService/v04_01/HTTP/default.aspx?lat="+lat+"&lon="+long+"&apikey="+API_KEY_TEXAS+"&version=4.01"
    if (userstate == "nil"){
        url = url! + extra!
        println("if")
    }
    else{
        url = url! + "&state="+state!+extra!
        println("else")
    }

    let raw_data = retrieveurl(url!)
    var data = split(raw_data) {$0 == ","}
    //data[1] is API version used.
    statuscode = data[0]; queryid = data[2]; time = data[3].toInt(); entirestreet = data[4]; city = data[5]
    state = data[6]; zip = data[7]; zip4 = data[8]
    //Do street, housenumber, errormessage, succeeded
    if (state != userstate){
        println("user state not equal to state")
    }
    var splittedstreet = split(entirestreet!){$0 == " "}
    housenumber = splittedstreet[0]
    street = splittedstreet[1]
    println(statuscode)
    //Error message handling
    switch String(statuscode!){
        case "200":
            errormessage = "Success"
        case "400":
            errormessage = "Unknown API key error"
        case "401":
            ...
    //Time handling
    if (succeeded && (time>1000)){
        errormessage += ";connection slow"
    }
   }
    println("/GeocodingAPIWrapper.swift/.geocodeObject.init: Not Implemented")
}

}

}

3 个解决方案

#1


16  

It had been a while but the answer I found is that you should add super.init() as the first line inside your init block

已经有一段时间了,但是我找到的答案是您应该将super.init()添加为init块中的第一行

init(lat: String,long:String,userstate:String) {
    super.init()
    ...

This way I got rid of it and fulfills what the error is asking for. As I understand this is that your variables are initialized during NSObject.init() so you can use assigned values inside your custom init(_) block

这样我就摆脱了它,并满足了错误的要求。正如我所理解的,您的变量在NSObject.init()中初始化,因此您可以在自定义的init(_)块中使用赋值

#2


22  

Swift 2.2 (still beta as of writing) currently displays this error if you accidentally forget to return nil from a guard's else:

如果您不小心忘记从一个警卫的else中返回nil,那么Swift 2.2(仍然是beta版本)当前将显示此错误:

required init?(dictionary: [String: AnyObject]) {

    guard let someValue = dictionary["someValue"] as? Bool else { return /*nil*/ } //Nil should not be commented here
    self.someValue = someValue

    super.init(dictionary: dictionary) //`self` used before super.init call
}

Hopefully this helps someone

希望这可以帮助别人

#3


-2  

Use:

使用:

var obj = geocodeObject(lat: "0.0",long:"0.0",userstate:"aaa")

var obj = geocodeObject(lat:“0.0”,long:“0.0”,userstate:“aaa”)

#1


16  

It had been a while but the answer I found is that you should add super.init() as the first line inside your init block

已经有一段时间了,但是我找到的答案是您应该将super.init()添加为init块中的第一行

init(lat: String,long:String,userstate:String) {
    super.init()
    ...

This way I got rid of it and fulfills what the error is asking for. As I understand this is that your variables are initialized during NSObject.init() so you can use assigned values inside your custom init(_) block

这样我就摆脱了它,并满足了错误的要求。正如我所理解的,您的变量在NSObject.init()中初始化,因此您可以在自定义的init(_)块中使用赋值

#2


22  

Swift 2.2 (still beta as of writing) currently displays this error if you accidentally forget to return nil from a guard's else:

如果您不小心忘记从一个警卫的else中返回nil,那么Swift 2.2(仍然是beta版本)当前将显示此错误:

required init?(dictionary: [String: AnyObject]) {

    guard let someValue = dictionary["someValue"] as? Bool else { return /*nil*/ } //Nil should not be commented here
    self.someValue = someValue

    super.init(dictionary: dictionary) //`self` used before super.init call
}

Hopefully this helps someone

希望这可以帮助别人

#3


-2  

Use:

使用:

var obj = geocodeObject(lat: "0.0",long:"0.0",userstate:"aaa")

var obj = geocodeObject(lat:“0.0”,long:“0.0”,userstate:“aaa”)