Reputation: 1162
I create the Bus Ticket Booking App. so I have Dictionary
with the Array
in it. In which the recent Route search result is save into the NSUserDefaults
. Now I want to print that result into the one TableView
.
Now in that TableView
I only want to show the last 10
That's all I can do perfectly but the problem is every time the result is save in to the UserDefaults
so the size of the UserDefaults
is increase so I just want to remove the Defaults
So every time my array element remove
Repeat While
loop is call the number of the element in the UserDefaults
(If I i have 30 element into it it run 20 times so like). This is what i'm doing for Dictionary.
var arrSearch = [[String:Any]]()
var searchBusVC:SearchBusViewController?
override func viewDidLoad() {
super.viewDidLoad()
if let arr = UserDefault["Search"] {
arrSearch.append(contentsOf: (arr as! [[String:Any]]))
repeat{
arrSearch.removeFirst()
}while (arrSearch.count > 10)
arrSearch.reverse()
}
self.tblSearch.reloadData()
}
So I want to remove the same from the UserDefaults
Is it possible? This is my Table View Image.
Upvotes: 0
Views: 194
Reputation: 2962
If you are saving [[String: Any]]
in UserDefaults
means its like a normal way to remove objects from array. Once you done removing objects from array then save it to UserDefaults
.
var recentRecords:[[String: Any]]? {
get {
return UserDefaults.standard.array(forKey:"recentRecord")
}
set {
var trimmed: [[String: Any]] = newValue
// Trim last 10 objects from array
if newValue.count >= 10 {
trimmed = newValue.suffix(10)
}
UserDefaults.standard.set(trimmed, forKey: "recentRecord")
}
}
Upvotes: 0