ios - Initializing a Struct in Swift Error: Generic parameter could not be inferred -
i'm trying declare struct in class without providing parameters. struct need initialized function should visible entire class. normally, i'm able var mystruct : mystruct?
time it's giving me error.
generic parameter 'type' not inferred
am initializing struct incorrectly?
how i'm using it:
class myclass: { static let sharedinstance = myclass() private override init() {} let mystruct = mystruct? // <-- above error occurs here. // let mystruct = mystruct<int>? // <-- error: expected member name or constructor call after type name // let mystruct = mystruct<int>?() // <-- error: cannot invoke initializer type 'mystruct<double>?' no arguments func runfunction(a: int, b: int) { mystruct(var1: a, var2: b) // <-- need initialize here. } func otherfunction() { mystruct.dostuff() // <-- , have access in other functions }
code struct:
public struct mystruct<type: custominput>: customprotocol { public let myvar1: type public let myvar2: type public init(var1: type, var2: type) { self.myvar1 = var1 self.myvar2 = var2 } ... }
for generic type, generic parameter considered part of type. type of struct isn't mystruct
, it's mystruct<int>
(or whatever generic type use. therefore, can't declare type if there's not information compiler use infer actual type. instead, have include generic type in declaration:
var mystruct: mystruct<int>?
Comments
Post a Comment