How to add array of dictionaries from NSArray to Existing NSMutableArray in swift? - ios

In my project I have one NSMutableArray and NSArray named logOrderMutArray and openOrderArray, here I want to merge the openOrderArray with logOrderMutArray based on same "AccountNumber". If both array have same "AccountNumber" means I want to replace the particular index in logOrderMutArray with openOrderArray index else if "AccountNumber" was not same in some index means I want to add the openOrderArray to logOrderMutArray.
But I have made some mistakes. How can I solve the issue?
Here is my code:
func mergeOrderAndOpenOrderArrayData() {
let validateMutArray = NSMutableArray()
for i in 0..<self.logOrderMutArray.count {
var dictMutableCopy = NSDictionary()
var editDict = NSMutableDictionary()
for j in 0..<self.openOrderArray.count {
var newDict = NSDictionary()
if let logOrderfips:NSNumber = (logOrderMutArray[i] as AnyObject).value(forKey: "FIPS") as? NSNumber {
if let openOrderfips:NSNumber = (openOrderArray[j] as AnyObject).value(forKey: "FIPS") as? NSNumber {
if let logOrderAccNum:String = (logOrderMutArray[i] as AnyObject).value(forKey: "AccountNumber") as? String {
if let openOrderAccNum:String = (openOrderArray[j] as AnyObject).value(forKey: "AccountNumber") as? String {
if logOrderfips == openOrderfips && logOrderAccNum == openOrderAccNum {
dictMutableCopy = logOrderMutArray[i] as! NSDictionary
editDict = NSMutableDictionary(dictionary: dictMutableCopy)
newDict = openOrderArray[j] as! NSDictionary
editDict["newlyAdded"] = newDict
logOrderMutArray.replaceObject(at: i, with: editDict)
}
else {
logOrderMutArray.add(openOrderArray[j])
}
}
}
}
}
}
}
}

Related

Efficient way to Insert and Update a core data object [2020]

Currently I am checking whether the object already exists in core data based on id and then updating and inserting. Is there any better way to do it? Have added "id" as a unique constraint, Which prevents inserting of objects with same "id". Does inserting just update the existing object with same id?
#nonobjc public class func saveUserMovies(movieJSON: [[String: Any]], user: UserProfile, isFavorites: Bool = false, isWatchlisted: Bool = false) {
let context = MMPersistentStore.sharedInstance.privateManagedObjectContext
for movie in movieJSON {
let movieID = movie["id"] as! Int
let fetchMovieWithIDRequest = fetchMovieRequest()
let moviePredicate = NSPredicate(format: "id == %d", movieID)
let sortDiscriptor = NSSortDescriptor(key: "id", ascending: false)
fetchMovieWithIDRequest.sortDescriptors = [sortDiscriptor]
fetchMovieWithIDRequest.predicate = moviePredicate
var userMovie: UserMovie?
context.performAndWait {
do {
userMovie = try fetchMovieWithIDRequest.execute().first
} catch {
print(MMErrorStrings.coreDataFetchError)
}
}
if let fetchedMovie = userMovie {
fetchedMovie.genreIds = movie["genre_ids"] as? [Int64]
fetchedMovie.adult = movie["adult"] as? Bool ?? false
if isFavorites {
fetchedMovie.isFavorite = isFavorites
} else {
fetchedMovie.isWatchlisted = isWatchlisted
}
fetchedMovie.video = movie["video"] as? Bool ?? false
fetchedMovie.backdropPath = movie["backdrop_path"] as? String
fetchedMovie.originalLanguage = movie["original_language"] as? String
fetchedMovie.originalTitle = movie["original_title"] as? String
fetchedMovie.overview = movie["overview"] as? String
fetchedMovie.posterPath = movie["poster_path"] as? String
fetchedMovie.releaseDate = movie["release_date"] as? String
fetchedMovie.releaseYear = String(fetchedMovie.releaseDate?.prefix(4) ?? "")
fetchedMovie.title = movie["title"] as? String
fetchedMovie.popularity = movie["popularity"] as? Double ?? 0.0
fetchedMovie.voteCount = movie["voteCount"] as? Int64 ?? 0
fetchedMovie.voteAverage = movie["voteAverage"] as? Double ?? 0.0
MMPersistentStore.sharedInstance.save(context: context)
} else {
let fetchedMovie = UserMovie(context: context)
fetchedMovie.id = movie["id"] as? Int64 ?? 0
fetchedMovie.user = user
fetchedMovie.genreIds = movie["genre_ids"] as? [Int64]
fetchedMovie.adult = movie["adult"] as? Bool ?? false
if isFavorites {
fetchedMovie.isFavorite = isFavorites
} else {
fetchedMovie.isWatchlisted = isWatchlisted
}
fetchedMovie.video = movie["video"] as? Bool ?? false
fetchedMovie.backdropPath = movie["backdrop_path"] as? String
fetchedMovie.originalLanguage = movie["original_language"] as? String
fetchedMovie.originalTitle = movie["original_title"] as? String
fetchedMovie.overview = movie["overview"] as? String
fetchedMovie.posterPath = movie["poster_path"] as? String
fetchedMovie.releaseDate = movie["release_date"] as? String
fetchedMovie.releaseYear = String(fetchedMovie.releaseDate?.prefix(4) ?? "")
fetchedMovie.title = movie["title"] as? String
fetchedMovie.popularity = movie["popularity"] as? Double ?? 0.0
fetchedMovie.voteCount = movie["voteCount"] as? Int64 ?? 0
fetchedMovie.voteAverage = movie["voteAverage"] as? Double ?? 0.0
MMPersistentStore.sharedInstance.save(context: context)
}
}
}
}
Have added "id" as a unique constraint, Which prevents inserting of objects with same "id".
I didn't use it before yet
Does inserting just update the existing object with same id?
No, it'll insert the new object.
For your case, you could make a refactoring, please refer the findOrCreate function in https://github.com/objcio/core-data/blob/master/SharedCode/Managed.swift
It'll help you avoid duplicated code.
One more thing, your request doesn't need the sortDescriptor, and it should have the limit = 1, and returnObjectAsFaults = false for optimisation.
After that, you just need to make sure your function is called in the same context to avoid duplications.

I am trying to get an icon array to display in my weather app, but can not seem to get UIImage to display them

This is my code so far, with no errors, but it is not picking the dates from the 5 day forecast. What is wrong in this code?
//: to display the 5 day date array from the open weather API
enter code herevar temperatureArray: Array = Array()
var dayNumber = 0
var readingNumber = 0
if let jsonObj = try? JSONSerialization.jsonObject(with: data, options: .allowFragments) as? NSDictionary {
if let mainArray = jsonObj!.value(forKey: "list") as? NSArray {
for dict in mainArray {
if let mainDictionary = (dict as! NSDictionary).value(forKey: "main") as? NSDictionary {
if let temperature = mainDictionary.value(forKey: "temp_max") as? Double {
if readingNumber == 0 {
temperatureArray.append(temperature)
} else if temperature > temperatureArray[dayNumber] {
temperatureArray[dayNumber] = temperature
}
} else {
print("Error: unable to find temperature in dictionary")
}
} else {
print("Error: unable to find main dictionary")
}
readingNumber += 1
if readingNumber == 8 {
readingNumber = 0
dayNumber += 1
}
var dateArray: Array<String> = Array()
var dayNumber = 0
var readingNumber = 0
if let weatherArray = jsonObj!.value(forKey: "list") as? NSArray {
for dict in weatherArray {
if let weatherDictionary = (dict as! NSDictionary).value(forKey: "list") as? NSDictionary {
if let date = weatherDictionary.value(forKey: "dt_txt") as? String {
if readingNumber == 0 {
dateArray.append(date)
} else if date > dateArray[dayNumber] {
dateArray[dayNumber] = date
}
}
} else {
print("Error: unable to find date in dictionary")
}
readingNumber += 1
if readingNumber == 8 {
readingNumber = 0
dayNumber += 1
}
}
}
}
}
}
func fixTempForDisplay(temp: Double) -> String {
let temperature = round(temp)
let temperatureString = String(format: "%.0f", temperature)
return temperatureString
}
DispatchQueue.main.async {
self.weatherLabel1.text = "Today: (fixTempForDisplay(temp: temperatureArray[0]))°C"
self.weatherLabel2.text = "Tomorrow: (fixTempForDisplay(temp: temperatureArray[1]))°C"
self.weatherLabel3.text = "Day 3: (fixTempForDisplay(temp: temperatureArray[2]))°C"
self.weatherLabel4.text = "Day 4: (fixTempForDisplay(temp: temperatureArray[3]))°C"
self.weatherLabel5.text = "Day 5: (fixTempForDisplay(temp: temperatureArray[4]))°C"
func formatDate(date: NSDate) -> String {
let dateFormatter = DateFormatter()
dateFormatter.dateStyle = .medium
return dateFormatter.string(from: date as Date)
}
self.dateLabel1.text = ": \(formatDate(date: dateArray[0]))"
self.dateLabel2.text = ": \(formatDate(date: dateArray[1]))"
self.dateLabel3.text = ": \(formatDate(date: dateArray[2]))"
self.dateLabel4.text = ": \(formatDate(date: dateArray[3]))"
self.dateLabel5.text = ": \(formatDate(date: dateArray[4]))"
}
}
}
dataTask.resume()
}
}
It looks to me like you need to change your icon array to contain strings
var iconArray: Array<String> = Array()
and then when parsing the json text
if let icon = weatherDictionary.value(forKey: "icon") as? String {
and finally
self.iconImage1.image = UIImage(named: iconArray[0])
self.iconImage2.image = UIImage(named: iconArray[1])
Of course the below comparison won't work anymore when icon is a string but I don't understand any of this if/else clasue so I don't know what to replace it with
if readingNumber == 0 {
iconArray.append(icon)
} else if icon > iconArray[dayNumber] { //This won't work now.
iconArray[dayNumber] = icon
}

Loop is taking too long to process

I am using following to code to arrange the array of contacts in sections (for e.g. contact with prefix "A" should show under "A" section). If there are contacts 4-5 hundred then it takes 20 sec to process.
Can you please check what is issue here? or any alternate for this.
let finalArr = NSMutableArray()
for contactDetail in conatctsArr {
let name = (contactDetail as! Dictionary)["givenName"] ?? ""// Getting First character of name
var key = String()
if name.characters.count > 0 {
let index1 = name.index((name.startIndex), offsetBy: 1)
key = name.substring(to: index1)
}
else {
key = ""
}
// getting all contatcts starting with particular character
let predicate=NSPredicate(format: "SELF.givenName beginswith[c] %#",key)
let filteredArr = (conatctsArr as NSArray).filtered(using: predicate)
var dic = Dictionary<String, Any>()
dic["key"] = key
dic["values"] = filteredArr
if filteredArr.count > 0 && !(finalArr.contains(dic)) {
finalArr.add(dic)
}
}
Removing the filtered elements from the array after processing in each loop might improve the performance. Try:
let finalArr = NSMutableArray()
var processingArray = NSMutableArray(array:conatctsArr)
while processingArray.count > 0 {
let contactDetail = processingArray[0]
let name = (contactDetail as! Dictionary)["givenName"] ?? ""
var key = String()
if name.characters.count > 0 {
let index1 = name.index((name.startIndex), offsetBy: 1)
key = name.substring(to: index1)
}
else {
key = ""
}
let predicate=NSPredicate(format: "SELF.givenName beginswith[c] %#",key)
let filteredArr = processingArray.filtered(using: predicate)
if filteredArr.count > 0 {
var dic = Dictionary<String, Any>()
dic["key"] = key
dic["values"] = filteredArr
finalArr.add(dic)
}
processingArray.removeObjects(in: filteredArr)
}
In your code, the filtering is being done multiple times for the same key. Try :
let finalArr = NSMutableArray()
for contactDetail in conatctsArr
{
let keysArray = [“A”, “B”, “C”,…. “Z”]
for key in keysArray
{
let predicate=NSPredicate(format: "SELF.givenName beginswith[c] %#",key)
let filteredArr = (conatctsArr as NSArray).filtered(using: predicate)
var dic = Dictionary<String, Any>()
dic["key"] = key
dic["values"] = filteredArr
if filteredArr.count > 0) {
finalArr.add(dic)
}
}
}

Could not cast value of type '__NSSingleObjectArrayI' to 'NSDictionary' in Swift

When I parse the Api I got response and stored full response to Dictionary then I split it to array and dictionary, But I get error so please help to solve the error,
Here I give the code what I am tried.
let responseDictionary:NSDictionary = responseObject as! NSDictionary
print(responseDictionary)
if let statusCode:NSNumber = responseDictionary.value(forKey: "success") as? NSNumber{
if statusCode == 1{
print("statusCode :",statusCode)
let incomingRequetArray:NSArray = responseDictionary.value(forKey: "incoming_requests") as! NSArray
print("incomingRequetArray",incomingRequetArray)
if (incomingRequetArray.count > 0)
{
let requestDataDict:NSDictionary = incomingRequetArray.value(forKey: "request_data") as! NSDictionary -----> Error in this line
print("requestDataDict",requestDataDict)
let ownerArray:NSArray = requestDataDict.value(forKey: "owner") as! NSArray
print("ownerArray",ownerArray)
self.mutDictOwner = ownerArray.object(at: 0) as! NSMutableDictionary
}
}
responseDictionary is
"incoming_requests" = (
{
datetime = "2017-04-17 12:25:58";
later = 0;
"request_data" = {
dog = (
);
owner = {
address = "";
"d_latitude" = "0.00000000";
"d_longitude" = "0.00000000";
"dest_latitude" = "0.00000000";
"dest_longitude" = "0.00000000";
latitude = "11.01449160";
longitude = "76.98273220";
name = "Sss Sas";
"num_rating" = 0;
"payment_type" = 1;
phone = "+919123456789";
picture = "";
rating = "0.00";
};
"payment_mode" = 1;
};
"request_id" = 1474;
"request_services" = 30;
"time_left_to_respond" = 21;
}
);
"is_approved" = 1;
"is_approved_txt" = Approved;
"is_available" = 1;
success = 1;
It is because your "request_data" is an Object and not a Dictionary, see how "incoming_request" has started (with a " ( ") and "request_data" is starting with "{". what you can do to access is this
let requestDataDict:NSDictionary = incomingRequetArray.value(forKey: "request_data") as! [AnyObject]-----> Error in this line
print("requestDataDict",requestDataDict)
let x = requestDataDict[0] as! NSDictionary
let ownerArray:NSArray = x.value(forKey: "owner")as! NSArray
your are doing wrong incomingRequetArray is NSArray and you are getting value for key
Write this
let requestDataDict:NSDictionary = incomingRequetArray[0]as!NSDictionary
let newDict: NSDictionary = requestDataDict.object(forKey: "request_data")
print("newDict is ---%#", newDict)
you have used NSDictionary and NSArray format So I have written the code according to yours. But for further uses in swift Do not use NSDictionary and NSArray.
This is a reasonable Swift transcription which solves the error. It uses a type alias for convenience.
typealias JSONDictionary = [String:Any]
let responseDictionary = responseObject as! JSONDictionary
print(responseDictionary)
if let success = responseDictionary["success"] as? Int, success == 1 {
print("statusCode :", success)
let incomingRequestArray = responseDictionary["incoming_requests" as! [JSONDictionary]
print("incomingRequetArray",incomingRequestArray)
if let requestDataDict = incomingRequestArray.first {
print("requestDataDict",requestDataDict)
if let ownerArray = requestDataDict["owner"] as? [JSONDictionary], !ownerArray.isEmpty {
print("ownerArray",ownerArray)
self.mutDictOwner = ownerArray[0] as! JSONDictionary
}
}
In the line that's throwing an error try to change incomingRequetArray.value... to incomingRequetArray[0].value... as you need to access the internal dictionary that's contained in an array and then query for the value for given key.

Why can I not access the second level of this array in Swift?

I have the following array which is passed from an API call from a PHP Script:
["playerForm": {
1 = (
{
date = "2017-01-31";
name = Dicky;
result = L;
"results_id" = 42;
},
{
date = "2016-12-24";
name = Dicky;
result = W;
"results_id" = 19;
}
);
2 = (
{
date = "2017-01-25";
name = G;
result = W;
"results_id" = 38;
},
{
date = "2017-01-25";
name = G;
result = D;
"results_id" = 40;
},
{
date = "2017-01-21";
name = G;
result = L;
"results_id" = 34;
}
);
3 = (
{
date = "2017-01-31";
name = Sultan;
result = W;
"results_id" = 42;
},
{
date = "2017-01-15";
name = Sultan;
result = L;
"results_id" = 30;
}
);
}]
However I cannot seem to access the 1,2,3 parts of it...
let json = try JSONSerialization.jsonObject(with: data!, options: .allowFragments) as? [String:AnyObject]
print (json!)
if let dict = json?["playerForm"] as? [String:AnyObject] {
print ("step 1")
if let arr = dict["1"] as? [[String:String]] {
print ("step 2")
self.leagueForm = arr.flatMap { Form($0) }
print (self.leagueForm)
print (self.leagueForm.count)
for i in 0..<self.leagueForm.count {
let form = self.leagueForm[i]
print (form.player_results!)
self.formGuide.append(form.player_results!)
}
print ("now")
print (self.formGuide)
self.resultsDisplay.results = self.formGuide
self.resultsDisplay.results = self.resultsDisplay.results.reversed()
self.resultsDisplay.displayResults()
}
}
With this code above it only gets as far as Step 1.
What am I doing wrong?
UPDATED WITH PROGRSS*
let json = try JSONSerialization.jsonObject(with: data!, options: .allowFragments) as? [String:AnyObject]
print (json!)
if let dict = json?["playerForm"] as? [String:AnyObject] {
print ("step 1")
for (_ , value) in dict {
if let arr = value as? [[String:Any]] {
print(arr)
//your code
}
}
}
Custom Struct to define array:
struct Form {
var player_result: String?
var player_name: String?
var result_date: String?
var result_id: String?
init(_ dictionary: [String : Any]) {
self.player_result = dictionary["result"] as? String ?? ""
self.player_name = dictionary["name"] as? String ?? ""
result_date = dictionary["date"] as? String ?? ""
result_id = String(dictionary["results_id"] as? Int ?? 0)
}
}
Change your array type [[String:String]] to [[String:Any]] because it contains both String and Number as value.
if let arr = dict["1"] as? [[String:Any]] {
print(arr)
//your code
}
Note: You need to loop through the dict Dictionary because its each key having array.
for (_ , value) in dict {
if let arr = value as? [[String:Any]] {
print(arr)
//your code
}
}

Resources