Swift Can't access Single object array - arrays

I am trying to access data from a json file. The problem is that some of the values are NSSingleObjectArrays (Arrays with only item) which I can't turn into strings.
class CarObject {
var pictureURL: String!
var carURL: String!
var carPrice: String!
required init(json: [String: AnyObject]) {
pictureURL = json["galleryURL"] as! String
carURL = json["viewItemURL"] as! String
carPrice = json["currentPrice"] as! String
}
}
I get the following error message:
Could not cast value of type '__NSSingleObjectArrayI' (0x10a2ec548) to 'NSString' (0x109729440).
I tried to access them like this:
"json["galleryURL"][0] as String!"
but I get the following error:
Type 'Any?' has no subscript members
The values look like this:
galleryURL = ("one value");
Do you guys know a way how to access them easily?
Thanks!

Just cast things to the appropriate types first. It sounds like your values are arrays containing a single string, so something like this:
var pictureURL: URL
required init?(json: [String: AnyObject]) {
guard let pictureURLString = (json["galleryURL"] as? [String])?.first,
let pictureURL = URLComponents(string: pictureURLString)?.url else {
return nil
}
self.pictureURL = pictureURL
}
Also, you may want to reconsider the types you're using for your properties. The pictureURL and carURL properties are URLs, so they make more sense typed as URL. The carPrice property is likely numeric, so it makes more sense as a Double.

Related

How to store array of dictionaries into class variable?

I have an array of dictionaries that is being read in from a JSON file as seen below. I would like to store that value (jsonResult) into a class variable so that I can use it to populate a tableview. However, I don't quite understand how to store that value.
Here is how I am getting my array of dictionaries (jsonResult):
if let path = Bundle.main.path(forResource: filename, ofType: "json") {
do {
let data = try Data(contentsOf: URL(fileURLWithPath: path), options: .mappedIfSafe)
let jsonResult = try JSONSerialization.jsonObject(with: data, options: .mutableLeaves) as! [String:Any]
self.tableData = jsonResult // WHAT GOES HERE?
} catch {
// handle error
}
}
And this is my class variable that I want to store my array of dictionaries into:
var tableData = [Dictionary<String, String>]()
How can I correctly store jsonResult into tableData? I do not want to use a struct as the structure of the dictionaries can vary.
You state the JSON is an array of dictionary but you are casting the result of JSONSerialization.jsonObject to just a dictionary. Since you seem to be expected an array of dictionary with both string keys and values, cast the result accordingly. But do it safely. Never use ! when working with JSON.
if let jsonResult = try JSONSerialization.jsonObject(with: data, options: .mutableLeaves) as? [[String:String]] {
self.tableData = jsonResult
} else {
// Error - unexpected JSON result
}
This assumes you want the top level of the JSON result. If in fact jsonResult should be a dictionary and that top-level dictionary has a key to the actual array of dictionary you want then you need to fix the code accordingly.

convert value of type 'Result?' to type 'String'

I have problems to cast my array of type 'result' to an array of string. This is what I already tried:
EDIT:
I need the information as String type since I want to use the URL as image source.
swift4
let message = try? JSONDecoder().decode(Welcome.self, from: data)
let imageURLs = message?.children.attachment.results.filter({ $0.metadata.mediaType == "image/png" })
let latestImageURls = imageURLs?.prefix(2)
let latestImageURlsArray = Array(latestImageURls ?? [])
let image1 = self.view.viewWithTag(63) as! UIImageView
let image2 = self.view.viewWithTag(64) as! UIImageView
let image3 = self.view.viewWithTag(65) as! UIImageView
let url1 = URL(string: latestImageURlsArray[0]) // error:Cannot convert value of type 'Result' to expected argument type 'String
let url2 = URL(string: latestImageURlsArray[1]) // error:Cannot convert value of type 'Result' to expected argument type 'String
let url3 = URL(string: latestImageURlsArray[2]) // error:Cannot convert value of type 'Result' to expected argument type 'String
image1.kf.setImage(with: url1)
image2.kf.setImage(with: url2)
image3.kf.setImage(with: url3)
I think there is no such thing as [Array], I guess you're talking about Array<Result>. An array of Result object. What is the same as [Result].
If for some reason you want to create a new Array object from you ArraySlice, just call an initializer.
let resultsArray = Array(latestImageURls ?? [])
UPDATE
You're saying that you also need to convert your Result objects into String, but you disn't explain what is the Result object and how does it how is it related to the String. Does it contain it or it is a String? So I'll try to assume.
If you want to cast you objects into String, you can do it like that
let latestImageURlsArray = resultsArray.compactMap { $0 as? String }
If you want to extract your strings from results if they have it, (let's say that it's stored at imageURL parameter) you can do it like that
let latestImageURlsArray = resultsArray.compactMap { $0.imageURL }
After that, latestImageURlsArray will be an [String].
If the relation is completely different and more complicated, please add more details about the Result class, so I could make my answer more specific.
You're not doing any "casting", you're just saying "I expected a [Array]". You're not doing anything to make that be the case. In any case, that's not even valid, because Array isn't a valid type.
As you see, prefix returns an ArraySlice<T>, which provides a lightweight view into the memory of Array<T>, without copying any elements.
In general, the way you convert an ArraySlice of a given type to an Array of that type is to use the Array initializer:
struct Thing {
}
let things = [Thing]()
let sliceOfArrayOfThings = things.prefix(2)
let arrayOfThings = Array(sliceOfArrayOfThings)
In your case your Array is actually an Optional array, so you have to take some extra steps to deal with the optionality, as described by Yury in his answer:
let things: [Thing]? = []
let sliceOfArrayOfThings = things?.prefix(2)
let arrayOfThings = Array(sliceOfArrayOfThings ?? [])

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
}
}

PFObject Array Sort

I'm using Parse and I have an array of PFObjects called "scorecardData". Each PFObject has a "score" property that is of type Int. I'm trying to sort my array by "score" but I'm getting the following error: "Binary operator '<' cannot be applied to two 'AnyObject?' operands". I'm not sure what I'm doing wrong here. I also tried down casting the objectForKey("score") as! Int but its not letting me do this. Any suggestions? Thanks in advance.
var scorecardData = [PFObject]()
scorecardData.sortInPlace({$0.objectForKey("score") < $1.objectForKey("score")})
You declared scorecardData variable as Array of PFObject. Why are you trying access PFObject property using objectForKey: reserved? Anyway I am not parse expert. But if you declared your array as [PFObject] you can use:
scorecardData.sortInPlace({$0.score < $1.score})
But this won't work unless you subclass PFObject for a more native object-oriented class structure. If you do that remember also to specify:
var scorecardData = [YOUR_NEW_CLASS]()
I strongly recommend subclassing PFObject to make use of all swift type-safe goodies.
But if you want to keep your data structure you can use:
scorecardData.sortInPlace({($0["score"] as! Int) < ($1["score"] as! Int)})
Keep in mind that it's dangerous, and in future avoid it.
If you want to Sort your array of PFOject... You can do this
extension Array where Element:PFObject {
func sort() -> [PFObject] {
return sort { (first, second) -> Bool in
let firstDate = first.objectForKey("time") as! NSDate//objectForKey(Constants.Parse.Fields.User.fullName) as? String
let secondDate = second.objectForKey("time") as! NSDate//objectForKey(Constants.Parse.Fields.User.fullName) as? String
return firstDate.compare(secondDate) == .OrderedAscending
}
}
}
Have you tried doing this?
var query = PFQuery(className:"ScoreCard")
// Sorts the results in ascending order by the score field
query.orderByDescending("score")
query.findObjectsInBackgroundWithBlock {

Resources