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 ``.
Related
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?
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).
This question already has answers here:
How to check if an element is in an array
(18 answers)
Closed 7 years ago.
Swift 2 - so, I have an array and a uitextfield that a user inputs a string, I want to check whether the textfield.text is equal to ANY of the values in the array, can I do this with one line of code rather than lots of if's and else if's?!?
This is a generic code that will do what you are looking for. The if statement checks to see if a given value is equal to something that is located in the array. Simply replace the arr.contains() with the output you have given for your UITextfield.text Try to do a little research before you post. I can see that you are new here, so here is a little bit of help.
var arr = [1,2,3,4]
if arr.contains(3) {
//do something
}
So I am really new to parse and I have no idea how to store a famous quote and it's own author, so then I can use that information in my iOS app made with swift.
I have imported all the frameworks in order to make parse work but I don't know to to store that quotes with the authors and then retrieve the information to display the quote in a label and the author in another label. Please don't be rude, I don't get who to make this work.
So if you want to save a text and retrieve to Parse
let's say you want to save some text into a class called Data
Save
var data = "Swift is nice"
var object = PFObject(className:"Data")
object["message"] = data
object.saveInBackground()
So I use the saveInBackground method just for simplicity however if you should other saveInBackground method where you could check if there is no error while you are saving into Parse.
Retrieve
#IBOutlet weak var textlabel:UILabel!
var query = PFQuery(className:"Data")
query.getFirstObjectInBackgroundWithBlock({ (objects:PFObject?, error:NSError?) -> Void in
if error == nil {
let retrieveData = objects?.objectForKey("message") as! String
//so lets say you had a UILAbel
self.textlabel.text = retrieveData
}
})
I used the getFirstObjectInBackgroundWithBlock method because I assume that I have at least one object save in Parse. So if you are retrieve a lot of data you could findObjects method .
Hope that helps :)
To answer your comment on Lamars's fine answer:
Create a class ("Add class") "Quotes" in Parse, if you haven't already. Create a column in that class named "Quote", set it's type to String. Click "Add Row" and you will get a new row in your table. Double Click it's field and you can write your quote there.
This is me editing the usernameField in my Parse Class:
You could have another column named "Author" of type String, and just do the same thing, but if your app gets more advanced and you would like to display all the quotes from a specific author, you should add another class, named "Author". Add a column named "Name", double click and submit your name.
In your "Quotes" class, add a column named "Author", type Pointer, and make it point to your Author Class. Then copy the correct objectId from Pointer (let's sat Steve Jobs has objectId "12345678") and paste it to the "Author" column in Quotes. Now, if there's another quote by Steve Jobs, you can re-use that objectId, not having to store the name "Steve Jobs" more than once.
I understand you're new to Parse.com and maybe databases as well, but this way of creating relations is very good knowledge, if you want to design stuff in the future.
Parse has a great documentation, in Obj-C and Swift, check it out:
https://www.parse.com/docs/ios/guide
This question already has answers here:
Syntax help - Variable as object name [duplicate]
(1 answer)
Objective C Equivalent of PHP's "Variable Variables" [duplicate]
(2 answers)
Closed 8 years ago.
I have several textfields (and labels) in my XIB and at some point in my application I build dynamicaly a string which contains a control name (i.e. one of the textfields).
How can I refer to the actual textfield using the string I created that holds the name of the textfield.
For example, I have txt1 , txt2, txt3, txt4 as UITextFields
and i have a string (str) that contains one of the fields above names (str ="txt3")
then i want to change the content of the uitextfield txt3 since str have "txt3"
at this point.
How do I cast from the string to the actual control?
If the UITextFields all have a backing property then you can use KVC to get a reference to the control and then you just use it as normal.
NSString *str = #"txt1";
UITextField *myTextField = [self valueForKey:str];
myTextField.text = #"what ever you want to update to";
Side note
Although you have not mentioned the source of str #JefferyThomas raises a very valid point about not trusting use input that has not been validated.