Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

erdinc27

macrumors regular
Original poster
Jul 20, 2011
168
1
Hi all. I have a problem. Maybe it is so easy for the people here but I couldn't solve it. I need help. I have let's say 380 dictionaries in an I want to put them in different arrays 10 by 10. Something like from 0 to 9 into an array, from 9 to 17 into another array and so on. How it could be done in Swift?
 
Hi all. I have a problem. Maybe it is so easy for the people here but I couldn't solve it. I need help. I have let's say 380 dictionaries in an I want to put them in different arrays 10 by 10. Something like from 0 to 9 into an array, from 9 to 17 into another array and so on. How it could be done in Swift?

Interesting problem. A few items:

1. I don’t understand the exact problem. It would be helpful if you could first restate the problem more precisely, and second provide a few small examples.

2. You asked for the solution in Swift. Is there and existing solution in another language or psudeo-code?
 
Interesting problem. A few items:

1. I don’t understand the exact problem. It would be helpful if you could first restate the problem more precisely, and second provide a few small examples.

2. You asked for the solution in Swift. Is there and existing solution in another language or psudeo-code?

1. I have an array that. This array holds 340 dictionaries inside. I want to divide that 340 dictionaries into small arrays. Something like first 10 elements will be a new array and second 10 elements will be another array. Long story short I need to get 34 new arrays from the beginning array with 340 elements. I hope it is more clear now.

2. There is no any other solution in any other languages. I wrote it just because someone may ask in which language I am writing.
 
OK you have an array of dictionary objects.

From your answer you will have to use what known as "brute force" to loop through your array, exam each one of the dictionaries and build your own indexes. Essentially you will be building arrays of indexes that point to your dictionary objects.

The NSArray can do simple indexes of the dictionaries but not at the multiple levels you seem to need.

What you really need is a relational database. While I don't code in Swift (Objective C) you can use the built-in database capabilities of SQL or go further and use Core Data. An array of dictionaries just won't do well what you want.

There are many helps on-line with relational databases for iOS so do a search and see what others have done.

Or maybe just do a fast for loop and generate your 34 arrays using a counter to fill each new array.
Again brute force and not graceful.


int i = 0;
for(dictionary in array){



}

***** Changes*****

OK you have an array of objects (NSDictionary) you want to make into arrays of ten objects. Again loop through the original array and create a new array every ten objects.

Best I can offer you.
 
Last edited:
With Swift, you just use the stride to step 10 at a time and then map those steps into groups. It shouldn’t take more than a line or two
 
Thanks for the help guys. @Mascots can you share code about what you suggested? I would like to learn that easy way
 
Last edited:
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.