Is my array empty or not? - ios

I have an array that becomes loaded with objects when I call it in my viewDidLoad method. I know that it is not empty because I am able to print the data and the count of the array when I call it in viewDidLoad.
However, when I try to take the data out of the array and put it into a text view, the array appears to be empty. The code below is where I attempt to add the items from requestList2 into a textfield in tableView:cellForRowAtIndexPath:.
Why does does my array possess data when I call it in viewDidLoad, but not when I try to add it to cell.usernameOfRequestSender.text?
if (requestList2.count > 0){
var user = requestList2.objectAtIndex(indexPath.row) as! String
cell.usernameOfRequestSender.text = user
}
return cell

Related

Display correct objects/keys from array in string?

I'm trying to display items from an array in my tableView cells, in a string. Currently, I'm using these lines of code:
.m
id swaptime = self.filteredTotal[0][#"swaptime"];
NSString *test = swaptime;
...to grab the value located in swaptime, and display it in each of my cells. That said, in all of my cells, this line forces them all to display only the FIRST swaptime value - rather than the list of swaptime values from my array in their corresponding cells. Is this because of the [0] in my line (e.g. only grab the value located at 0)? If so, how should it be written instead so that all values are displayed?
I'm not sure of the answer since you have not provided all of your code, but if the code shown above is from the cellForRowAt method of the UITableView datasource, then this should work:
id swaptime = self.filteredTotal[indexPath.row][#"swaptime"];
(provided you do not intend on having more than 1 section)

Displaying data in a tableView from a filtered array

I have written code that filters events from Core Data and only prints the events that have a date attribute that is equal to a date that was selected in a Calendar. Heres the code:
let predicate = NSPredicate(format: "eventPastStringDate = %#", formatter.stringFromDate(selectedDate))
//This prints the exact data that I want
print((eventsPast as NSArray).filteredArrayUsingPredicate(predicate))
This works and it filters that data how I would like and it prints only the events that I want. The problem is that I do not know how to display this data in the tableView. Usually I can display all the data from Core Data in the tableView like this in cellForRowAtIndexPath...
let ePast = eventsPast[indexPath.row]
let cellPast = self.tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! CustomCell
// Sets labels in cell to whatever user typed in on the events page
cellPast.titlePrototypeCell!.text = ePast.eventPastTitle!
cellPast.datePrototypeCell!.text = "Date: " + ePast.eventPastDateLabel!
return cellPast
...but I am not sure how to access the new data as an array like I did above. Any ideas?
You are going to have a second array in your class. When there is no filter, the two arrays are identical. When there is a filter, then you have a master array with absolutely everything and the filtered array is a small subset.
(class var) myFilteredArray = (eventsPast as NSArray).filteredArrayUsingPredicate(predicate)
Then in your tableview methods:
let ePast = myFilteredArray[indexPath.row]
And make sure to set the table row size to your filtered array count, not the master array count. Otherwise you are going to get out of bounds crashes.

How can I access an item from an array dynamically with Swift

I've got an Array that I'm using to populating the rows of a UITableView.
After a row is selected I need to retrieve information from the Array based on the row selected to populate some outlets (labels, textfields, etc.)
For example:
I create an itemSelected variable in the didSelectRowAtIndexPath in my ViewController for the TableView which I set to indexPath.row
itemSelected = indexPath.row
Then in my viewDidLoad for my otherViewController I need to retrieve the info by
array[itemSelected]
But, I get a compiler error that says: "Expression resolves to unused i-value"
In here you simply accessing the array but not calling any value. As a example if you have a key call "Name" in your array and you want to set it to a UILabel just do it as this.
self.Name.text = array[itemSelected].valueForKey("Name") as! String
if not just do something with it.
self.Name.text = array[itemSelected] as! String
OR
print(array[itemSelected])

Remove objects from an array before calling the API

I'm creating a table view which is hooked to an API. However, I'm having trouble with doing a refresh on pull. I've added the logic, however I can't seem to delete all the objects in the array before making a new api call.
Here is my array
var recentArray = Array<News>()
UIRefreshControl function:
func refresh(sender: UIRefreshControl){
lastObjectIndex=0
// remove all objects
getRecent()
self.tableVIew.reloadData()
self.refreshControl?.endRefreshing()
}
How can i remove all objects in my array before calling getRecent, which adds an object to the array?
You can reset your array like this:
recentArray = []
The compiler already knows the type of the array objects, so there's no need to do anything else.
You can remove all objects by calling
recentArray.removeAll(keepCapacity: false)
You can removed all object by adding following code befor getRecent called.
var array = [0, 1, 2, 3]
array.removeAll()
let count = array.count
// count is 0
Hope this will help you.

cannot insert into NSMutableArray due to indexvalue being out of bounds

I have a MutableArray on this view called array and the object in question is detailItem, which has a property of rank (int). On this view, there's a text field displaying the rank and I want to be able to move the detailItem up and down the MutableArray by changing the rank.
So, for example let's say the detailItem has a rank of 3, which is index value of 2. If I change this in the text field to 3, I want the array to adjust and move it down one place. However, as I type in the value of rankField (the text field), it crashes the app since it automatically updates the value before I'm done editing. So, if I click on the text field and write 23 (planing on deleting the 2) or just press delete (now the value is nil) the app crashes with an uncaught exception.
Here's the code:
- (IBAction)rankFIeldTextChanged:(id)sender {
QueueMember *member = self.detailItem;
[self.array removeObjectAtIndex:self.detailItem.rank];
if (0<= [self.rankField.text intValue]<= self.array.count) {
[self.array insertObject:member atIndex:[self.rankField.text intValue]-1];
}
}
The if condition of making the text value in-between the array size and 0 seems to have no effect.
btw this is all in the detailsViewController which is connected to the main view controller via push segue. does it make more sense(or more better coding) to just set the new rank value in details and actually make the array changes in the mainviewcontroller.m?
The problem is that you are trying to do two boolean statements at once (which doesn't work). Change your if statement to something like:
if (0< [self.rankField.text intValue] && [self.rankField.text intValue] < self.array.count) {
//Insert your object here
}
else
{
//Add object here
}
Your current setup check to see if 0<= [self.rankField.text intValue], which will return true for all values greater than or equal to 0. Then it checks the result of that (YES:1, NO:0) if it's less than or equal to your array count. That will always return true if your array has anything in it. So basically your check will always return true.
Since it always returns true I could check for array object number 1000, your if statement says go for it, then I check and the array says "No way in heck!" and crashes your app.
EDIT: Updated my code snippet to take into account your array insertion line.
I'd just do this
- (IBAction)rankFIeldTextChanged:(id)sender {
QueueMember *member = self.detailItem;
[self.array removeObjectAtIndex:self.detailItem.rank];
if ((0<= [self.rankField.text intValue])&&([self.rankField.text intValue]<= self.array.count)) {
if (self.array.count >([self.rankField.text intValue]-1)){
[self.array insertObject:member atIndex:[self.rankField.text intValue]-1];
}
}
}
you are probably trying to insert at an index greater than the count of the array.

Resources