1

What am I doing wrong? I have a Class with a variable that is a struct and that works perfectly. However I also have a variable within the struct that is of type another struct. When I declare that variable (the second level struct) it keeps asking me to provide the variables.. I just want them blank and I will set them or send them values later.

ERROR TEXT:

Missing argument for parameter 'SV_StinkLocation_Longitude' in call

Calling Code:

struct SV_StinkDetails_Model{
    var SV_StinkDetails_Description:String = ""
    var SV_StinkDetails_RecordingDate:NSDate! = NSDate()
    var SV_StinkDetails_Location:SV_StinkLocation_Model! = SV_StinkLocation_Model()
}

ERROR LINE

var SV_StinkDetails_Location:SV_StinkLocation_Model! = SV_StinkLocation_Model()

Struct Code:

import Foundation
import UIKit
import CoreData
import AVFoundation
import CoreLocation


struct SV_StinkLocation_Model{  
    var SV_StinkLocation_Longitude:NSNumber!
    var SV_StinkLocation_Latitude:NSNumber!
    var SV_StinkLocation_Suburb:String!
    var SV_StinkLocation_State:String!
    var SV_StinkLocation_Country:String!
}

However it works fine when I make the details struct a class variable.

var SV_Stink_StinkDetails:SV_StinkDetails_Model! = SV_StinkDetails_Model()

What am I doing wrong?

1 Answer 1

1

I would advice to change your Model struct to:

struct SV_StinkDetails_Model{
    var SV_StinkDetails_Description:String
    var SV_StinkDetails_RecordingDate:NSDate
    var SV_StinkDetails_Location :SV_StinkLocation_Model?
}

You don’t have to declare values since you use the default init (neither do you need to declare them if you use a custom init)
In the struct I placed above your Location_Model is a optional, this means you can set the initial value to “nil”. Later you can add this info. Remember to check (with a “if let” statement) if the value is set whenever you try to access it though!

For added value:
You don’t need to put the “!” behind your type declarations in the Model struct.

If you change these 2 things you can get your new class using:

import Foundation
import UIKit
import CoreData
import AVFoundation
import CoreLocation

struct SV_StinkLocation_Model{
    var SV_StinkLocation_Longitude:NSNumber
    var SV_StinkLocation_Latitude:NSNumber
    var SV_StinkLocation_Suburb:String
    var SV_StinkLocation_State:String
    var SV_StinkLocation_Country:String
}

struct SV_StinkDetails_Model{
    var SV_StinkDetails_Description:String
    var SV_StinkDetails_RecordingDate:NSDate
    var SV_StinkDetails_Location :SV_StinkLocation_Model?
}

var details_model = SV_StinkDetails_Model(SV_StinkDetails_Description: "Description:", SV_StinkDetails_RecordingDate: NSDate(), SV_StinkDetails_Location: nil)

details_model.SV_StinkDetails_Location = SV_StinkLocation_Model(SV_StinkLocation_Longitude: 1, SV_StinkLocation_Latitude: 1, SV_StinkLocation_Suburb: “Suburb", SV_StinkLocation_State: "State", SV_StinkLocation_Country: "Country")

Lastly you can access let’s say the longtitude of the option value using:

if let longtitude =  details_model.SV_StinkDetails_Location?.SV_StinkLocation_Longitude {
    println(longtitude)
}
Sign up to request clarification or add additional context in comments.

4 Comments

Absolute Legend Thanks. I am still finding it hard to get my head around the Bloody Optional factor in Swift. Really appreciate you answer @milo526
How can I call the function in my Location Struct?
The if let statement could be used here ones more. You could check if a optioneel location has been set, if it has been set you can do all what you could do with a normal location struct (can't access my Mac atm, I'll upload an example this afternoon)
@TimHiatt Since comments aren’t the most pretty place to explain code; I’ve made a pastebin paste with code here I hope this will help you use your functions

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.