Could Not Find Member <Method Name> for Struct Type in Swift

Could not find member method name for struct type in Swift

It is because initial returns an immutable instance of $. You are then trying to call a mutating method (flatten) on it. In order to mutate it again, you will have to store it in another variable that can be mutated. You could also change it to be a class instead of a struct because classes are mutable.

Note currently, the error message you are getting is terrible, but that is life with Swift at the moment.

Type 'Item' has no member 'init'

The error occurs because the generic type Item has nothing to do with the struct Item and the compiler treats Item as generic type which doesn't have an initializer.

As you append the concrete type Item in the init method the generic type is pointless.

struct Model {
var items = Array<Item>()

init(){
items.append(Item(value: "A", id: 1))
}
}

which is practically the same as

struct Model {
var items = [Item(value: "A", id: 1)]
}

To clarify the issue this is a generic method with a different type name

struct Model<M : Equatable> {
var items = Array<M>()

init(){
items.append(Item(value: "A", id: 1))
}
}

Of course this throws the compiler error

Cannot convert value of type 'Item' to expected argument type 'M'

You can cast the type

items.append(Item(value: "A", id: 1) as! M)

but this makes the generic type pointless – as mentioned – because now M is equal to static Item and if you try to specify another type

let model = Model<String>()

you'll get a runtime error

Could not cast value of type '__lldb_expr_1.Item' (0x104dc0118) to 'Swift.String' (0x1f7c94ab8).

which causes a crash.

Type X has no member of Y

As Frankenstein mentioned, you're calling a function on the Type itself, not an instance of the type. In other words, you're not calling any sort of variable or constant instance, you're calling the Type definition (the blueprint) itself. And that's not allowed unless you explicitly state it in the type.

Sometimes you want this, but then you have to make the fetchPosts function in the Post type static. Like:

struct Post { // or maybe your Post type is a class, so it would be class Post {
...
static func fetchPosts() { ... }
...
}

But I'm guessing you don't actually want to run a static function on the Type itself, because it would have no access to any of the properties of any Post struct/class that you created and populated with settings/data in the properties.

It's hard to tell how to help without seeing exactly what's going with your Post type.

But I'd guess you actually want to create an instance of Post first. Something like:

func fetchPosts()
{
//this could be from Firebase, Amazon, whatever

var postInstance = Post() // This creates a new Post instance for you to work with
self.posts = postInstance.fetchPosts() // This calls your new Post instance's fetchPost() method
self.tableView.reloadData()
}

That will make your error go away, but I'm not sure you want to run fetchPosts on a brand new instance of Post. Did you already have a Post instance somewhere that needs to be accessed in this scope?

And is it just a coincidence that you have a fetchPosts() method in both your Post class/struct AND in your UIFeedViewControllerTableViewController class?

Type does not have a member

You cannot initialize an instance class property referencing another instance property of the same class, because it's not guaranteed in which order they will be initialized - and swift prohibits that, hence the (misleading) compiler error.

You have to move the initialization in a constructor as follows:

let components: NSDateComponents

init() {
self.components = myCalendar.components(.CalendarUnitYear | .CalendarUnitMonth, fromDate: now)
}

Value of type User has no member 'logStatus Swift Error

You should take logStatus() out of your initializer

struct SomeStruct {
let name: String
var email: String
var followers: Int
var isActive: Bool

init(name: String, email: String, followers: Int, isActive:Bool) {
self.name = name
self.email = email
self.followers = followers
self.isActive = isActive
}

func logStatus() {
if (isActive) {
print("\(name) is working hard" )
} else {
print("\(name) has left Earth")
}
}
}

Instance member cannot be used on type

You just have syntax error when saying = {return self.someValue}. The = isn't needed.

Use :

var numPages: Int {
get{
return categoriesPerPage.count
}

}

if you want get only you can write

var numPages: Int {
return categoriesPerPage.count
}

with the first way you can also add observers as set willSet & didSet

var numPages: Int {
get{
return categoriesPerPage.count
}
set(v){
self.categoriesPerPage = v
}
}

allowing to use = operator as a setter

myObject.numPages = 5

X is not a member type of Y

You likely have a class/struct/enum DataManager anywhere in your code which hides the DataManager module. The error states that it cannot find LocationManager in type DataManager where it should read module instead.

Your app modules should be designed in a way so that you never need to explicitly use the module's name except for the import statement. So just use LocationMessage.Type directly without stating the module.

This is one reason all our app's modules are prefixed with an X, e.g. XDataManager. This avoids conflicts with Apple's modules, external modules (like from CocoaPods) and with types (like in your case).

The X also makes it obvious that these modules are part of the app itself and not some third-party frameworks.

Initializing a Struct in Swift Error: Generic parameter could not be inferred

For a generic type, the generic parameter is actually considered to be part of the type. So the type of your struct isn't MyStruct, it's MyStruct<Int> (or whatever generic type you use. Therefore, you can't just declare the type by itself if there's not any information for the compiler to use to infer the actual type. Instead, you have to include the generic type in your declaration:

var myStruct: MyStruct<Int>?

Swift code can not find the symbol of the data

Your response is of type Post which has no property data. You'll need to extract your photos array from the response, and then map across that array and retrieve the rovers property from it.

I think what you meant to write was

self?.rovers = response.photos.camera.map{$0.rover}

However even that won't work as your data structures don't match your JSON. From what can be seen, rover is a property on photo not on camera.

You will need to validate the JSON -> Model mapping

EDIT after JSON linked in comment below:

Using the JSON from the API, it confirms that camera and rover sit at the same level in the JSON:

{
"photos": [
{
"id": 102693,
"sol": 1000,
"camera": {
"id": 20,
"name": "FHAZ",
"rover_id": 5,
"full_name": "Front Hazard Avoidance Camera"
},
"img_src": "http://mars.jpl.nasa.gov/msl-raw-images/proj/msl/redops/ods/surface/sol/01000/opgs/edr/fcam/FLB_486265257EDR_F0481570FHAZ00323M_.JPG",
"earth_date": "2015-05-30",
"rover": {
"id": 5,
"name": "Curiosity",
"landing_date": "2012-08-06",
"launch_date": "2011-11-26",
"status": "active"
}
},
....

So you will need to change your data model:

struct Photo : Codable{
let id : Int
let sol : Int
let camera : Camera
let imgSrc: String
let earthDate: String
let rover: Rover
}

and then to decode it

self?.rovers = response.photos.map{$0.rover}

nb. in Swift all struct types should be capitalised by convention.



Related Topics



Leave a reply



Submit