Im updating my app to swift 3
I am getting a couple of errors
for (k, v): (AnyObject, AnyObject) in value {
Gets an NSDictionary.Iterator.Element is not convertable to (Anyobject, Anyobject)
Subsiquently Im getting this error
var artworks = [Artwork]()
func loadInitialData() {
// 1
let fileName = Bundle.main.path(forResource: "PublicArt", ofType: "json");
let data: Data = try! Data(contentsOf: URL(fileURLWithPath: fileName!), options: NSData.ReadingOptions(rawValue: 0))
// 2
var error: NSError?
let jsonObject: AnyObject!
do {
jsonObject = try JSONSerialization.jsonObject(with: data,
options: JSONSerialization.ReadingOptions(rawValue: 0))
} catch let error1 as NSError {
error = error1
jsonObject = nil
}
// 3
if let jsonObject = jsonObject as? [String: AnyObject], error == nil,
// 4
let jsonData = JSONValue.fromObject(jsonObject)?["data"]?.array {
for artworkJSON in jsonData {
if let artworkJSON = artworkJSON.array,
// 5
let artwork = Artwork.fromJSON(artworkJSON) {
artworks.append(artwork)
}
}
}
}
JsonObject produces 'Any' not the expected contextual result type
'AnyObject'
and
Argument type [String : AnyObject] does not conform to expected type
'AnyObject'
Im assuming this is an easy one but I havent coded in a year and would be very appreciative of the help
Thanks
Travis
UPDATE
So I just updated the code
but getting an error in the JSON.swift file
static func fromObject(_ object: AnyObject) -> JSONValue? {
switch object {
case let value as NSString:
return JSONValue.jsonString(value as String)
case let value as NSNumber:
return JSONValue.jsonNumber(value)
case _ as NSNull:
return JSONValue.jsonNull
case let value as NSDictionary:
var jsonObject: [String:JSONValue] = [:]
for (k, v): (AnyObject, AnyObject) in value {
if let k = k as? NSString {
if let v = JSONValue.fromObject(v) {
jsonObject[k as String] = v
} else {
return nil
}
}
}
return JSONValue.jsonObject(jsonObject)
case let value as NSArray:
var jsonArray: [JSONValue] = []
for v in value {
if let v = JSONValue.fromObject(v as AnyObject) {
jsonArray.append(v)
} else {
return nil
}
}
return JSONValue.jsonArray(jsonArray)
default:
return nil
}
}
}
error is:
nsdictionary.iterate.element '(aka (key: Any, value: Any)') is not
convertible to 'AnyObject, AnyObject)'
for code line
for (k, v): (AnyObject, AnyObject) in value {
Sorry for the late reply
Regards
Travis
You are using too much AnyObject aka it's-an-object-but-I-don't-know-the-type.
Since the JSON file is in your bundle you know exactly the types of all objects.
In Swift 3 a JSON dictionary is [String:Any] and a JSON array is [[String:Any]].
However I don't know the exact structure of the JSON and I don't know what JSONValue does – a third party library is actually not necessary – but this might be a starting point to get the array for key data.
func loadInitialData() {
let fileURL = Bundle.main.url(forResource: "PublicArt", withExtension: "json")!
do {
let data = try Data(contentsOf: fileURL, options: [])
let jsonObject = try JSONSerialization.jsonObject(with: data) as! [String: Any]
let jsonData = jsonObject["data"] as! [[String:Any]]
for artworkJSON in jsonData {
print(artworkJSON)
// ... create Artwork items
}
} catch {
print(error)
fatalError("This should never happen")
}
}
Related
I do not know how to access the 'duration' value within my nested Optional NSSingleObjectArrayI that is constructed from a JSON response. How do I access the nested values within this data structure?
When I call print(firstRow["elements"]), I get the following output:
Optional(<__NSSingleObjectArrayI 0x60000120f920>(
{
distance = {
text = "1.8 km";
value = 1754;
};
duration = {
text = "5 mins";
value = 271;
};
"duration_in_traffic" = {
text = "4 mins";
value = 254;
};
status = OK;
}
))
I have tried string indexing (firstRow['elements']['duration']) but am getting errors.
fetchData { (dict, error) in
if let rows = dict?["rows"] as? [[String:Any]]{
if let firstRow = rows[0] as? [String:Any]{
print("firstRow is")
print(firstRow["elements"])
// Trying to access duration within firstRow['elements'] here
}
}
}
For reference, this is the fetchData function:
func fetchData(completion: #escaping ([String:Any]?, Error?) -> Void) {
let url = getRequestURL(origin: "test", destination: "test")!;
let task = URLSession.shared.dataTask(with: url) { (data, response, error) in
guard let data = data else { return }
do {
if let array = try JSONSerialization.jsonObject(with: data, options: .allowFragments) as? [String:Any]{
completion(array, nil)
}
} catch {
print(error)
completion(nil, error)
}
}
task.resume()
}
A sample HTTP JSON request is here:
https://maps.googleapis.com/maps/api/distancematrix/json?destinations=77%20Massachusetts%20Ave,%20Cambridge,%20MA&departure_time=now&key=AIzaSyB65D4XHv6PkqvWJ7C-cFvT1QHi9OkqGCE&origins=428%20Memorial%20Dr,%20Cambridge,%20MA
Seeing your output, your firstRow["elements"] is Optional, so you need to unwrap it. And it actually is an NSArray with a single element, where the only element is a Dictionary, with 4 entries -- "distance", "duration", "duration_in_traffic" and "status". You may need to cast the element to a Dictionary to access each entry.
You may use Optional binding with as?-casting for this purpose:
fetchData { (dict, error) in
if let rows = dict?["rows"] as? [[String: Any]] {
if let firstRow = rows.first {
print("firstRow is")
print(firstRow["elements"])
//Unwrap and cast `firstRow["elements"]`.
if let elements = firstRow["elements"] as? [[String: Any]] {
//The value for "duration" is a Dictionary, you need to cast it again.
if let duration = elements.first?["duration"] as? [String: Any] {
print(duration["text"] as? String)
print(duration["value"] as? Int)
}
}
}
}
}
Or too deeply nested ifs are hard to read, so someone would like it as:
fetchData { (dict, error) in
if
let rows = dict?["rows"] as? [[String: Any]],
let firstRow = rows.first,
let elements = firstRow["elements"] as? [[String: Any]],
let duration = elements.first?["duration"] as? [String: Any]
{
print(duration["text"] as? String)
print(duration["value"] as? Int)
}
}
Or using guard may be a better solution.
Or else, if you can show us the whole JSON text in a readable format, someone would show you how to use Codable, which is a modern way to work with JSON in Swift.
I am trying to check the version of my app with the iTunes lookup api. I have problems in parsing the response. Please find the code
static func needsUpdate() -> Bool
{
do {
let infoDictionary = Bundle.main.infoDictionary
let appID = infoDictionary?["CFBundleIdentifier"]
let url:URL = URL(string: "http://itunes.apple.com/lookup?bundleId=\(appID!)")!
let data = try Data(contentsOf: url)
let lookup = try JSONSerialization.jsonObject(with:data, options: []) as! [String:AnyObject]
print(lookup)
let resultCount:Int = lookup["resultCount"] as! Int
if (resultCount == 1)
{
var results = lookup["results"] as! [String:AnyObject] // ***Error***
if results.isEmpty
{
print(results)
}
}
} catch
{
}
return true
}
Please let me know how can i parse this response
The error message clearly reveals that the value for results is an array.
let results = lookup["results"] as! [[String:Any]]
And consider that a JSON dictionary is [String:Any] in Swift 3
I have made the following function in Swift 3:
func parseJSON() {
var JsonResult: NSMutableArray = NSMutableArray()
do {
JsonResult = try JSONSerialization.jsonObject(with: self.data as Data, options:JSONSerialization.ReadingOptions.allowFragments) as! NSMutableArray
} catch let error as NSError {
print(error)
}
var jsonElement:NSDictionary=NSDictionary()
let locations: NSMutableArray = NSMutableArray()
for i in 0 ..< JsonResult.count
{
jsonElement = JsonResult[i] as! NSDictionary
let location = Parsexml()
if let title = jsonElement["Title"] as? String,
let body = jsonElement["Body"] as? String,
let userId = jsonElement["UserId"] as? Int,
let Id = jsonElement["Id"] as? Int
{
location.title = title
location.body = body
location.userId = userId
location.id = Id
}
locations.add(location)
}
DispatchQueue.main.async { () -> Void in
self.delegate.itemsDownloaded(items: locations)
}
When i call this function from another method, i get the following error:
Could not cast value of type '__NSArrayI' (0x105d4fc08) to 'NSMutableArray' (0x105d4fcd0).
It points me towards the element here:
JsonResult = try JSONSerialization.jsonObject(with: self.data as Data, options:JSONSerialization.ReadingOptions.allowFragments) as! NSMutableArray
Where it exits with a SIGBRT..
What have i missed here?
You are trying to convert an NSArray into an NSMutable array which is what the warning is complaining about.
Take the array it provides you, and then convert it into a mutable one.
let jsonArray = try JSONSerialization.jsonObject(with: self.data as Data, options:JSONSerialization.ReadingOptions.allowFragments) as! NSArray
jsonResult = jsonArray.mutableCopy() as! NSMutableArray
Unrelated, but you may also want to user a lower case value for the JsonResult to fit with normal iOS style guidelines. It should instead be jsonResult.
Another way to improve your code:
You are not mutating your JsonResult, so you have no need to declare it as NSMutableArray:
var JsonResult = NSArray()
do {
JsonResult = try JSONSerialization.jsonObject(with: self.data as Data, options:JSONSerialization.ReadingOptions.allowFragments) as! NSArray
} catch let error as NSError {
print(error)
}
And some steps to improve your code...
enum MyError: Error {
case NotArrayOfDict
}
func parseJSON() {
do {
guard let jsonResult = try JSONSerialization.jsonObject(with: self.data as Data) as? [[String: Any]] else {
throw MyError.NotArrayOfDict
}
let locations: NSMutableArray = NSMutableArray()
for jsonElement in jsonResult {
let location = Parsexml()
if let title = jsonElement["Title"] as? String,
let body = jsonElement["Body"] as? String,
let userId = jsonElement["UserId"] as? Int,
let Id = jsonElement["Id"] as? Int
{
location.title = title
location.body = body
location.userId = userId
location.id = Id
}
locations.add(location)
}
DispatchQueue.main.async { () -> Void in
self.delegate.itemsDownloaded(items: locations)
}
} catch let error {
print(error)
}
}
as! casting sometimes crashes your app, use it only when you are 100%-sure that the result is safely converted to the type. If you are not, using guard-let with as? is safer.
Use Swift types rather than NSSomething as far as you can.
Specifying .allowFragments is not needed, as you expect the result as an Array.
And if you can modify some other parts of your code, you can write your code as:
func parseJSON() {
do {
//If `self.data` was declared as `Data`, you would have no need to use `as Data`.
guard let jsonResult = try JSONSerialization.jsonObject(with: self.data) as? [[String: Any]] else {
throw MyError.NotArrayOfDict
}
var locations: [Parsexml] = [] //<-Use Swift Array
for jsonElement in jsonResult {
let location = Parsexml()
if let title = jsonElement["Title"] as? String,
let body = jsonElement["Body"] as? String,
let userId = jsonElement["UserId"] as? Int,
let Id = jsonElement["Id"] as? Int
{
location.title = title
location.body = body
location.userId = userId
location.id = Id
}
locations.append(location)
}
DispatchQueue.main.async { () -> Void in
self.delegate.itemsDownloaded(items: locations)
}
} catch let error {
print(error)
}
}
The errors are:
Initializer for conditional binding must have Optional type, not 'NSData'
and
Call can throw, but it is not marked with 'try' and the error is not handled
class func loadMembersFromFile(path:String) -> [Member] //Function
{
var members:[Member] = []
var error:NSError? = nil
if let data = NSData(contentsOfFile: path, options:[]), //data
json = NSJSONSerialization.JSONObjectWithData(data, options: []) as? NSDictionary, //my array (json)
team = json["team"] as? [NSDictionary] { // display json
for memberDictionary in team { //cylce for
let member = Member(dictionary: memberDictionary)
members.append(member)
}
}
return members
}
First, you need to use the do catch syntax for methods that can throw exceptions. Secondly, the NSData initializer doesn't produce an Optional value so you can't put it in an if statement.
class func loadMembersFromFile(path:String) -> [Member] //Function
{
var members:[Member] = []
var error:NSError? = nil
do {
let data = try NSData(contentsOfFile: path, options:[])
if let json = try NSJSONSerialization.JSONObjectWithData(data, options: []) as? NSDictionary,
let team = json["team"] as? [NSDictionary] {
for memberDictionary in team { //cylce for
let member = Member(dictionary: memberDictionary)
members.append(member)
}
}
} catch {
//handle exceptions
}
return members
}
See documentation.
The following code throws a message which says "Initializer for conditional binding must have Optional type, not 'AnyObject'"
func parseData2(){
var data:NSData?
if let data2 = data {
do {
let details = try NSJSONSerialization.JSONObjectWithData(data2, options: .AllowFragments)
if let actualDetails = details where actualDetails.isKindOfClass(NSDictionary) {
print("Parse Data")
}
}catch {
print("Error \(error)")
}
}
}
To resolve the above error I used the following code.
func parseData2(){
var data:NSData?
if let data2 = data {
do {
let details:AnyObject = try NSJSONSerialization.JSONObjectWithData(data2, options: .AllowFragments)
if let actualDetails:AnyObject = details where actualDetails.isKindOfClass(NSDictionary) {
print("Parse Data")
}
}catch {
print("Error \(error)")
}
}
}
Is there any better approach then the above or my code might crash?
There is one more code which I want to add considering nil check,type check and then type cast check. The reason behind that Swift offers great flexibility but litle bit difficult to fix issues. Let's say I have a dictionary, cityDetails and I am trying to get data for self.cityZipCode and self.cityIdentifier, which are optional, defined as var cityZipCode:Int? and var cityIdentifier:Int?
if let cityBasic = cityDetails["basicDetails"] where
cityBasic!.isKindOfClass(NSDictionary) {
self.cityZipCode = (cityBasic as! NSDictionary)["zip"].integerValue ?? 0
self.cityIdentifier = (cityBasic as! NSDictionary)["cityId"].integerValue ?? 0
}
No need to unwrap the result from try. It is not an optional. You do need to cast the result from try to an NSDictionary. Use as? to downcast it.
Best practice: full access to returned error for good error handling
func parseData2(){
var data:NSData?
if let data2 = data {
do {
let details = try NSJSONSerialization.JSONObjectWithData(data2, options: .AllowFragments)
if let detailsDict = details as? NSDictionary {
print("Parse Data")
} else if let detailsArray = details as? NSArray {
print("array")
}
} catch {
print("Error \(error)")
}
}
}
Quick and dirty: error handling is not for me!
func parseData2(){
var data:NSData?
if let data2 = data {
let details = try? NSJSONSerialization.JSONObjectWithData(data2, options: .AllowFragments)
if let detailsDict = details as? NSDictionary {
print("Parse Data")
} else {
print("details might be nil, or not an NSDictionary")
}
}
}
Bad Ass Mode: crashes are features
func parseData2(){
var data:NSData?
if let data2 = data {
let details = try! NSJSONSerialization.JSONObjectWithData(data2, options: .AllowFragments) as! NSDictionary
}
}
Some extra info on multiple unwraps :
Drop the code below in a playground.
struct SomeStruct {
var anOptional : Int?
init() {
}
}
func unwrapWithIfLet() {
if let unWrappedStruct = myStruct, let unWrappedSomething = unWrappedStruct.anOptional {
print("multiple optional bindings succeeded")
// both unWrappedStruct and unWrappedSomething are available here
} else {
print("something is nil")
}
}
func unwrapWithGuard() {
guard let unWrappedStruct = myStruct, let unWrappedSomething = unWrappedStruct.anOptional else {
print("something is nil")
return
}
print("multiple optional bindings succeeded")
// both unWrappedStruct and unWrappedSomething are available here
}
var myStruct : SomeStruct?
//unwrapWithGuard()
//unwrapWithIfLet()
myStruct = SomeStruct()
myStruct!.anOptional = 1
unwrapWithGuard()
unwrapWithIfLet()
You are looking for as?, which attempts to convert the thing on the left to the type on the right, and returns nil if the conversion is not possible:
let details = try NSJSONSerialization.JSONObjectWithData(data2, options: .AllowFragments)
if let actualDetails = details as? NSDictionary {
print("Parse Data")
}
You rarely need to use isKindOfClass in Swift. If you find yourself using it, ask why, and consider whether as or as? will work instead.