Append does more than wanted - swift

I have a tabbar view controller with three tabs. I have implemented a global array in this tabBar controller in order to make it accessible from all the tabs. It is an array of type [[[Any]]]. I wanted to modify this array from the first tab by appending an element but the problem is that instead of just appending the element, it modifies the existing element too. Can you help me pls ? Thanks.
Here is the definition of the array :
var invoices: [[[Any]]] = []
Here is the appending :
let tabBar = tabBarController as! baseTabBarController
tabBar.invoices.append(cells)
P.S: The append is done in a view controller connected to the first view controller (child of tabbar view controller) by a segue link.

You really need to give more information to get more personalised answers.
However, your Array is a three - dimensional - array, which means that you have an array inside an array inside an array.
When performing the append Method on an array, it always adds a new element to the end of this array.
In your case: The element that is added is of type [[Any]], which is a two - dimensional - array.
NOTE: More - dimensional - arrays are a bit tricky. What your code does is appending a new two - dimensional - array at the end of your outer array, not adding a new Any to your inner array!
However, the append Method will never modify existing Elements, so please check your syntax again, since more - dimensional - arrays are tricky.

Related

How to count element of one controller into another

i've a question to ask for my mvc project...
i have two controllers: Giurisprudenza e GiurisprudenzaNode, these controllers implement an index with the list of elements and a crud system to add/edit/remove elements. In the Girusiprudenza view page every element has a button that return you into the relative GiurisprudenzaNode page, with the list of elements contained into the primary element.
I would like to insert a label next to every element of the list in Giurisprudenza that say how many elements there are into that single primary element, but i don't know how to do that count... Anybody can help me?
for this you dont need two controllers or two different view .
For this you need to follow below steps.
Create a viewModel (Customize mix of both models)
add custom property in you this viewmodel for counts
create new view for this ViewModel class .
Let me know if you need code snippet for this.
View Model from MSDN

Using {{each}} block to iterate through an array backwards in DerbyJS

In a DerbyJS component, I have an array items, which I want to display in the view. I want to display the latest items of the array first, so I am looking for something like each-reverse:
{{each-reverse items as #item}}
<p>{{#item}}</p>
{{/each-reverse}}
. Of course, this snippet of code does not work but I hope it explains what I want to achieve. Is there a way to do this in DerbyJS?
Theoretically I could reverse the items array, but I do not want to do this, because sometimes I update this array by using .push. Using .unshift instead seems to be suboptimal.
I'm pretty sure there is no each-reverse method that you can use in the view. One way to achieve what you want is to first reverse sort the list in the controller.
If you use a Derby sort filter to reverse the items in the array, you could still use push to update the original array like you desire.
#originalList = #model.at 'originalList'
#reversedList = #model.ref 'reversedList', #model.sort 'originalList', (a, b) ->
return a - b
// add new items to the list
#originalList.push 4
// in the view
{{each reversedList as #item}}
<p>{{#item}}</p>
{{/each}}
Alternatively, you could try
// in the view
{{each originalList as #item, #index}}
{{originalList[originalList.length - #index]}}
{{/each}}

Access multiple cells within cell array

for i=1:30
a{i}=rand(2,2);
end
a{[6 23]}=[] %get an error here
How do I access element 6 and 23 efficiently?
If you want to assign emptys array to the contents of those two cells, you can use the brackets ([]) and deal:
[a{[6 23]}]=deal([])
If instead you want to remove those two cells entirely, use parentheses:
a([6 23])=[]
The reason a{[6 23]}=[] gives an error is because accessing a cell array that way returns a comma-separated list of the cell contents. In other words, doing [a{[6 23]}] is like doing [a{6},a{23}].

NSSortDescriptor/NSPredicate to organize my tableView

i have a table view. In the table view, there is a custom UILabel, And each label with have a letter. "A, B, C, D" etc.. You can hit a plus button on the nav bar and it bring you to another view controller to enter info, and you hit save, and it adds the row onto the table view. Is there a way to utilize NSSortDescriptor/NSPredicate to make all the "A"'s together (together meaning i want all the cells with a to be with each other, but i still want them in the same section. ) if that makes sense! Is there a way to do this? Im using Core Data to save all my information. Any help is appreciated. Thanks in advance
I assume you are using NSFetchedResultsController. If not, please refactor and do.
The section grouping is anyway taken care of by the key path during the initialization of the fetched results controller with the argument sectionKeyPath.
As the sorting does not change, simply add a sort descriptor to the fetch request before initializing the fetched results controller:
fetchRequest.sortDescriptors = #[[NSSortDescriptor
sortDescriptorWithKey:#"textAttributeName" ascending:YES]];
where textAttributeName is the whatever you called your A B C label attribute in your entity.

Xcode Obj-C: Make new NSArray from one key each out of an Array of Dictionaries

This actually could be a multipart question. But here's the first part ...
I have an array (actually in a plist) of dictionaries. Each dictionary has 3 keys in it: (title), (points), and (description).
I am trying to make a NEW array with the values of the key "title" from each dictionary in that first array.
Let me explain WHY I am doing this and maybe that will provide a better all around explanation.
I am trying to let people pick from a pre-determined list. Heck, if this was a web page it would be very simple since all I really care about are the "points" and the "Title". On a web site I could simply do a drop down combo-box with the "points" being the value and the title being the text for each row.
But this is not a web page.
So what I am trying to do here is pop out a modal picker when they click the text field. The modal picker shows the alphabetical ordered "titles" from our new array. And whichever one they select, it closes the modal view and assigns that "title" text to the UITextField which cannot be edited by the user.
I have some code to get my modal picker to pop out. But I need to feed it an array of just the "titles" of each dictionary in my real array.
[array valueForKeyPath:#"title"]
That should return a new array with the result of calling [obj valueForKey:#"title"] on each object in the array.
In this case, you should be able to just use
[array valueForKey:#"title"]
as well, but if you wanted to go further into the dictionary (like "person.name" or something), the key-path version is very nice.