In Swift 3, you could do this to get the name of the current user:
var username = NSUserName()
as was explained here: Getting the username of actual user in Swift
But in Swift 4 it only returns a blank string without any error, when executed in a playground. What is the correct way to do it in Swift 4?
Related
This question already has an answer here:
Can we use keywords as parameter names in SWIFT?
(1 answer)
Closed 4 years ago.
I am a new iOS programming. I am creating a sample app which get data and set data using Firestore. For the collection contains some documents which and named those properties like extension... So in programming I need to create variable with that name extension in order to set data to Firestore but Xcode is warning me that I don't have to declare that variable name.
I have been trying to figure it out but not work. So, I decided to ask my own question if there any technique to delacre variable name which is warning by Xcode.
var `extension`: String?
var username: String?
init(`extension`: String, username: String) {
self.`extension` = `extension`
self.username = username
}
I think this will work by using ``.
I'm working with core data to save data from my users, I'm following a tutorial with Swift 3 It works perfectly fine on the tutorial and actually the code works on my Xcode too but it does show some errors!
Here is an image of my code and the errors
Full error:
Cannot convert value of type 'UserInfoEntity!.Type' to expected argument type 'NSEntityDescription'
And the the other error:
Cannot call value of non-function type 'NSEntityDescription?'
Images of my core data entities
Ask anything if the info is not complete
Replace your line:
var newUser = UserInfoEntity(entity: UserInfoEntity!, insertInto: self.appDelegate.coreDataStack.managedObjectContext)
With:
var newUser = UserInfoEntity(entity: UserEntity!, insertInto: self.appDelegate.coreDataStack.managedObjectContext)
Ah, you're passing "UserInfoEntity" class type instead of an object of that class - the 5th line of code.
Advice: keep your variables in lowercase.
This question already has answers here:
Dictionary error: Ambiguous reference to member '+' [duplicate]
(4 answers)
Closed 5 years ago.
teaching myself swift I am trying to understand how dictionaries work. Using playground. I have made a simple dictionary called "menu" that has a list of items with their name as keys and their price as values. Like so:
let menu = ["crisps": 2,
"oranges": 3,
"chicken": 8,
"meat": 12]
Then, I try to add the values of those items like so:
let costOfMeal = menu["crisps"]! + menu["oranges"]! + menu["chicken"]! + menu["meat"]!
This gives me the error: ambiguous reference to member '+'
not sure what's going on. Any input appreciated.
Thanks,
David
let costOfMeals = Array(menu.values).reduce(0, +)
You are trying to add up every key and value together! You should only add up the values. You know the dictionary is Key and Value, and you should only add up the Value's.
This is merely the compiler's automatic type casting getting confused by the multiple additions of unwrapped optionals.
You can help it along by adding an actual integer in the formula.
let costOfMeal = 0 + menu["crisps"]! + menu["oranges"]! + menu["meat"]! + menu["chicken"]!
Don't let it bother you as it has nothing to do with what you're trying to learn and your formula was correct (albeit not safe for production).
I am trying to update a string with firebase swift but I am getting an error that I do not know how to get rid of.
I have this code part that is getting an error:
self.dbRef.child("feed-items/\(dataPathen)/likesForPost").updateChildValues("likesForPost": "7")
The error I am getting is expected "," seperator just before the :. I am using dbRef in another code part so I know i works and the dataPathen is being printed just before the above code part, so that is working too.
Can anyone help me with this bug?
Just change
self.dbRef.child("feed-items/\(dataPathen)/likesForPost").updateChildValues("likesForPost": "7")
To
self.dbRef.child("feed-items/\(dataPathen)/likesForPost").updateChildValues(["likesForPost": "7"])
And if you are only looking for incrementing a particular value at a specific node you might wanna check my answer's :- https://stackoverflow.com/a/39465788/6297658, https://stackoverflow.com/a/39471374/6297658
PS Prefer runTransactionBlock: to update properties like likeForPosts as there might be a moment when two users try to like same post at the same moment (Highly Unlikely, but still a possibility...),using updateChildValues might end up just updating like only from one user. But runTransactionBlock: keep firing until the changes of that thread have been committed to the node
updateChildValues accepts [AnyHashable:Any] dictionary:
self.dbRef.child("feed-items/\(dataPathen)/likesForPost")
.updateChildValues(["likesForPost": "7"])
Whenever updating values at any reference in Firebase Database, you need to pass a dictionary parameter for updateChildValues method as [AnyHashable: Any] for your path reference. So just update your code of line as below:
self.dbRef.child("feed-items/(dataPathen)/likesForPost").updateChildValues("likesForPost": "7")
Also if you need to update more than 1 key-value pairs then you can pass those key-value pairs inside dictionary by seperating using comma as below:
self.dbRef.child("feed-items/(dataPathen)/likesForPost").updateChildValues(["likesForPost": "7", "otherKey": "OtherKeyValue"])
I am a beginner with swift and parse, but I am to make an App in which I have to retrieve a user's First Name and post it in a tableView. The thing is that I can't find a way to retrieve the user's First Name with only the user's objectId. I found this on the website during my research but it does not seemed to be what I want to do. I have also tried to do :
userNameLabel.text = (interest.user!["FirstName"] as! String)
I get the error : 'NSInternalInconsistencyException', reason: 'Key "FirstName" has no data. Call fetchIfNeeded before getting its value.'
I guess the problem is because it thinks it is a PFObject and therefore does not see it as a String (??)
Anyway does someone have an idea on how I can retrieve my user ("interest.user") First Name ?
Thank you for your time.
Since your user object is a pointer, when you run your query you need to tell Parse to include any extra information that's not part of that class. In this case, since it's a pointer, you need to do query.includeKey("user") or whatever the name of the column is that points to your user class.