Swift 5 Value of type has no member
If you define a type’s access level as internal or public (or use the
default access level of internal without specifying an access level
explicitly), the default access level of the type’s members will be
internal.
https://docs.swift.org/swift-book/LanguageGuide/AccessControl.html
I hate that, and wish it worked like you were thinking it would. The big problem with it is inconsistency: public
will cascade down, but only in a public extension
. As such, I recommend using extensions for access grouping. You can't do that with stored properties, though.
Value of type ... has no member
You need to move the declaration of result
var result: Double {
get {
return 0.0
}
}
here, to inside of the class:
class calculatorBrain {
var result: Double {
get {
return 0.0
}
}
...
}
Since you are defining result outside of the CalculatorBrain
class so you get the error:
Value of type 'CalculatorBrain' has no value 'result'
swift error: Value of type _ has no member _
You are trying to assign the subscript values from an array object without specifying the index.
Try this:
class ViewController: UIViewController {
@IBOutlet weak var prizeLabel: UILabel!
@IBOutlet weak var priceLabel: UILabel!
@IBOutlet weak var timeLabel: UILabel!
@IBOutlet weak var categoryLabel: UILabel!
@IBOutlet weak var ContentLabel: UILabel!
@IBOutlet weak var TopicLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
if let url = URL(string: "http://{Local-Host}/post") {
URLSession.shared.dataTask(with: url) { data, response, error in
if let data = data {
do {
let parsedJSON = try JSONDecoder().decode(parsingss.self, from: data)
print(parsedJSON)
DispatchQueue.main.async {
self.TopicLabel.text = parsedJSON.scrims[0].topic
self.ContentLabel.text = parsedJSON.scrims[0].content
self.categoryLabel.text = parsedJSON.scrims[0].category
self.timeLabel.text = parsedJSON.scrims[0].time
self.priceLabel.text = parsedJSON.scrims[0].price
self.prizeLabel.text = parsedJSON.scrims[0].prize
}
}
catch {
print(error)
}
}
}.resume()
}
}
}
Type has no member
your property image access the block property before they are both initilized, to fix your problem your variable "image" should be flagged as lazy variable, meaning it will be initilized after the other properties have been initilized hence it is safe to access those other properties.
More about lazy properties
The official Swift documentation says,
"Lazy properties are useful when the initial value for a property is dependent on outside factors whose values are not known until after an instance’s initialization is complete."
You can read more about lazy properties here: - Swift Properties Official documentation
Here is how to do it: -
lazy var image: Image = {
return Image(blocksArray: blocks, code: codes, color: colors)
}()
for the case of colors part, I think it is a good practice to avoid typing strings when initializing especially for simple colors like yours and use the enum versions.
Change it to this : -
var colors = [UIColor.red]
or this: -
var colors: [UIColor] = [.red]
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")
}
}
}
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?
Swift 3 Value of type 'Any?' has no member 'object'
Unlike Swift 2, Swift 3 imports Objective-C's id
as Any?
instead of AnyObject?
(see this Swift evolution proposal). To fix your error, you need to cast all of your variables to AnyObject
. This may look something like the following:
jsonmanager.post("http://myapi.com", parameters: nil) { (operation: AFHTTPRequestOperation?, responseObject: Any?) in
let response = responseObject as AnyObject?
let meta = response?.object(forKey: "meta") as AnyObject?
let status = meta?.object(forKey: "status") as AnyObject?
let totalData = response?.object(forKey: "total_data") as AnyObject?
if status?.intValue == 200 && totalData?.intValue != 0 {
let aa = response?.object(forKey: "response") as AnyObject?
self.data = aa?.mutableCopy()
}
}
Value of type 'Tags' has no member 'lastUsed'
The problem is that the declarations of name
and lastUsed
have an invisible character (U+200B = "ZERO WIDTH SPACE") as the last character of the identifier.
Here is a short example demonstrating what seems to be a paradox. The first print statement does not compile, but the last one does:
struct Tags {
let name = "name"
}
print(Tags().name) // (1) Error: Value of type 'Foo' has no member 'name'
print(Tags().name) // (2) No error
Unfortunately, Xcode does not display this character, even if “Editor->Invisibles” is switched on. Opening the file in vi
shows the issue:
struct Tags {
let name<200b> = "name"
}
print(Tags().name) // (1) Error: Value of type 'Foo' has no member 'name'
print(Tags().name<200b>) // (2) No error
Note that invisible characters are allowed in identifier names, this has been discussed in the Swift forum.
The first print statement was created with Xcode's code completion and Xcode omits the trailing invisible character. I would consider that a bug.
The second print statement was created by carefully copying the name
identifier including the trailing invisible character.
Summary: Swift identifiers can contain invisible characters, but those do not work well with Xcode's code completion and cause only confusion. Rewriting all occurrences of those identifiers fixes the issue.
Related Topics
Sharing Screenshot of Swiftui View Causes Crash
Swift, Detect Ibeacons on The Background and Send Notifications When in Range
Transparent Sticky Header UI Collectionview Don't Show Cells Underneath
How to Store Data from a Picker View in a Variable When a Button Is Pressed
Applescript Used in My Cocoa MAC App, Stopped Working in Osx 10.14
Open Attachment from Mail Using iOS 8 App [Swift]
In Swift Can You Trap "Fatal Error Unexpectedly Found Nil While Unwrapping an Optional Value"
Include Inheritance Constraint in Swift Generic Types
Msmessagelivelayout Freeze/Crash in Transcript When Info.Plist Contains Privacy Request
How to Create Travelling Wave in Spritekit
Spritekit: Sprites Are Moving Through Each Other with a Physicsbody Already Set
Verifying The Purchase (Receipt) of Another Application (Mac App Store)
Calling Asynchronous Method Inside For-Loop
Cache That Can Purge Unused Objects by Demand
Creating Decoration View as Custom Column in UIcollection View
Issue with Optional Core Data Relationship Using Nspersistentcloudkitcontainer