trying to populate a cell label with a string from an array - ios

So, I have a cell with one label inside. I am trying to populate that label text with the various items in my array - all strings.
My array
var restauranttypelist: [String] = ["American", "Asian", "Bakery & Deli",
"Burgers", "Italian", "Mexican", "Seafood", "Steakhouse"]
and my cell label text
let type = restauranttypelist [indexPath.row]
var typecell = tableView.dequeueReusableCellWithIdentifier("cellone") as RestaurantTypeCell
typecell.restaurantTypeLabel.text = restauranttypelist.text
return typecell
I have tried a number of solution ranging from ".text" seen above, to ".String", to "restauranttypelist:indexPath.row" to no avail.
I suppose I have two questions. Am I setting up my array correctly? Do I need to insert the "[String]" portion after the variable name?
Finally, how would I be able to set the cell label to the numerous items I have in my array?
Thanks for any help... beginning.
Jon

In let type = restauranttypelist[indexPath.row] you're accessing a String from your Array and storing it in type. Therefore, all you need is typecell.restaurantTypeLabel.text = type.
There's nothing wrong with how you setup the array. You don't need the [String] type annotation since it can be inferred from the value you are assigning to it, but having it there does no harm.
Finally, this doesn't affect how your code works, but it's nice to know anyway:
Swift variable names follow the convention of starting with a lowercase character, and then capitalizing every subsequent word. Following that convention your variable names should be typeCell and restaurantTypeList.

Related

How do I convert Collection of [UILabel] to string text to display on ViewController?

I am trying to display the same information on a collection of [UILabel], but I am getting this error,
Value of type '[UILabel]?' has no member 'text'
I am getting the specific error on this line in my code.
storagetypelabel.text = "\(booking.storageType.uppercased()) STORAGE"
Would love some input on how to alter the way my property.text label is typed.
Since I hooked up multiple UILabel to one [UILabel] collection, when I call the [UILabel] property, I should be able to display the same information for all the connected labels in the collection right?
text is a property of a single UILabel not an array , To set the text for all labels use
let str = "\(booking.storageType.uppercased()) STORAGE"
storagetypelabel.forEach { $0.text = str }

How to convert NSArray to Set?

Most of the posts here are on about how to convert a set into an array.But I would like to know if I could convert an array to a set.I have downloaded my array from parse and want to convert it to a set because I want the user to edit their details and the form builder I used needs to accept a set data type only.
This is what I got.The "Subjects" field is an NSArray in Parse.
let subjects = Set(currentuser?.objectForKey("Subjects"))
Basically the syntax is correct, but the compiler (and me too) doesn't know that the object for key Subjects is supposed to be an array. Use optional binding to check at least for an array of NSObject to conform to the requirement that the items in the array are hashable (thanks to dan for pointing that out):
if let userSubjects = currentuser?.objectForKey("Subjects") as? [NSObject] {
let subjects = Set(userSubjects)
}
If the real type of Subjects is more specific than [NSObject] use that instead

Multidimensional Array Looping in cellForRowAtIndexPath Swift

I have a multidimensional array that I want to display the values of onto one UILabel in each respective cell.
My arrays look like this:
var arrayExample = [["beverages", "food", "suppliers"]["other stuff, "medicine"]]
I'm looping through these values in the cellForRowAtIndexPath in order for it to display on different cells (on a UILabel) the appropriate values:
if let onTheLabel: AnyObject = arrayOfContactsFound as? AnyObject {
for var i = 0; i < objects!.count; i++ {
cell?.contactsUserHas.text = "\(onTheLabel[indexPath.row][i])" as! String
print("arrayOfContactsFound Printing! \(onTheLabel)")
}
}
When printing to the console I get:
arrayOfContactsFound Printing! (
(
beverages,
"supply chain",
pharmacuticals
)
)
But on my label I get "beverages". That's it. How can I get the other 2 values (or X amount if there are more or less than 3 values)?
My for in loop is obviously not doing the trick. Assuming I can optimize/fix that to display all the values?
Thanks in advance.
In your loop you're setting the text of your label multiple times. Each time you set it it doesn't accumulate, it completely replaces the current text with the new text. You'll want something like this:
// Remove the cast and the loop in your code example, and replace with this
let items = arrayOfContactsFound[indexPath.row]
let itemsString = items.joinWithSeparator(" ")
cell?.contactsUserHas.text = itemsString
Another thing to note is your cast doesn't quite make a lot of sense.
var arrayExample = [["beverages", "food", "suppliers"]["other stuff, "medicine"]]
So arrayExample is of type [[String]]. I'm assuming each cell in your table view represents one of the arrays of strings in your array. So each cell represents one [String]. So your items should be arrayExample[indexPath.row]. The cast to AnyObject doesn't make too much sense. If anything you'd be casting it to [[AnyObject]], but there's no reason to because the compiler should already know it's [[String]].

'Binding' is not convertible to UILabel using SQLite in Swift

My first time around here with a Swift related question with the SQLite.Swift library.
I have a for loop for a db.prepare statement, but I am stuck when trying to assign an array value to a UILabel.
// Prepare query to retrieve the message
var _phraseMessage:String = ""
let _stmt = _db.prepare("SELECT id, message FROM messages WHERE language = 'EN' AND category = 1 and username = 'user' LIMIT 1")
for row in _stmt {
println("id: \(row[0]), message: \(row[1])")
self._phraseMessageLabel = row[1] --> i get an error here **"'Binding' is not convertible to UILabel"**
}
How can assign the value in row[1] to my UILabel? Or even better to a String variable if possible.
Thanks in advance!
Disclaimer: This is my first attempt to Swift + Third party library
You're trying to set the UILabel property itself to your value - you want to set the text property on the label to your value:
self._phraseMessageLabel.text = row[1] as! String
If you want to assign the value to a variable:
var message = row[1] as! String
One thing to note is that with both approaches, your label text or variable will only end up being set to the value for the last row returned, the one that is processed last by the for loop.
Beyond Undo's fix above, I wanted to offer a couple other solutions.
Try using Database.scalar or Statement.scalar if you're only using a single value, as in your example above.
// make sure to remove "id" from your SELECT statement
self._phraseMessageLabel.text = _stmt.scalar() as! String
Note: Make sure to remove id from your SELECT statement; scalar returns the first column of the first row only.
Use the Statement.row cursor.
_stmt.step()
self._phraseMessageLabel.text = _stmt.row[1]

Int is not convertible to 'Range<Int>'

I have a custom UITableViewCell, which contains a UIButton(upVoteButtonPressed) and a UILabel (voteScoreLabel).
In order to get the numerical scores to show up in the labels of the UITableView, the array that contains that the number of votes is a String array, although its contents are in actuality integers.
The tag of the label has been set in the tableview controller to be the same as the indexPath.row. This lets me pick a particular item in the array.
However when I want to add a specific value, add 1 for example, I cannot because when I try to execute the following code I get the following error: Int is not convertible to 'Range'.
The code below is in my custom UITableViewCell. My goal is to pick out a particular value of the array, convert it to an integer and then add 1 to it. What does this mean and how can I fix it.
#IBAction func upVoteButtonPressed(sender: AnyObject) {
groupVote[voteScoreLabel.tag]=groupVote[voteScoreLabel.tag].toInt()
}
Edit
The solution suggested in the comments to make the array type int, and then when putting into the tableview to convert to string has worked. Problem Solved.
Make the array type an int because the array is currently seen as a String type array and that is backwards and inefficient. Then when putting the array values into the UITableView use label.text = "(theIntValue)" to convert the int values into strings.

Resources