Missing Argument for Parameter 'From' in Call When Creating Instance of Codable Class

Missing argument for parameter 'from' in call. Insert 'from: #Decoder# '

Every object in Swift needs an initializer: some code to set up the object when it is first created. If your object is an instance of a class, the initializer needs to be explicitly defined by you. However if the object is an instance of a struct, Swift implicitly defines an initializer. For example, this struct

struct Foo {
let bar: Int
}

implicitly gets an initializer that looks like this

 init(bar: Int) {
self.bar = bar
}

Initializers can also be implicitly created through protocol extensions. That means if your struct inherits a protocol (such as Codable), the protocol can define additional initializers for you. For this simple example, Codable would add something like this

init(from decoder: Decoder) throws {
// decode a key value pair with name "bar" and an Int value using decoder
let decodedBar = try ...
self.init(bar: decodedBar)
}

In your case, when you write parcel = Parcel() you are calling this kind of initializer

init() {
// initialize somehow with no input!
}

But you never defined anything like that! The compiler is suggesting that you call the initalizer you got from Codable since it's a close match, but that's probably not what you want either.

You can either define that missing initializer, or define default values for all of your struct's members. If you do that, the implicit initializer defined by Swift will have no arguments, making your code valid. For example

struct Foo {
let bar: Int = 3
}

let f = Foo() // now this is valid, implicit init has no arguments

Missing argument for parameter 'from' in call when creating instance of Codable class


I don't understand since this is a parameterless class. I have no idea
what the from parameter is all about.

I get no error when I run the following:

class Articles: Codable {
let value: [Articles]?

init() {
value = nil
print("in init()")
}
}

var x = Articles()

output:

in init()

And without init():

class Articles: Codable {
let value: [Articles]?

// init() {
// value = nil
// print("in init()")
// }
}

var x = Articles() //Missing argument for parameter 'from' in call

First, read this:

Automatic Initializer Inheritance

As mentioned above, subclasses do not inherit their superclass
initializers by default. However, superclass initializers are
automatically inherited if certain conditions are met. In practice,
this means that you do not need to write initializer overrides in many
common scenarios, and can inherit your superclass initializers with
minimal effort whenever it is safe to do so.

Assuming that you provide default values for any new properties you
introduce in a subclass, the following two rules apply:

Rule 1 If your subclass doesn’t define any designated initializers, it
automatically inherits all of its superclass designated initializers.

If you look at the docs, Codable is a typealias for the Decodable protocol (a protocol is like an interface in java). Protocols specify functions that a class must implement if they adopt the protocol. Your Articles class adopts the Decodable protocol. However, the docs for Decodable say,

init(from: Decoder)

Creates a new instance by decoding from the given decoder. Required.
Default implementation provided.

Protocols can actually implement functions by using extensions, which are then inherited by the class adopting the protocol.

As a result, the only designated initializer for your class is the one defined in the Decodable protocol, which takes one argument--which you inherit according to Rule 1. On the other hand, when you explicitly define another designated initializer in your class that takes no arguments, then you will call that initializer when you provide no arguments.

Missing argument for parameter 'from' in call - Swift

Either create a custom initializer or give a default value and var initialization for each property and you'll be fine, here's how:

class GMCategory: Codable {
var category_id: String = ""
var prodcat_name: String?
var category_image: URL?
var sub_categories: String?

enum CodingKeys: String, CodingKey {
case category_id
case prodcat_name
case category_image
case sub_categories
}
}
var category = GMCategory()

A better way is to change it from class to struct and you'll get the initializer for free, like this:

struct GMCategory: Codable {
let category_id: String
let prodcat_name: String?
let category_image: URL?
let sub_categories: String?

enum CodingKeys: String, CodingKey {
case category_id
case prodcat_name
case category_image
case sub_categories
}
}

var category = GMCategory(category_id: <#T##String#>, prodcat_name: <#T##String?#>, category_image: <#T##URL?#>, sub_categories: <#T##String?#>)

Missing argument for parameter 'from' in call - SwiftUI

You cant initialise Movie object like this ... it needs Decoder object or all member wise intialization ---
You can define your function like this

class DetailViewModel: ObservableObject {

@Published var fetchedMovie : Movie?

func getMovieDetails(id: Int) {

WebService().getMovieDetails(movie: id) { movie in

if let movieDetails = movie {

self.fetchedMovie = movieDetails

}
}
}
}

Swift Custom Struct missing argument from: Decoder

Conforming to Codable automatically creates init(from:) for your class.

SongLinkAPIResponse() is the same as SongLinkAPIResponse.init().

You'd have to specify an initialiser in your class:

init() {
// init properties here
}

Alternatively you can provide default values for your properties:

public struct SongLinkAPIResponse: Codable, Equatable {
public var entityUniqueId: String = "" // some default value
...
}

Missing argument for parameter in call

Your initializer has two parameters which are required when calling it.

Calling it properly would look something like this:

let whiskeyItemInstance = WhiskeyItem(wName: "name", wType: "type")

If you don't want to pass parameters to the initializer, you could provide default params:

init(wName: String = "default name", wType: String = "default type") {

or use an initializer with no parameters at all:

init() {
self.wName = "wName"
self.wType = "wType"
}

or call the initializer you already created like so:

convenience init() {
self.init(wName: "default name", wType: "default type")
}

Or you could forgo initializers altogether:

class WhiskeyItem {
let wName: String = "asdf"
let wType: String = "asdf"
}

Why am I getting a missing argument for parameter error in this code?

You're trying to initialize your Radio class with a parameter which is url.

But this class doesn't have an init(url: NSURL) (or at least it's not shown in your question) so you can't.

You need to create an init(url: NSURL) and inside this init you also have to call the super init for UIViewController (even if you don't use it, as in my example) since your Radio class inherits from it.

This is why you get this error:

Missing argument for parameter 'bundle' in call

because Xcode suggests to use the init(nibName: String?, bundle: NSBundle?) initializer for the class since it's the only one available.

So, keeping your code example as a base, you could do something like this to make it work:

class Radio : UIViewController {

var url = NSURL(string: "someDefaultURL")

init(url: NSURL) {
self.url = url
super.init(nibName: nil, bundle: nil)
}

required init?(coder aDecoder: NSCoder) {
fatalError("not implemented")
}

@IBAction func newButtonPressed(sender: AnyObject) {
let newURL = NSURL(string: "myurl")
let streamer = Radio(url: newURL!)
// do something with `streamer`
}
}

Missing argument for parameter 'delegate' in call // JSON file corrupted in SWIFT?


Missing argument for parameter 'delegate' in call

When a struct is create a value for each property is required.

If each property is specified with a default value and there is no user-defined initializer then Swift will create a default initializer for the struct.

If there is at least one property without a default value and there is no user-defined initializer then Swift will create a memberwise initializer which has one parameter for each property without a default value.

For example your type:

struct WeatherModel {
let conditionId: Int
let cityName: String
let temperature: Double
...

has three properties without default values. If you start typing:

let myWeatherModel = WeatherModel(

and then take the completion offered you will get (something like):

let wm = WeatherModel(conditionId: <Int>, cityName: <String>, temperature: <Double>)

The completion is showing the memberwise initializer.

Your type which produces the error is:

struct WeatherManager {
let weatherURL = "https://api.openweathermap.org/d.5/weather?appid=c8b50079338280b47a65dd6082551e3b&units=imperial"
let delegate: WeatherManagerDelegate?

which has two properties only one of which has a default value, and it has no initializers, so Swift will create a member wise initialiser automatically.

There is nothing wrong so far.

The line producing the error is:

var weatherManager = WeatherManager()

Here you are attempting to create a WeatherManager without invoking the member wise initalizer, so Swift gives you the error message.

If you click on the error message itself you will see a Fix is offered, click that and Swift will change your code to:

var weatherManager = WeatherManager(delegate: <WeatherManagerDelegate?>)

Select the <WeatherManagerDelegate?> and type the value you wish to pass.

HTH

Why am I getting this swift error? missing argument for parameter 'price' in call

price is a parameter to your init constructor. An argument must be provided, even if the parameter is of an optional type. If you don't have a value for it, you can give it nil:

let myFlight = FlightDataModel(airline: carrierName["name"] as String, price: nil)

If you wanted price to default to nil if that argument is not provided you do one of two things:

  1. You could provide a default value for price in the initializer:

    init(airline: String?, price: String? = nil) {
    self.airline = airline
    self.price = price
    }
  2. You could provide a second separate init that just takes an airline:

    init(airline: String?) {
    self.airline = airline
    }


Related Topics



Leave a reply



Submit