Correct Syntax for accessing this array - arrays

I have a View controller, with a tableview inside of it. I have a navigation button called "create". After the user has filled in a couple textfields and selected a cells from the UITableview i want the create button to create a parse object with the selected and inputed information.
I have these arrays..
var name = [String]()
var address = [String]()
var theseItems:[String] = [] //globalArray
im appending the selected cells to "theseItems" array.
//i have already queried and added what i wanted, to the name and address array..so they are filled with information.
didSelectRowAtIndexPath {
self.theseItems.append(name[indexPath.row] as String)
self.theseItems.append(address[indexPath.row] as String)
now with the create Button i want to create an object from this information but am having a hard time accessing the selected cell index path in the button...
#IBAction func createButton(sender: AnyObject) {
let thisObject = PFObject(className:"thisObject")
thisObject["name"] = name.text
thisObject["address"] = address.text
thisObject["selectedCell"] = theseItems(name[indexPath.row])
thisObject["selectedCell2"] = theseItems(address[indexPath.row])
//error** unresolved identifier "indexPath"
I'm not sure the correct way to access the array with a cells information.. to save it to parse. thanks in advance!
thisObject.saveEventually()

You can get the index paths of the currently selected cells by calling the indexPathsForSelectedRows() method on the table view (or indexPathForSelectedRow() if constrained to a single selection at one time).

You're trying to access a an array position from text, but text is a textField, not an array

Related

Swift: How do you use a variable in an array?

I am using a pod called iOSDropDown to display a dropdown selection menu for a textfield. I am getting a list of data from php to populate that selection menu that Im storing in a variable.
The PHP data passed stored in a swift variable looks like this when printed -> "option01","option02","option03"... and so on. This is dynamic data that will change that is why I am retrieving from PHP/MYSQL Database instead of just manually typing in the options in the Swift array.
Below is my code. What I am trying to do is use the "dropdownData" variable that holds the options for the array. Each option should be in its own row and separately selectable. What I am getting is one option, one string of coding with all my options as shown in the picture below.How would I use the dropdownData variable to display options instead of one string, one option?
dropdownData = "option01","option02","option03"... etc. ALL OPTIONS STORED IN THIS ONE ARRAY
let dropdownData : String = (dumpsArray[indexPath.row] as AnyObject).value(forKey: "dropdownData") as! String
.
cell.nameField.optionArray = [dropdownData]
Image
In the image above there should be no comma after styrofoam cooler... the next product should be the next option displaying under the styrofoam cooler and separately selectable.
Seems like dumpsArray[indexPath.row] as AnyObject).value(forKey: "dropdownData") returns a String where names are comma separated,
so
if let nameString = dumpsArray[indexPath.row] as AnyObject).value(forKey: "dropdownData") as? String {
let namesArray = nameString.components(separatedBy: ",")
cell.nameField.optionArray = namesArray
}
That should do

Iterate through an array in an array of dictionaries swift

I am currently in a bit of a bind.
struct sectionWithDatesAsName {
var sectionName : String
var sectionObjects : [SoloTransactionModel]!
init(uniqueSectionName: String?, sectionObject: [SoloTransactionModel]?) {
sectionName = uniqueSectionName ?? "nil"
if let section = sectionObject {
sectionObjects = section.reversed()
}
}
}
I currently have an array of sectionWithDatesAsName. And I can work with it, display in the tableView among other things.
The bind comes up when I want to check some information in the sectionObject before displaying it on the tableView.
I want to check the type of the sectionObject which is saved in the object itself.
How do I check the information in the sectionObject without slowing down the app? Or have a horrible time complexity calculated?
(Note: I can't change the format of the struct has this has already been used by a whole lot of other processes)
Write a function in your sectionWithDatesAsName with the signature filteredSections(type: sectionType) -> sectionWithDatesAsName
(If you don't have the ability to edit the definition of sectionWithDatesAsName you can create an extension that adds the above function)
If the sectionWithDatesAsName is defined elsewhere, define this function in an extension.
When you call it, build a new sectionWithDatesAsName object by filtering the arrays to match the specified type.
Use the resulting filtered sectionWithDatesAsName object as the data model for your table view. It will be built once and used for the lifetime of the tableView, so you will pay an O(n) time cost to filter it once when you create it.

Update UIlabel text with a String

I have a label in the prototype table view cell, and also a UITextviewfield, I want to update the text of the label by using UITextviewfield.
first I created a struct:
struct CellElements {
let title: UILabel
}
then an array:
var list = [CellElements] ()
then I use these codes to add the text inside the UITextfieldto the list array and later I will add them in the table view.
#IBOutlet weak var inputField: UITextField!
#IBAction func addName(_ sender: UIButton) {
list.append(inputField.text)
}
the problem is, swift doesn't let me to do that with this error
Cannot convert value of type 'String?' to expected argument type 'CellElements'
I will appreciate for your help in advance.
list is an array of CellElements. When you attempt to append to that array, you need to add a CellElements instance.
But your line:
list.append(inputField.text)
is attempting to append a String. Hence the error since String isn't CellElements.
What you want to do is to create an instance of CellElements from the text field and then append that instance.
list.append(CellElements(title: inputField.text!))
Note that you need a String and inputField.text is an optional String. This is a case where you can safely force-unwrap an optional. See the documentation for UITextField text for why this is safe.

Best way to utilize retrieved object's properties to populate arrays and subsequent labels?

I have a database (parse-server) from which I can fetch objects which contain information. Some of the information in the properties of the objects are used to populate labels on table views. The way I have been populating, let's say, the userName and userLike labels are as follows:
Appending Different Arrays with the objects properties
var userName = [String]()
var userLikes = [String]()
func query(){
let commentsQuery = PFQuery(className: "UserStuff")
commentsQuery.findObjectsInBackground { (objectss, error) in
if let objects = objectss{
for object in objects{
self.userName.append(object["userName"] as! String)
self.userLikes.append(object["userLikes"] as! String)
}
}
}
}
Ignore the fact that I don't have a .whereKey or any else statements to handle other cases... this is bare bones just for illustration of the question. Anyway, in this method, the userName and userLikes arrays are iterated through to populate the labels. The for object in objectss{} ensures that the indexes in one array (whether index 0,1,2,3,etc...) refers to/comes from the same object as the value in the index of the other array. However, I was wondering if would be better to do it as follows:
Appending the whole object to a PFObject array
var userObjects = [PFObject]()
func query(){
let commentsQuery = PFQuery(className: "UserStuff")
commentsQuery.findObjectsInBackground { (objectss, error) in
if let objects = objectss{
for object in objects{
self.userName.append(object)
}
}
}
}
With this method I could instead populate the labels with something like:
userNameLabel.text = String((userObjects[0])["userName"])
In this method all properties of the object would be accessible form the same array. I can see that this may have some advantages, but is this definitively the better way to do it/should I switch immediately?
I am going to say that the answer is that the latter of the two is probably the better method. This is because in the former, the information from a particular object is only linked between arrays by the order in the array. Any accidental or incorrectly scripted functions involving .append or .remove could skew the order between arrays and then an object's name might be the 3rd index in the nameArray but its likes may end up being the 4th index in the likesArray and it would be difficult to amend this issue. With the latter method, all information regarding an object's properties are linked to the object itself in the array and this issue is avoided.

Get indexOfObject method without using Foundation objects

by selecting 1st tableView row/section I want to check if selected item already in 2nd tableView ?, if yes then find that selected item indexOfObject in 2nd tableView.
NSInteger sectionIndex = [[allSelectedProducts valueForKey:#"productID"] indexOfObject:[allProductData[indexPath.section] valueForKey:#"productID"]];
this will return the index of selected object in allSelectedProducts, Returns the lowest index whose corresponding array value is equal to a given object.
I want to perform this same task in swift, how can I achive that !
In Swift I've taken allSelectedProducts for 1st tableView and allProductData for 2nd tableView both arrays with Array<Dictionary<String, Any>> type
I want to perform this task without using Foundation classes, can we perform that same task in array using indexOf in Swift !?
let productIndex = allSelectedProducts.indexOf(<#T##predicate: ([String : Any]) throws -> Bool##([String : Any]) throws -> Bool#>)
If we can, then how ?
please guide me how to use indexOfin swift
thanx in advance for any help
Probably something like this:
let searchedProductID = allProductData[indexPath.section]["productID"]
let index = allSelectedProducts.indexOf { $0["productID"] == searchedProductID }
which is a direct translation of your original code

Resources