Fetch Core Data as comma separated list - arrays

I realise that I am missing something simple but as a Swift newbie I am going around in circles & would appreciate a pointer as to what I am doing wrong?!
I have a Core Data Entity called "Numbers" with an attribute (Int16) called "userNumbers". I am fetching the results like:
let appDelegate = UIApplication.shared.delegate as! AppDelegate
let context = appDelegate.persistentContainer.viewContext
let request = NSFetchRequest<NSFetchRequestResult>(entityName: "Numbers")
//request.predicate = NSPredicate(format: "age = %#", "12")
request.returnsObjectsAsFaults = false
do {
let result = try context.fetch(request)
for data in result as! [NSManagedObject] {
print("\(data.value(forKey: "userNumbers") as! Int16)")
}
} catch {
print("Failed")
}
The result in my console is:
12
13
18
19
21
I need to know how to make this a comma separated list so I can use it in an array. Essentially I need the return to be: 12,13,18,19,21
Everything I try seems to be wrong!

First of all create a more specific fetch request to get a distinct result type
let request = NSFetchRequest<Numbers>(entityName: "Numbers")
A comma separated list is not possible because the type of userNumbers is numeric.
You can map the result to an array of Int16 with
do {
let result = try context.fetch(request) // the type is [Numbers]
let numberArray = result.map{$0.userNumbers}
print(numberArray)
}

Related

Add values from multiple dictionaries in firebase into an Int

I'm using firebase to store the amount of views every video in my app has been seen. What I want to is to gather all views from from one users all videos and display the total number of views. However I'm having problems fetching down the data and putting all the dictionary values together into a Int/String!
Ive tried many different solutions so far, but still I get all the different values in like array / values of the dictionary instead of everything added into one value
This is my code for getting all the videoviews of a specific user, no problems with this so far. When I print "intConvert" I get like all the views in different rows.
let ref = Database.database().reference().child("videoviews").child(stringUid)
ref.observe(.value, with: { (snapshot) in
if let dictionary = snapshot.value as? [String: AnyObject]{
let numbOfViews = dictionary["views"] as! String
let intConvert = Int(numbOfViews)!
let ArrayViews = [intConvert]
This is my database structure:
**videoviews**
-Lb52VxrEqdSRGljJdP7
views: "25"
-Lb53ARq_lOHEbTruW8s
views: "273"
-Lb53A_cEyX3CYc4mKYn
views: "38"
EDIT: If I do print(dictionary), the dictionary from "if let dictionary = snapshot.value as? [String: Anyobject] looks like this:
["views": 642]
["views": 660]
["views": 628]
["views": 630]
["views": 615]
["views": 3]
["views": 0]
["views": 2]
["views": 1]
Edit: (I was confused and forgot to add the bracelets, sorry for that.)
when I do I "print(dictionary.values) the console looks like this (the key values from different dictionaries):
[642]
[660]
[628]
[630]
[615]
[3]
[0]
[2]
[1]
I then tried to put this together in a loop like this:
var bLoader = 0.0
for hejArray in 0...ArreyViews.count-1{
bLoader += Double(Arrej[hejArray])
}
But when I print "bLoader" I still get all the views for every video in different rows instead of gathered in one value.
So what do I need to do put together all the values from different dictionaries in Firebase into one Variable?
I appreciate all help I can get with this, I can imagine it shouldn't be too hard but that im missing out on something.
EDIT /// I finally found the problem. the "StringUid" that I passed in have different amount of values and therefore the whole function would be called for 9 times if the videos of the user had the amount of 9. The solution that finally worked looked like this:
Global Array declaration:
var myArray = [String]()
Inside the function:
if let dictionary = snapshot.value as? [String: AnyObject]{
let numbOfViews = dictionary["views"] as! String
let intConvert = Int(numbOfViews)!
self.myArray.append(numbOfViews)
let intArray = self.myArray.map { Int($0)!}
let total = intArray.reduce(0, +)
self.totalViewsShower2.text = String(total)
Thank you in advance!
Update
If you can directly print the values from your dictionary like that then the solution might be as easy as
let total = dictionary.values.reduce(0, +)
or if values are strings you need to convert them to Int
let total = dictionary.values.reduce(0) {sum, value -> Int in return sum + (Int(value) ?? 0)}
If on the other hand they values are string but defined as Any you need an extra cast
let total2 = dictionary2.values.reduce(0) {sum, value -> Int in
if let str = value as? String {
return sum + (Int(str) ?? 0)
}
return 0
}
I am not exactly sure what your dictionary contains but I assumed something like this
let dictionary: [String: Any] = ["views": ["1", "2", "3"]]
Then you can cast the value for the "views" key to a String array and then use reduce on that array to get the sum
let array = dictionary["views"] as! [String]
let total = array.reduce(0) {sum, value -> Int in return sum + (Int(value) ?? 0)}
According to your Database structure which looks something like this
{
"videoviews": {
"Lb52VxrEqdSRGljJdP7": {
"views": "25"
},
"Lb53ARq_lOHEbTruW8s": {
"views": "273"
},
"Lb53A_cEyX3CYc4mKYn": {
"views": "38"
}
}
}
let ref = Database.database().reference().child("videoviews").child(stringUid)
ref.observe(.value, with: { (snapshot) in
if let dictionary = snapshot.value as? [String: AnyObject] {
let viewsArray = dictionary.values
.compactMap { $0 as? [String: String] }
.compactMap { Int($0["views"] ?? "") }
let totalViews = viewsArray.reduce(0, +)
}
})

How to store multiple datas in an Array in swift 4?

I am getting a JSON from an API. So, I want to store a particular value with key SKU. So, what I did was:
var skuArr = [""]
{ (response, error) in
if (error != nil)
{
print("Error \(error.debugDescription)")
}
else
{
self.coinsArr = response.arrayObject as? Array<Dictionary<String, Any>>
for i in 0 ..< (self.coinsArr?.count)!
{
self.skuArr = [response[i]["sku"].rawValue as! String]
}
}
So, with this I am getting the array in skuArr, but when i is 0 I am getting ["a"], and when i is 1 I want it to be ["a","b"], but it gives ["b"] only and when the loop ends with only the last value and not with ["a","b","c","d"] which I want as the final result. How can I insert each of them in the Array?
First of all declare skuArr as empty string array.
var skuArr = [String]()
And this is Swift. There are better ways than ugly index based loops to extract data for example with map or compactMap
if let result = response.arrayObject as? Array<Dictionary<String, Any>> {
self.coinsArr = result
self.skuArr = result.compactMap{ $0["sku"] as? String }
}
And why is coinsArr declared as optional as you are going to force unwrap it anyway? It's highly recommended to use non-optional types as much as possible. Non-optionals can never cause a well-liked unexpected found nil crash
Don't use this:
self.skuArr = [response[i]["sku"].rawValue as! String]
As this will replace the previous value with new one.
Use .append to insert into array.
self.skuArr.append([response[i]["sku"].rawValue as! String])
EDIT
change your initialisation as below:
skuArr: [String] = []

Swift3 how do I get the value of a specific key in a string?

I've got a server response returning
(
{
agreementId = "token.virtual.4321";
city = AMSTERDAM;
displayCommonName = "bunch-of-alphanumeric";
displaySoftwareVersion = "qb2/ene/2.7.14";
houseNumber = 22;
postalCode = zip;
street = "";
}
)
how do I get the value of agreementId? response['agreementId'] is not working. i've tried some example code with .first but I cannot get it working.
Some extra information, I do a http call to a server with alamofire. I try to parse the json to a constant response:
let response = JSON as! NSDictionary
However that returns a error message
Could not cast value of type '__NSSingleObjectArrayI' (0x1083600) to 'NSDictionary' (0x108386c).
So now parse the json to an array, which seems to be working. The code above is what
let response = JSON as! NSArry
print(response)
spits out.
Now I only need to retrieve the value from the key "agreementId" and I have no clue how to do that.
In swift you need to use Swift's native type Array/[] and Dictionary/[:] instead of NSArray and NSDictionary, if you specify the type like above means more specific then the compiler won't complain. Also use optional wrapping with if let or guard let to prevent crash.
if let array = JSON as? [[String:Any]] {//Swift type array of dictionary
if let dic = array.first {
let agreementId = dic["agreementId"] as? String ?? "N/A"//Set default value instead N/A
print(agreementId)
//access the other key-value same way
}
}
Note: If you having more than one object in your array then you need to simply loop through the array to access each dictionary of array.
if let array = JSON as? [[String:Any]] {//Swift type array of dictionary
for dic in array {
let agreementId = dic["agreementId"] as? String ?? "N/A"//Set default value instead N/A
print(agreementId)
//access the other key-value same way
}
}

How to save response.result.value from Alamofire in local array swift 3

Alamofire.request(NEWS_FEED_URL).responseJSON { response in
guard let newsResponse = response.result.value else{
print("Error: \(response.result.error)")
return
}
print("JSON: \(newsResponse)")
let localArray:Array = [newsResponse]
print("Local Array:", localArray)
}
I am using alamofire to fetch data from server and trying to write parser on the response data. When I fetch response.result.value from alamofire and print it is working fine. But as soon as I put response.result.value in my local array it gives
[<__NSArrayI 0x6080000bcf80>(
{
category = World;
})]
and so on as result (Console). Also I am unable to convert response value of type 'Any' to 'Array'. Please help me how to add response.result.value in local array of type 'Array'. Can someone explain something about type casting on alamofire in brief?
You need to simply cast the result of response.result.value to Array of Dictionary.
guard let newsResponse = response.result.value as? [[String:String]] else{
print("Error: \(response.result.error)")
return
}
print("JSON: \(newsResponse)")
let localArray = newsResponse
//Access the localArray
print("Local Array:", localArray)
//Now to get the desired value from dictionary try like this way.
let dic = localArray[0]
let title = dic["title"] ?? "Default Title"
let link = dic["link"] as? "Default link"
//... get other value same way.
Note : Your response is already an Array so don't make array from it by adding it inside another array.
There is problem in responseJOSN result because result may not correct JSON format to correct use [[String:Any]] for formating JSON
Alamofire.request(NEWS_FEED_URL).responseJSON { response in
guard let newsResponse = response.result.value as? [[String:Any]] else{
print("Error: \(response.result.error)")
return
}
print("JSON: \(newsResponse)")
let firstRow = newResponse[0]
let categoryValue = firstRow["category"] ?? "Default Category"
}
If you are using Decodable technique you can save the DATA from response in user defaults. User defaults allows us to save the DATA. When you need it you can just use JSON decoder to decode the JSON Data.

Ambiguous use of subscript error only on real device

When I try to run the code below on my iPhone, I get the following error
rrr.swift:356:72: Ambiguous use of 'subscript'
The strange thing is it happens only when I want to run the app on the phone, on the simulator however it works fine.
There are 3 lines which cause the error where. I noted them below in the code in capital letters.
I try to access properties in an array inside the JSON array, I suspect Im doing it the wrong way but have no idea how to fix it.
How can these properties be retrieved without the subscript error?
self.restApi.getSchoolDetails(schoolId) {responseObject, error in
// use responseObject and error here
self.schoolDetailsCollection = NSDictionary(dictionary: responseObject! as! [String : AnyObject])
print(self.schoolDetailsCollection)
if let response = responseObject as? NSDictionary {
//parse the response object
self.schoolDetailsList = (response as? NSDictionary)!
//assigning all values to shareData class
self.shareData.sco_id = response["result"]!["sco_id"] as!NSInteger
self.shareData.address = response["result"]!["address"] as!String
self.shareData.name = response["result"]!["name"] as! String
print("school name")
print(self.shareData.name)
self.shareData.intro = response["result"]!["intro"] as! String
self.shareData.sell_point = response["result"]!["sell_point"] as! String
self.shareData.city = response["result"]!["city"] as! String
self.shareData.cou_id = response["result"]!["cou_id"] as! String
//get images from the nested array in the json array
/THESE THREE LINES CAUSE THE ERROR SUBSRCIPT
self.shareData.image1 = response["result"]!["images"][0]! as! String
self.shareData.image2 = response["result"]!["images"]![1] as! String
self.shareData.image3 = response["result"]!["images"]![2] as! String
print(self.shareData.image1)
print(self.shareData.image2)
print(self.shareData.image3)
//open next controller after all info has been set correctly
//info is being passed by Singleton class Sharedata
if let COBezierDemoViewController = self.storyboard!.instantiateViewControllerWithIdentifier("COBezierDemoViewController") as? COBezierDemoViewController {
self.presentViewController(COBezierDemoViewController, animated: true, completion: nil)
}
}
}
}
The JSON file:
{
result = {
address = "223 Vincent St, West Perth WA 6005, Australia";
city = Perth;
"cou_id" = AU;
"cur_id" = "";
environment = R;
financed = "<null>";
images = (
"Phoenix_Academy_1.jpeg",
"Phoenix_Academy_3.jpeg",
"Phoenix_Academy_2.jpeg"
);
intro = "Our language school in Perth has a modern campus and a spacious garden. The language school is located 10 minutes north from the city center. You can reach the city center and the famous bea
As you hint that error in this line:
self.shareData.image1 = response["result"]!["images"][0]! as! String
Error:
Error is [0] subscript which you are implementing in AnyObject.
response["result"]!["images"] // this is returning Optional<AnyObject> which do not have subscript option.
Correct way should be this:
Downcast AnyObject as? [String] // this will either return nil or an Optional Array<String?> where String is image name.
Without downcasting, you will get error: Ambiguous use of subscript.
let images = response["result"]? ["images"] as? [String]
images?.flatMap({ $0 }).forEach {
print($0) // Phoenix_Academy_1.jpeg, Phoenix_Academy_3.jpeg, Phoenix_Academy_2.jpeg
}

Resources