Encoding toJSON from Arrays and Dictionaries in Swift - arrays

I am trying to create a JSON file from an array that i make inside my application with swift.
I need to encode to JSON an 'Array' because i am creating dictionaries and arrays before and i just combine them to that array.
In my code i have :
var order = Array<Any>()
var orderArray = Array<Dictionary<String, String>>()
var dict = Dictionary<String, Any>()
and i put first the orderArray in dict and then the dict inside order.
The output if i print it is correct and it works. The problem is when i try to encode the order(array) into JSON. Then i get the following error:
Cannot invoke 'dataWithJSONObject' with an argument list of type '(Array'
This is the code i use:
let json = NSJSONSerialization.dataWithJSONObject(order, options: NSJSONWritingOptions.PrettyPrinted, error: nil)
If i try the same code with the orderArray for example it works.
The 'Any' i think does the mess. But how could i resolve this?
Thank you.

Related

Kotlin Array with named parameter equivalent of swift

Hi i'am new to Kotlin and i would like to do an array with named parameter. In Swift i can do :
let analytics = ["organization_login": "toto", "organization_name": "titi", "lead_email": "tata"]
The type is : [String: String]
I Looked all Array and Arraylist in kotlin but i couln't find the equivalent.
What i want it's to be able to give parameter name for my array.
Edit
I misunderstood the swift syntaxe, it's seem that it's only a dictonary, so we just have to use map.
The reason is that [String: String] is not an array, it's a Dictionary.
The equivalent of Dictionary in Kotlin is Map.
Maps can be created like so:
val map = mapOf("string_one" to "string_2", "string_3" to "string_4")
or, if you want to mutate it:
val mutableMap = mutableMapOf("string_one" to "string_2")
You need to use Map as
val map = mapOf("organization_login" to "toto", "organization_name" to "titi")
// immutable map
you can also use sortedMapOf, hashMapOf linkedMapOf etc for different algo based storage.
Note: If you want to add more elements later then make sure to use mutableMapOf

Working with Unsplash. Cannot assign value of type 'Array<_>' to type 'Any?'

Currently, I'm working on processing information I've received from a JSON file, and as it's organized into multiple levels, I need to be able to convert not only the file, but all of its contents into the correct file types. I'm having some issues with this, and I can't find anything for Swift on this issue.
So far, I've tried the methods that are apparent within the code below. I couldn't find much information on alternate methods to do this, and I'm still new to programming, so I haven't been able to do much.
URLSession.shared.dataTask(with: urlRequest) { data, response, error in
if let data = data {
print("Clear")
let returnData = String(data: data, encoding: .utf8)
do {
let image = try JSONSerialization.jsonObject(with: data, options: []) as? [String: Any]
print(image!["results"])
var imgl2 = image!["results"]
imgl2 = imgl2 as! Array
//imgl2 = imgl2![0]
//print(imgl2!["color"])
// var imgl3 = imgl2!["results"]
// var imgl4 = imgl3!["results"]
// var imgl5 = imgl4!["results"]
// var imgl6 = imgl5!["results"]
} catch let error {
print(error)
}
}
}.resume()
What should happen here is that I should be able to convert the JSON's contents into the correct file types. To some extent, this works, and I'm able to obtain the first level of results. However, partway through the process at imgl2 = imgl2 as! Array, I can't convert it to an Array. Instead, I receive the error (Cannot assign value of type 'Array<_>' to type 'Any?'). I'd really like to be able to use the array enclosed, but I can't. After this, there will be several additional levels of content that I will need to sort through in this manner. I've tried looking at the API documentation, but I don't understand the way they've written it (this is Unsplash, by the way), and as such, I've tried this method. How exactly would I go about this?
(For further information, I'm trying to pull out the first image in the results, so that I can then edit it programmatically. I need the URL enclosed in the response to the search query, but the way the documentation is worded is unclear enough, and solutions to this problem so sparse, that I've had to use trial and error to get to where I am. Any insight into how to accomplish this would be greatly appreciated.)
Cannot assign value of type 'Array<_>' to type 'Any?
The error tells you that imgl2 is of type Any?, but you're trying to assign it a value of type Array. Swift doesn't allow changing the type of a variable after it has been initialized. What you could do is to assign imgl2 as! Array to a new variable to avoid this error.
I also suggest you to have a look at Decodable protocol to create models corresponding to the JSON structure.

Parsing array literal from JSON in Swift 3

I have a PHP script that returns a JSON response in the format of an array literal.
Example of exactly what it returns:
["school","chess_club"]
However, I am unable to parse it in Swift after I received the response successfully.
Code:
let json = try JSONSerialization.jsonObject(with: data!, options:.allowFragments) as! [String:AnyObject]
print (json)
Error:
Could not cast value of type '__NSArrayI' (0x10a440d88) to 'NSDictionary' (0x10a441288).
Note on duplicate:
I found this duplicate, Could not cast value of type '__NSArrayI' (0x10df73c08) to 'NSDictionary' (0x10df74108), but I can't figure out how can I use it to fix the error in my situation.
Specifically, I don't get how am I casting it to a NSDictionary since I didn't specify anywhere in my code "NSDictionary".
[String:AnyObject] is a Dictionary Object although you didn't specify it. Try this:
let json = try JSONSerialization.jsonObject(with: data!, options:.allowFragments) as! [String]
print (json)

Parse.com Query with swift 1.2 and string array

I am trying to query from parse.com and I would db receiving about 100 objects per time. I used the swift example code on their website, and the app doesn't build with that code. So I looked around and found that people were using code similar to this:
var query = PFQuery(className:"posts")
query.whereKey("post", equalTo: "true")
query.findObjectsInBackgroundWithBlock({ (objects: [AnyObject]?, error: NSError?) -> Void in
// do something
self.myDataArray = objects as! [String]
})
This does not work, because I am trying to convert PFObject to String
I would need to get the one one value from each object into a swift string array [String]. How do I get just the one text value, instead of the PFObject and how do I get it into the swift string array?
I don't speak swift very well, but the problem with the code is it's trying to cast the returned PFObject to a string, but you want to extract a string attribute, so (if you really want to do it):
for object in objects {
var someString = object.valueForKey("someAttributeName") as String
self.myDataArray.addObject(someString)
}
But please make sure you need to do this. I've noticed a lot of new parse/swift users (especially those who are populating tables) have the urge to discard the returned PFObjects in favor of just one of their attributes. Consider keeping the PFObjects and extracting the attributes later as you need them. You might find you'll need other attributes, too.
For starters, I would definitely recommend using the "if let" pattern to qualify your incoming data. This is a nice Swift feature that will help avoid run-time errors.
var query = PFQuery(className:"posts")
query.whereKey("post", equalTo: "true")
query.findObjectsInBackgroundWithBlock(
{ (objects: [AnyObject]?, error: NSError?) -> Void in
// check your incoming data and try to cast to array of "posts" objects.
if let foundPosts = objects as? [posts]
{
// iterate over posts and try to extract the attribute you're after
for post in foundPosts
{
// this won't crash if the value is nil
if let foundString = post.objectForKey("keyForStringYouWant") as? String
{
// found a good data value and was able to cast to string, add it to your array!
self.myDataArray.addObject(foundString)
}
}
})

Empty Array in Swift?

I am trying to create an empty array in Swift that the user adds on to. It is a very small class because I am just starting a new file. I am using this tutorial for my project which is what the code is based off. Here is the code I tried (It didn't work):
var tasks = task[]()
Here is all of my code in case it is needed:
class TaskManager: NSObject {
var tasks = task[]()
func addTask(name: String){
tasks.append(task(name: name))
}
}
There is an error on the var tasks = task[]() line saying: "Array types are now written the brackets around the element type". I am unsure of how to fix the problem.
How can one create an empty array?
Any input or suggestions would be greatly appreciated.
Thanks in advance.
You have to declare the array in this way:
var tasks = [task]()
It changed in swift from the tutorial you are watching.
The syntactic sugar for an array of Type is [Type] in Swift. You can create an empty array of Tasks like this:
class Task{}
var tasks = [Task]()

Resources