Reputation: 9130
I'm trying to access to function in struct like this
struct DoSomething {
var str: String
func processNumber(number: Int) -> String {
return "\(number)"
}
}
let number = DoSomething().processNumber(number: 10)
print(number)
But I'm getting this error:
Missing argument for parameter 'str' in call
Any of you knows how can Access the function in the struct.
I'll really appreciate your help.
Upvotes: 1
Views: 1151
Reputation: 100523
You should pass a paramter like this be
let number = DoSomething(str:"someValue").processNumber(number: 10)
As there is no ()
init in that case , if you need it a separate logic do
class func processNumber(number: Int) -> String {
return "\(number)"
}
and call like
let number = DoSomething.processNumber(number: 10)
note if the var
var str: String
has no usage you should not create it either
Upvotes: 1
Reputation: 773
The issue can be resolved by declaring the str
as an optional.
struct DoSomething {
var str: String?
func processNumber(number: Int) -> String {
return "\(number)"
}
}
let number = DoSomething().processNumber(number: 10)
print(number)
Upvotes: 1
Reputation: 285082
You have to initialize str
when creating an instance of DoSomething
.
Either assign a default value in the struct
struct DoSomething {
var str: String = ""
....
}
let number = DoSomething().processNumber(number: 10)
Or in the initializer which has the signature init(str : String)
let number = DoSomething(str: "").processNumber(number: 10)
Upvotes: 1