Well, basically all im trying to do is I get a list of weather from the API which is an array of each node of the array represents a weather data every 3 hours, now all I want is to append only the first 6 items into my own array to load up the tableView with them.
so my question is : how can I append only six items into my own array? :)
since you have not posted any code, here is one example of how to do it.
var i = 0
for x in apiData{
if i >= 6 {
return
} else {
myArray.append(x)
i += 1
}
}
Note: this code will only append the first 6 items no matter what they are. If you want specific 6 items in the data the code will need to be modified, you will also need to provide more information in your question
Here is another way of doing so
if myArray.count == 6 {
return
}
Related
I am trying to remove some objects from 1 array, and move them to another.
I am doing this by removing them from a reversed array, and adding them to another array, like so:
var array1 = [1,2,3,4,5,6]
var array2 = [1,2]
for (index, number) in array1.enumerated().reversed() {
if(number>2) {
array1.remove(at: index)
array2.append(number)
}
}
The problem is, the objects in array 2 are obviously reversed (1,2,6,5,4,3)
I can easily come up with complicated workarounds, but I was wondering if there are any straightforward ways of doing this.
Thanks in advance!
Rather than appending the numbers insert them
array2.insert(number, at: 2)
You can do the same thing without a loop
let droppedItems = array1.dropFirst(2)
array1.removeLast(array1.count - 2)
array2.append(contentsOf: droppedItems)
If I understand you correctly, you want to move numbers from array1 to array2 if they are higher than 2:
// get only numbers higher than 2 and append them to the second array
array2.append(contentsOf: array1.filter { $0 > 2 })
// filter the moved items from the first array
array1 = array1.filter { $0 <= 2 }
or
// split the array into two parts in place
let index = array1.partition { $0 > 2 }
// move the second part
array2 += array1[index...]
// remove the second part
array1.removeSubrange(index...)
Reverse it, grab subarray then append to array2. You don't need to mutate array1. Something like:
array2.append(contentsOf: array1.reversed()[0..<array1.count-1])
Is there a way to set the maximum index size of an array. For example I have an array of UIImage but I only want the array to store 6 images. How would I set a restriction on that array so it can only hold 6 images
There is no such functionality. You would have to implement it yourself:
if array.count < 6 {
array.append(element)
}
or perhaps:
while array.count >= 6 {
array.removeFirst()
}
array.append(element)
Initialize your array with size of 6 and then do any one of the following checks:
Check element count of the array before inserting new element
You can surround your insertion code with try / catch block with 'ArrayIndexOutOfBounds' Exception being handled
I have an array of CKRecords that I would like to sort into a three dimensional array. Within the first array is an array of dates, and each date is an array of names, where a name is an Int between 0 and 4. I'm successfully sorting my records into a two dimensional array currently (code below).
Name can be retrieved with record.objectForKey("Name") as Int
func buildIndex(records: [CKRecord]) -> [[CKRecord]] {
var dates = [NSDate]()
var result = [[[CKRecord]]]()
for record in records {
var date = record.objectForKey("startTime") as NSDate
if !contains(dates, date) {
dates.append(date)
}
}
for date in dates {
var recordForDate = [CKRecord]()
for (index, exercise) in enumerate(records) {
let created = exercise.objectForKey("startTime") as NSDate
if date == created {
let record = records[index] as CKRecord
recordForDate.append(record)
}
}
result.append(recordForDate)
}
return result
}
Not sure the best way to approach this problem. Even general guidance would be appreciated.
General Overview:
Step 1 - choose your sort algorithm. I find that the insertion sort algorithm is the easiest for me to understand and is fast too.
Step 2 - decide on your data structure. You could use a 2-dimensional array. The first dimension represents your dates, and the second dimension represents your records. So the array might be defined like this List<List<CKRecord>>. So the first entry would contain a list (List<CKRecord>) of all the records with the earliest date (it may be one or many).
Basic Steps
(with a 2-D array)
So start with the empty data structure
Figure out which Date list it should go into
If the date does not exist yet, you need to sort the date into the correct position and add a new array/list with the new entry as the only contents
If the date already exists, you need to sort the record into the correct position of the already existing list of records
Enjoy
I am experiencing one minor problem with Swift.
I am porting my old C++ code to it, and there are several places where I am using two-dimensional arrays.
Swift code now looks like:
var m_blocksSave:Array<Array<Int>>
var columns = 32
var rows = 32
//fill array with 0
for column in 0..<columns {
var columnArray = Array<Int>()
for row in 0..<rows {
columnArray.append(0)//default is 0
}
m_blocksSave.append(columnArray)
}
Then I want to check this array, find 1 for example. In fact any access to array's item creates a HUGE slowdown.
This code for example can take up to 4 seconds on my machine (regardless of environment, I can use iOS with simulator or run it as native OS X app):
for var i=0;i<columns;i++
{
for var j=0;j<rows;j++
{
let intVariable = m_blocksSave[i][j]//this thing is slow
}
}
What can I do in this situation and what is going on? Why is it so slow?
you can refer to : Most efficient way to access multi-dimensional arrays in Swift?.
In one word, 2D array is not so good right now, you need to construct your own 2D Array with 1D array.
I have a simple question and I am looking for most efficient way to deal with this.
I have a main table (say mainTableView) and it has search bar controlled by searchResultsTableView.
My main table has a mutable array say mainItems of say 10 items.
When search is performed, the searchResultsTableView may contain say 3 items in different mutable array say searchedItems
now in that search controller I deleted 2 out of 3 items and I also delete from searchedItems and searchResultsTableView. These are delete 1 at a time.
So as I delete from searchedItems I also needs to delete from mainItems to keep in sync but the index would keep changing for every delete in mainItems so how do I know the original index to be deleted in mainItems?
Should I look for some dictionary based approach instead of array?
Cant you compare the objects inside array and remove using index.
Say like this, if the item deleted from searchedItems is deletedObj
int indexToDelete = -1;
for(i=0; i<[mainItems length]; i++){
Object * myMainObj = mainItems[i];
if (myMainObj isEqual:deletedObj) //this will work depends on the object inside your datasource
{
indexToDelete = i;
break;
}
}
if(indexToDelete != -1){
[mainItems removeObjectAtIndex:indexToDelete];
}