Populate UITableView from JSON Array using Swift - json

Dear stackoverflow community,
I have the following function written in swift:
func jsonParsing()
{
let url2 = NSURL(string: "http://localhost:8000/straightred/jsonfixture")
let data = NSData(contentsOfURL: url2!)
var arr = NSJSONSerialization.JSONObjectWithData(data!, options: nil, error: nil) as NSArray
for var i = 0 ; i < (arr as NSArray).count ; i++
{
arrDict.addObject((arr as NSArray) .objectAtIndex(i))
}
println(arrDict);
}
Which produces the following output:
(
{
fields = {
"away_team" = Burnley;
awayteamscore = 2;
fixturedate = "2014-11-22T15:00:00";
fixturematchday = 12;
fixturestatus = FINISHED;
"home_team" = Stoke;
hometeamscore = 1;
soccerseason = 354;
};
model = "straightred.straightredfixture";
pk = 136932;
},
{
fields = {
"away_team" = Swans;
awayteamscore = 1;
fixturedate = "2014-11-22T15:00:00";
fixturematchday = 12;
fixturestatus = FINISHED;
"home_team" = ManCity;
hometeamscore = 2;
soccerseason = 354;
};
model = "straightred.straightredfixture";
pk = 136930;
},
{
fields = {
"away_team" = Sunderland;
awayteamscore = 0;
fixturedate = "2014-11-22T15:00:00";
fixturematchday = 12;
fixturestatus = FINISHED;
"home_team" = Foxes;
hometeamscore = 0;
soccerseason = 354;
};
model = "straightred.straightredfixture";
pk = 137852;
},
{
fields = {
"away_team" = "West Ham";
awayteamscore = 1;
fixturedate = "2014-11-22T15:00:00";
fixturematchday = 12;
fixturestatus = FINISHED;
"home_team" = Everton;
hometeamscore = 2;
soccerseason = 354;
};
model = "straightred.straightredfixture";
pk = 136929;
},
{
fields = {
"away_team" = "West Bromwich";
awayteamscore = 0;
fixturedate = "2014-11-22T15:00:00";
fixturematchday = 12;
fixturestatus = FINISHED;
"home_team" = Chelsea;
hometeamscore = 2;
soccerseason = 354;
};
model = "straightred.straightredfixture";
pk = 136928;
},
{
fields = {
"away_team" = QPR;
awayteamscore = 0;
fixturedate = "2014-11-22T15:00:00";
fixturematchday = 12;
fixturestatus = FINISHED;
"home_team" = Newcastle;
hometeamscore = 1;
soccerseason = 354;
};
model = "straightred.straightredfixture";
pk = 136931;
},
{
fields = {
"away_team" = ManU;
awayteamscore = 2;
fixturedate = "2014-11-22T17:30:00";
fixturematchday = 12;
fixturestatus = FINISHED;
"home_team" = Arsenal;
hometeamscore = 1;
soccerseason = 354;
};
model = "straightred.straightredfixture";
pk = 136927;
},
{
fields = {
"away_team" = Liverpool;
awayteamscore = 1;
fixturedate = "2014-11-23T13:30:00";
fixturematchday = 12;
fixturestatus = FINISHED;
"home_team" = Crystal;
hometeamscore = 3;
soccerseason = 354;
};
model = "straightred.straightredfixture";
pk = 136926;
},
{
fields = {
"away_team" = Spurs;
awayteamscore = 2;
fixturedate = "2014-11-23T16:00:00";
fixturematchday = 12;
fixturestatus = FINISHED;
"home_team" = Hull;
hometeamscore = 1;
soccerseason = 354;
};
model = "straightred.straightredfixture";
pk = 136925;
},
{
fields = {
"away_team" = Southampton;
awayteamscore = 1;
fixturedate = "2014-11-24T20:00:00";
fixturematchday = 12;
fixturestatus = FINISHED;
"home_team" = "Aston Villa";
hometeamscore = 1;
soccerseason = 354;
};
model = "straightred.straightredfixture";
pk = 136924;
}
)
I then want to populate a UITableView with this information. I have the following two functions to achieve this:
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return arrDict.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell:TblCell = self.tableView.dequeueReusableCellWithIdentifier("cell") as TblCell
var homeTeamName : NSString=arrDict[indexPath.row] .valueForKey("home_team") as NSString
var awayTeamName : NSString=arrDict[indexPath.row] .valueForKey("away_team") as NSString
cell.lblHomeTeam.text = homeTeamName
cell.lblAwayTeam.text = awayTeamName
return cell
}
However, the output window shows:
fatal error: unexpectedly found nil while unwrapping an Optional value
(lldb)
It highlights the "var homeTeamName : NSString=arrDict[indexPath.row] .valueForKey("home_team") as NSString" in green and states "EXC_BAD_INSTRUCTION".
I am convinced that I am trying to access the array incorrectly but I am not sure the correct way forward. Any help on this would be appreciated.
Many thanks, ALAN.

Look at your data: "home_team" and "away_team" are values from a level 2 dictionary, in the "fields" key of a level 1 dictionary.
But you try to access "home_team" and "away_team" directly inside your "arrDict" array of level 1 dictionaries.
Array => Dictionaries => Fields key of each dic => your data (in a dictionary)
So you should first access "fields":
let fields = arrDict[indexPath.row].valueForKey("fields") as NSDictionary
Then access the team:
let homeTeamName = fields.valueForKey("home_team") as NSString

Related

.net core connect to mysql server

I have a issue here for the connect mysql.
I have a vps that contain mysql server have a database name is 'tuyendungbg'. it's had table and data in there. But when i'm connect and query to select data, that have error for 'Table 'tuyendungbg.Careers' doesn't exist'.
here is my controller :
[HttpGet("/nganh-nghe/{slug?}")]
public IActionResult Index(string slug, [FromQuery(Name =
"p")] int currentPage, int
pageSize)
{
if (!string.IsNullOrEmpty(slug))
{
var career = _context.Careers.FirstOrDefault(x =>
x.Slug.Equals(slug));
if (career != null)
{
ViewBag.CareerName = career.CareerName;
//var qr = _context.Careers.ToList();
var qr = (from p in
_context.CompanyCareers.Where(x => x.CareerId ==
career.ID)
from b in _context.Companies.Where(x =>
x.Id == p.CompanyId)
from i in
_context.IndustrialAreas.Where(x => x.ID ==
b.IndustrialAreaId).DefaultIfEmpty()
select new CompanyModelView()
{
Id = b.Id,
CompanyName = b.CompanyName,
BasicSalary = b.BasicSalary,
Allowance = b.Allowance,
DateCreate = b.DateCreate,
JobTitle = b.JobTitle,
Src = b.Src,
_IndustrialArea = i,
Address = b.Address,
Slug = b.Slug,
WorkTypes = (from tc in
_context.CompanyWorkTypes.Where(x =>
x.CompanyId == b.Id)
from type in
_context.WorkTypes.Where(x => x.Id
==
tc.TypeId)
select type)
}
).OrderByDescending(x => x.DateCreate).ToList();
// var worktype = (from w in
_context.CompanyWorkTypes.Where(x =>
x.CompanyId
== qr.))
var totalCate = qr.Count();
if (pageSize <= 0) pageSize = 10;
int countPages =
(int)Math.Ceiling((double)totalCate / pageSize);
if (currentPage > countPages)
currentPage = countPages;
if (currentPage < 1)
currentPage = 1;
var pagingModel = new PagingModel()
{
countpages = countPages,
currentpage = currentPage,
generateUrl = (pageNumber) =>
Url.Action(nameof(Index), new
{
p = pageNumber,
pageSize = pageSize
})
};
ViewBag.pagingModel = pagingModel;
ViewBag.totalPost = totalCate;
ViewBag.cateIndex = (currentPage - 1) * pageSize;
var companies = qr.Skip((currentPage - 1) *
pageSize)
.Take(pageSize).ToList();
ViewBag.JobCount = companies.Count();
//Set slider menu
var leftSlider = new LeftSliderModel()
{
Areas = new SelectList(_context.Areas,
nameof(Area.ID),
nameof(Area.NameArea)),
Careers = new SelectList(_context.Careers,
nameof(Career.ID),
nameof(Career.CareerName)),
WorkTypes = _context.WorkTypes.ToList(),
Experiences = _context.Experiences.ToList()
};
ViewBag.LeftSlider = leftSlider;
return View(companies);
}
}
return NotFound();
}

Unable to parse the json in swift

I have a json response as follows
{
"payment_methods" = (
{
code = checkmo;
title = "Check / Money order";
},
{
code = "paypal_express";
title = "PayPal Express Checkout";
},
{
code = banktransfer;
title = "Bank Transfer Payment";
}
);
totals = {
"base_currency_code" = USD;
"base_discount_amount" = "-258.38";
"base_grand_total" = "2725.37";
"base_shipping_amount" = 400;
"base_shipping_discount_amount" = 0;
"base_shipping_incl_tax" = 400;
"base_shipping_tax_amount" = 0;
"base_subtotal" = "2583.75";
"base_subtotal_with_discount" = "2325.37";
"base_tax_amount" = 0;
"coupon_code" = ADAMAS10;
"discount_amount" = "-258.38";
"grand_total" = "2725.37";
items = (
{
"base_discount_amount" = "134.2";
"base_price" = 1342;
"base_price_incl_tax" = 1342;
"base_row_total" = 1342;
"base_row_total_incl_tax" = 1342;
"base_tax_amount" = 0;
"discount_amount" = "134.2";
"discount_percent" = 10;
"item_id" = 3292;
name = BWBCA14KWGVC015;
options = "[{\"value\":\"18K White Gold\",\"label\":\"Metal\"},{\"value\":\"2\",\"label\":\"size\"}]";
price = 1342;
"price_incl_tax" = 1342;
qty = 1;
"row_total" = 1342;
"row_total_incl_tax" = 1342;
"row_total_with_discount" = 0;
"tax_amount" = 0;
"tax_percent" = 0;
"weee_tax_applied" = "[]";
"weee_tax_applied_amount" = 0;
},
{
"base_discount_amount" = "124.18";
"base_price" = "1241.75";
"base_price_incl_tax" = "1241.75";
"base_row_total" = "1241.75";
"base_row_total_incl_tax" = "1241.75";
"base_tax_amount" = 0;
"discount_amount" = "124.18";
"discount_percent" = 10;
"item_id" = 3342;
name = BWBCA14KWGCV008;
options = "[{\"value\":\"18K White Gold\",\"label\":\"Metal Type\"},{\"value\":\"1.75\",\"label\":\"Ring Size\"}]";
price = "1241.75";
"price_incl_tax" = "1241.75";
qty = 1;
"row_total" = "1241.75";
"row_total_incl_tax" = "1241.75";
"row_total_with_discount" = 0;
"tax_amount" = 0;
"tax_percent" = 0;
"weee_tax_applied" = "[]";
"weee_tax_applied_amount" = 0;
}
);
"items_qty" = 2;
"quote_currency_code" = USD;
"shipping_amount" = 400;
"shipping_discount_amount" = 0;
"shipping_incl_tax" = 400;
"shipping_tax_amount" = 0;
subtotal = "2583.75";
"subtotal_incl_tax" = "2583.75";
"subtotal_with_discount" = "2325.37";
"tax_amount" = 0;
"total_segments" = (
{
code = subtotal;
title = Subtotal;
value = "2583.75";
},
{
code = discount;
title = "Discount (ADAMAS10)";
value = "-258.38";
},
{
code = shipping;
title = "Shipping & Handling (Flat Shipping Charge - Flat Shipping Charge)";
value = 400;
},
{
area = taxes;
code = tax;
"extension_attributes" = {
"tax_grandtotal_details" = (
);
};
title = Tax;
value = 0;
},
{
area = footer;
code = "grand_total";
title = "Grand Total";
value = "2725.37";
}
);
"weee_tax_applied_amount" = "<null>";
};
}
I need to fetch values from total segments.The code is shipping and value is Shipping & Handling (Flat Shipping Charge - Flat Shipping Charge.
I tried to parse the json using the below code
if let res = json as? [String:Any]
{
if let dict = res as? [String:Any]
{
if let total = dict as? [[String:Any]]
{
}
}
}
I am getting value till dict.But i am getting nil in the total variable.
How to get the total segments value?
you're missing out to mention the inner key path of the dictionary. hope this is what you're trying to do
if let res = json as? [String:Any] {
if let paymentMethods = res["payment_methods"] as? [[String:Any]] {
}
if let totals = res["totals"] as? [String:Any] {
if let items = totals["items"] as? [[String:Any]] {
}
}
}
Use this to get the total_segments and value of all code keys:
var codes = [String]()
if let res = json as? [String:Any] {
if let totals = res["totals"] as? [String:Any] {
if let totalSegments = totals["total_segments"] as? [[String:Any]] {
for segment in totalSegments {
if let code = segment["code"] as? String {
codes.append(code)
}
}
}
}
}

parsing nested json issue

I am new here and I am parsing json which is array in side array so I can't understand how get inside array from json let me show you my json.
JSON
{
"subject_list" = (
{
"con_id" = 2;
"level_id" = 1;
"sub_id" = 4;
"sub_list" = (
{
"ch_id" = 17;
"ch_image" = "http://mobileapp.xmeducation.com/upload/1530600693.jpg";
"ch_name" = " 01. Measurement";
"con_id" = 2;
"level_id" = 1;
"sub_id" = 4;
},
{
"ch_id" = 23;
"ch_image" = "http://mobileapp.xmeducation.com/upload/1451930609.jpg";
"ch_name" = "1. Test Chapter";
"con_id" = 2;
"level_id" = 1;
"sub_id" = 4;
},
{
"ch_id" = 24;
"ch_image" = "http://mobileapp.xmeducation.com/upload/1884777188.jpg";
"ch_name" = "1. Test Chapter";
"con_id" = 2;
"level_id" = 1;
"sub_id" = 4;
},
{
"ch_id" = 25;
"ch_image" = "http://mobileapp.xmeducation.com/upload/1518702048.jpg";
"ch_name" = "1. Test Chapter";
"con_id" = 2;
"level_id" = 1;
"sub_id" = 4;
}
);
"sub_name" = Physics;
},
{
"con_id" = 2;
"level_id" = 1;
"sub_id" = 8;
"sub_list" = (
{
"ch_id" = 26;
"ch_image" = "http://mobileapp.xmeducation.com/upload/1437196139.jpg";
"ch_name" = " 1. Test Chapter";
"con_id" = 2;
"level_id" = 1;
"sub_id" = 8;
},
{
"ch_id" = 27;
"ch_image" = "http://mobileapp.xmeducation.com/upload/1903171865.jpg";
"ch_name" = "1. Test Chapter";
"con_id" = 2;
"level_id" = 1;
"sub_id" = 8;
}
);
"sub_name" = Chemistry;
},
{
"con_id" = 2;
"level_id" = 1;
"sub_id" = 9;
"sub_list" = (
{
"ch_id" = 31;
"ch_image" = "http://mobileapp.xmeducation.com/upload/1319333294.jpg";
"ch_name" = "1. Test Chapter";
"con_id" = 2;
"level_id" = 1;
"sub_id" = 9;
}
);
"sub_name" = Testing;
},
{
"con_id" = 2;
"level_id" = 1;
"sub_id" = 10;
"sub_list" = (
{
"ch_id" = 28;
"ch_image" = "http://mobileapp.xmeducation.com/upload/1373218664.jpg";
"ch_name" = "1. Test Chapter";
"con_id" = 2;
"level_id" = 1;
"sub_id" = 10;
}
);
"sub_name" = "Test Subject";
},
{
"con_id" = 2;
"level_id" = 1;
"sub_id" = 11;
"sub_list" = (
{
"ch_id" = 29;
"ch_image" = "http://mobileapp.xmeducation.com/upload/246189282.jpg";
"ch_name" = "1. Test Chapter";
"con_id" = 2;
"level_id" = 1;
"sub_id" = 11;
}
);
"sub_name" = "Test Subject 1";
},
{
"con_id" = 2;
"level_id" = 1;
"sub_id" = 12;
"sub_list" = (
{
"ch_id" = 30;
"ch_image" = "http://mobileapp.xmeducation.com/upload/1342731807.jpg";
"ch_name" = "1. Test Chapter";
"con_id" = 2;
"level_id" = 1;
"sub_id" = 12;
}
);
"sub_name" = "Test Subject 2";
}
);
this is my JSON and I want sub_list array from subject_list I have done some code let me show you.
CODE
func callSubChapAPI(){
let preferences = UserDefaults.standard
let studentlvl = "student_lvl"
let student_lvl = preferences.object(forKey: studentlvl) as! String
print(student_lvl)
let params = ["level_id": student_lvl]
Alamofire.request(subListWithChapter, method: .post, parameters: params).responseJSON(completionHandler: {(response) in
switch response.result{
case.success(let data):
print(data)
let json = JSON(data)
print(json)
do {
let decoder = JSONDecoder()
decoder.keyDecodingStrategy = .convertFromSnakeCase
let subjects = try decoder.decode(SubjectResponse.self, from: data as! Data)
print(subjects)
} catch {
print(error)
}
case.failure(let error):
print(error.localizedDescription)
}
})
}
here is my struct:
struct sub_list {
let ch_id : Int
let ch_image: String
let ch_name: String
let con_id: String
let level_id: String
let sub_id: String
}
Please see my code and json format please tell me how to get sub_list array from json please hell me and thanks in advance.
Actually subject_list is also an array so you need to iterate that as well.
let data = json["subject_list"]
print(data)
data.array?.forEach({ (subject) in
subject["sub_list"].array?.forEach({ (chapList) in
let chapter = sub_list(ch_id: chapList["ch_id"].intValue, ch_image: chapList["ch_image"].stringValue, ch_name: chapList["ch_name"].stringValue, con_id: chapList["con_id"].stringValue, level_id: chapList["level_id"].stringValue, sub_id: chapList["sub_id"].stringValue)
print(chapter)
self.chapListData.append(chapter)
})
})
self.collView.reloadData()
Recommendation
It's not worth adding any dependency for json parsing when you already have Apple provided Codable. It will reduce much of your boilerplate code added because of SwiftyJSON.
Below is a complete example for your current api,
struct SubjectResponse: Decodable {
let subjectList: [Subject]
}
struct Subject: Decodable {
let subList: [Chapter]
}
struct Chapter: Decodable {
let chId : Int
let chImage: String
let chName: String
let conId: Int
let levelId: Int
let subId: Int
}
Alamofire.request(subListWithChapter, method: .post, parameters: params).responseData() { (response) in
switch response.result {
case .success(let data):
do {
let decoder = JSONDecoder()
decoder.keyDecodingStrategy = .convertFromSnakeCase
let subjects = try decoder.decode(SubjectResponse.self, from: data)
print(subjects)
} catch {
print(error)
}
case .failure(let error):
print(error.localizedDescription)
}
}

parsing the nested json in swift

I am new to swift. Here is a deserialized JSON
Optional({
data = (
{
comments = (
{
comments = good;
"created_date" = "2018-01-01";
id = 2;
likes = 0;
"news_id" = 4;
status = 1;
"user_id" = 5;
}
);
date = 1510167600000;
id = 4;
images = "0c980d3f9cd0393a46bb04995d16177a.jpg";
likes = 25;
title = facebook;
totalcoments = 1;
},
{
comments = (
{
comments = "Good News";
"created_date" = "2018-01-01";
id = 1;
likes = 2;
"news_id" = 7;
status = 1;
"user_id" = 5;
},
{
comments = "well good";
"created_date" = "2018-01-02";
id = 3;
likes = 0;
"news_id" = 7;
status = 1;
"user_id" = 5;
},
{
comments = "well good";
"created_date" = "2018-01-02";
id = 4;
likes = 0;
"news_id" = 7;
status = 1;
"user_id" = 5;
},
{
comments = "well good";
"created_date" = "2018-01-03";
id = 5;
likes = 0;
"news_id" = 7;
status = 1;
"user_id" = 5;
}
);
date = 1511377200000;
id = 7;
images = "0c980d3f9cd0393a46bb04995d16177a.jpg";
likes = 3;
title = don;
totalcoments = 4;
},
{
comments = (
);
date = 1510513200000;
id = 8;
images = "0c980d3f9cd0393a46bb04995d16177a.jpg";
likes = 90;
title = Dell;
totalcoments = 0;
},
{
comments = (
);
date = 1515351600000;
id = 14;
images = "0c980d3f9cd0393a46bb04995d16177a.jpg";
likes = 0;
title = dfsfdsfdfs;
totalcoments = 0;
}
);
message = "Success.";
newsurl = "http://dev.nsol.sg/projects/sneakers/assets/brand_images/thumb/";
success = 1;
totalnews = 4;
})
Here in following class i am trying to parse the json
import UIKit
class getData: NSObject {
var descriptionn : String = ""
var image : String = ""
var likes : String = ""
var comments : Int = 0
var com:String=""
var like:String = ""
func getDataForTableView(results: [[String:Any]], index : Int){
var productArray = [String:Any]()
productArray = results[index]
descriptionn = productArray["title"]! as! String
image = productArray["images"]! as! String
likes=productArray["likes"]! as! String
comments=productArray["totalcoments"]! as! Int
/*{
comments = (
{
comments = good;
"created_date" = "2018-01-01";
id = 2;
likes = 0;
"news_id" = 4;
status = 1;
"user_id" = 5;
}
);*/
//code for parsing the "comments" section of json is
let comment=productArray["comments"] as! [String:Any]?
com = comment!["comments"] as! String
like = comment!["likes"] as! String
}
}
The code for populating the table view cell is below
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "discoveryNewscell") as! DiscoveryNewsTableViewCell
classObject.getDataForTableView(results: results, index: indexPath.row)
cell.sneakerImageView.image=filteredsneakernews[indexPath.row].image
cell.newsTitle.text = classObject.descriptionn
cell.likes.text=classObject.likes
cell.comments.text=String(classObject.comments)
let imageURLPathString = newsurl + classObject.image
print("comments")
print(classObject.image)
let url1 = URL(string: imageURLPathString)
print("xyz", url1)
let data = try? Data(contentsOf: url1!)
if let imageData = data {
let image = UIImage(data: imageData)
cell.sneakerImageView.image = image
}
return cell
}
Here is didSelectRowAt Index path function
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let storybaord=UIStoryboard(name: "Main", bundle: nil)
let tabBar=storybaord.instantiateViewController(withIdentifier: "tabBar") as! UITabBarController
let DVC = tabBar.viewControllers?[0] as! NewsViewController
classObject.getDataForTableView(results: results, index: indexPath.row)
let imageURLPathString = newsurl + classObject.image
let url1 = URL(string: imageURLPathString)
print("xyz", url1)
let data = try? Data(contentsOf: url1!)
if let imageData = data {
let image = UIImage(data: imageData)
DVC.getImage = image!
}
let news = classObject.descriptionn
DVC.getNews = news
DVC.getnumberOfComment=classObject.comments
print("comment")
//code for passing "comment" part of json to view controller
print(classObject.like)
print(classObject.com)
DVC.getlikes=classObject.like
DVC.getcomment=classObject.com
self.navigationController?.pushViewController(tabBar, animated: true)
}
I want when a particular row is selected from table view it should display the number of "likes" and "comments" that are from nested part of JSON on "Likes Screen" view controller
comments = (
{
comments = good;
"created_date" = "2018-01-01";
id = 2;
likes = 0;
"news_id" = 4;
status = 1;
"user_id" = 5;
}
);
How to read and parse that nested "Comments" in JSON? You can download the project from this link .https://drive.google.com/file/d/1VYM4nywaBYDP2BcyBYPPNUYAu_18mhd9/view?usp=sharing

Trouble with nested Json data

I'm having a ton of trouble trying to access the nested json data (pasted at the bottom). I'm able write:
var dataResults = jsonResult["data"] as NSDictionary
In order to create a dictionary containing the data within "data", however, Xcode will not allow me to call on anything within "data" such as the information within "current_condition". I've tried to make current_condition it's own dictionary like so:
var results = dataResults["current_condition"] as NSDictionary
But it would seem that this is turning up as nil
I have also attempted accessing using the standard method for calling nested loops:
var dataResults = jsonResult["data"]["current_condition"] as NSDictionary
But this results in a compiler error.
Any help? Much appreciated!
Json data:
{
data = {
"current_condition" = (
{
cloudcover = 0;
humidity = 68;
"observation_time" = "01:39 AM";
precipMM = "0.0";
pressure = 1017;
"temp_C" = 20;
"temp_F" = 68;
visibility = 10;
weatherCode = 143;
weatherDesc = (
{
value = Mist;
}
);
weatherIconUrl = (
{
value = "http://cdn.worldweatheronline.net/images/wsymbols01_png_64/wsymbol_0006_mist.png";
}
);
winddir16Point = NE;
winddirDegree = 50;
windspeedKmph = 7;
windspeedMiles = 4;
}
);
request = (
{
query = "London, United Kingdom";
type = City;
}
);
weather = (
{
date = "2014-07-25";
precipMM = "1.5";
tempMaxC = 27;
tempMaxF = 81;
tempMinC = 14;
tempMinF = 57;
weatherCode = 353;
weatherDesc = (
{
value = "Light rain shower";
}
);
weatherIconUrl = (
{
value = "http://cdn.worldweatheronline.net/images/wsymbols01_png_64/wsymbol_0009_light_rain_showers.png";
}
);
winddir16Point = NE;
winddirDegree = 54;
winddirection = NE;
windspeedKmph = 15;
windspeedMiles = 10;
},
{
date = "2014-07-26";
precipMM = "5.8";
tempMaxC = 28;
tempMaxF = 83;
tempMinC = 16;
tempMinF = 61;
weatherCode = 176;
weatherDesc = (
{
value = "Patchy rain nearby";
}
);
weatherIconUrl = (
{
value = "http://cdn.worldweatheronline.net/images/wsymbols01_png_64/wsymbol_0009_light_rain_showers.png";
}
);
winddir16Point = NNE;
winddirDegree = 12;
winddirection = NNE;
windspeedKmph = 11;
windspeedMiles = 7;
},
{
date = "2014-07-27";
precipMM = "0.2";
tempMaxC = 26;
tempMaxF = 80;
tempMinC = 13;
tempMinF = 55;
weatherCode = 116;
weatherDesc = (
{
value = "Partly Cloudy";
}
);
weatherIconUrl = (
{
value = "http://cdn.worldweatheronline.net/images/wsymbols01_png_64/wsymbol_0002_sunny_intervals.png";
}
);
winddir16Point = NW;
winddirDegree = 321;
winddirection = NW;
windspeedKmph = 14;
windspeedMiles = 9;
},
{
date = "2014-07-28";
precipMM = "1.9";
tempMaxC = 26;
tempMaxF = 78;
tempMinC = 12;
tempMinF = 54;
weatherCode = 116;
weatherDesc = (
{
value = "Partly Cloudy";
}
);
weatherIconUrl = (
{
value = "http://cdn.worldweatheronline.net/images/wsymbols01_png_64/wsymbol_0002_sunny_intervals.png";
}
);
winddir16Point = N;
winddirDegree = 351;
winddirection = N;
windspeedKmph = 13;
windspeedMiles = 8;
},
{
date = "2014-07-29";
precipMM = "0.0";
tempMaxC = 28;
tempMaxF = 82;
tempMinC = 16;
tempMinF = 60;
weatherCode = 113;
weatherDesc = (
{
value = Sunny;
}
);
weatherIconUrl = (
{
value = "http://cdn.worldweatheronline.net/images/wsymbols01_png_64/wsymbol_0001_sunny.png";
}
);
winddir16Point = NNW;
winddirDegree = 329;
winddirection = NNW;
windspeedKmph = 13;
windspeedMiles = 8;
}
);
};
}
Oddly enough, I have a sample weather app that I converted to Swift. I get my data elsewhere, but I found this library to be a great way to deal with JSON in Swift: https://github.com/dankogai/swift-json/. Much cleaner and easier.
It's because the subscript in Dictionary returns an optional:
subscript (key: KeyType) -> ValueType?
Meaning you have to unwrap the optional before you can downcast:
let dataResults = jsonResult["data"]! as NSDictionary
let results = dataResults["current_condition"]! as NSDictionary
As of Beta 3 you can access the value directly without downcasting, like this:
let dataResults = jsonResult["data"]!
let results = dataResults["current_condition"]
However I would suggest you wrap it around an if-let to make sure you don't unwrap nil values.