How to unwrap my variables used in UILabels properly? - ios

EDIT 1: Added my revision cellForRowAtIndexPath code at the bottom of the post
EDIT 2: Added my new EditorialElement code
I am having difficulty properly unwrapping my UILabel text inputs properly, so all of my text says "Optional(Author name)" (for example, this is an app for a newspaper). I have tried to force unwrap my variables in different ways but was not able to make it work.
The text input for my UILabels are created in the following way. The corresponding class is "EditorialElement", which has the following property definitions:
class EditorialElement: NSObject {
var title: String! // title
var nodeID: Int? // nid
var timeStamp: Int // revision_timestamp
var imageURL: String? // image_url
var author: String? // author
var issueNumber: String! // issue_int
var volumeNumber: String! // volume_int
var articleContent: String! // html_content
/* To get an NSDate objec from Unix timestamp
var date = NSDate(timeIntervalSince1970: timeStamp) */
init(title: String, nodeID: Int, timeStamp: Int, imageURL: String, author: String, issueNumber: String, volumeNumber: String, articleContent: String) {
self.title = title
self.nodeID = nodeID
self.timeStamp = timeStamp
self.imageURL = imageURL
self.author = author
self.issueNumber = issueNumber
self.volumeNumber = volumeNumber
self.articleContent = articleContent
}
override func isEqual(object: AnyObject!) -> Bool {
return (object as! EditorialElement).nodeID == self.nodeID
}
override var hash: Int {
return (self as EditorialElement).nodeID!
}
}
Then, I use this class to retrieve data from my JSON file and parse it into an "editorialObjects" array (sorry about all the commenting and bad spacing):
func populateEditorials() {
if populatingEditorials {
return
}
populatingEditorials = true
Alamofire.request(GWNetworking.Router.Editorials(self.currentPage)).responseJSON() { response in
if let JSON = response.result.value {
/*
if response.result.error == nil {
*/
/* Creating objects for every single editorial is long running work, so we put that work on a background queue, to keep the app very responsive. */
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0)) {
/* Making an array of all the node IDs from the JSON file */
var nodeIDArray : [Int]
if (JSON .isKindOfClass(NSDictionary)) {
for node in JSON as! Dictionary<String, AnyObject> {
let nodeIDValue = node.0
var lastItem : Int = 0
self.nodeIDArray.addObject(nodeIDValue)
if let editorialElement : EditorialElement = EditorialElement(title: "init", nodeID: 0, timeStamp: 0, imageURL: "init", author: "init", issueNumber: "init", volumeNumber: "init", articleContent: "init") {
editorialElement.title = node.1["title"] as! String
editorialElement.nodeID = Int(nodeIDValue)
let timeStampString = node.1["revision_timestamp"] as! String
editorialElement.timeStamp = Int(timeStampString)!
editorialElement.imageURL = String(node.1["image_url"])
editorialElement.author = String(node.1["author"])
editorialElement.issueNumber = String(node.1["issue_int"])
editorialElement.volumeNumber = String(node.1["volume_int"])
editorialElement.articleContent = String(node.1["html_content"])
lastItem = self.editorialObjects.count
print (editorialElement.nodeID)
self.editorialObjects.addObject(editorialElement)
/* Sorting the elements in order of newest to oldest (as the array index increases] */
let timestampSortDescriptor = NSSortDescriptor(key: "timeStamp", ascending: false)
self.editorialObjects.sortUsingDescriptors([timestampSortDescriptor])
let indexPaths = (lastItem..<self.editorialObjects.count).map { NSIndexPath(forItem: $0, inSection: 0) }
/*
nodeIDArray[nodeCounter] = jsonValue{nodeCounter}.string
let editorialInfos : EditorialElement = ((jsonValue as! NSDictionary].1["\(nodeIDArray[nodeCounter]]"] as! [NSDictionary]].map { EditorialElement(title: $0["title"] as! String, nodeID: $0["nid"] as! Int, timeStamp: $0["revision_timestamp"] as! Int, imageURL: $0["image_url"] as! String, author: $0["author"], issueNumber: $0["issue_int"] as! Int, volumeNumber: $0["volume_int"] as! Int, articleContent: $0["html_content"] as! String] // I am going to try to break this line down to simplify it and fix the build errors
*/
}
print(self.editorialObjects.count)
}
}
dispatch_async(dispatch_get_main_queue()) {
self.editorialsTableView.reloadData()
}
self.currentPage++
}
}
self.populatingEditorials = false
}
}
And then I just use those objects for my labels in my cellForRowAtIndexPath method:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let row = indexPath.row
let cell = tableView.dequeueReusableCellWithIdentifier(EditorialTableCellIdentifier, forIndexPath: indexPath) as! EditorialsTableViewCell
let title = (editorialObjects.objectAtIndex(indexPath.row) as! EditorialElement).title
let timeStamp = (editorialObjects.objectAtIndex(indexPath.row) as! EditorialElement).timeStamp
let timeStampDateObject = NSDate(timeIntervalSince1970: NSTimeInterval(Int((editorialObjects.objectAtIndex(indexPath.row) as! EditorialElement).timeStamp)))
timeStampDateString = dateFormatter.stringFromDate(timeStampDateObject)
let imageURL = (editorialObjects.objectAtIndex(indexPath.row) as! EditorialElement).imageURL
let author : String! = (editorialObjects.objectAtIndex(indexPath.row) as! EditorialElement).author!
let issueNumber = (editorialObjects.objectAtIndex(indexPath.row) as! EditorialElement).issueNumber
let volumeNumber = (editorialObjects.objectAtIndex(indexPath.row) as! EditorialElement).volumeNumber
let articleContent = (editorialObjects.objectAtIndex(indexPath.row) as! EditorialElement).articleContent
let nodeID = (editorialObjects.objectAtIndex(indexPath.row) as! EditorialElement).nodeID
/* Unlike the Pictures Collection View, there is no need to create another Alamofire request here, since we already have all the content we want from the JSON we downloaded. There is no URL that we wish to place a request to to get extra content. */
cell.editorialHeadlineLabel.font = UIFont.preferredFontForTextStyle(UIFontTextStyleHeadline)
cell.editorialHeadlineLabel.text = title
cell.editorialAuthorLabel.font = UIFont.preferredFontForTextStyle(UIFontTextStyleSubheadline)
cell.editorialAuthorLabel.text = author
cell.editorialPublishDateLabel.font = UIFont.preferredFontForTextStyle(UIFontTextStyleSubheadline)
cell.editorialPublishDateLabel.text = timeStampDateString
return cell
}
Where should I be force-unwrapping my variables ?
EDIT 1: Revised code
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let row = indexPath.row
guard let cell = tableView.dequeueReusableCellWithIdentifier(EditorialTableCellIdentifier, forIndexPath: indexPath) as? EditorialsTableViewCell else {
print ("error: editorialsTableView cell is not of class EditorialsTableViewCell, we will use RandomTableViewCell instead")
return tableView.dequeueReusableCellWithIdentifier(EditorialTableCellIdentifier, forIndexPath: indexPath) as! RandomTableViewCell
}
if let editorialObject = editorialObjects.objectAtIndex(indexPath.row) as? EditorialElement {
// we just unwrapped editorialObject
let title = editorialObject.title ?? "" // if editorialObject.title == nil, then we return an empty string.
let timeStampDateObject = NSDate(timeIntervalSince1970: NSTimeInterval(editorialObject.timeStamp))
let timeStampDateString = dateFormatter.stringFromDate(timeStampDateObject)
let author = editorialObject.author ?? ""
let issueNumber = editorialObject.issueNumber ?? ""
let volumeNumber = editorialObject.volumeNumber ?? ""
let articleContent = editorialObject.articleContent ?? ""
let nodeID = editorialObject.nodeID ?? 0
cell.editorialHeadlineLabel.font = UIFont.preferredFontForTextStyle(UIFontTextStyleHeadline)
cell.editorialHeadlineLabel.text = title
cell.editorialAuthorLabel.font = UIFont.preferredFontForTextStyle(UIFontTextStyleSubheadline)
cell.editorialAuthorLabel.text = String(author)
cell.editorialPublishDateLabel.font = UIFont.preferredFontForTextStyle(UIFontTextStyleSubheadline)
cell.editorialPublishDateLabel.text = timeStampDateString
} else {
}
return cell
}
EDIT 2: new EditorialElement code
class EditorialElement: NSObject {
var title: String // title
var nodeID: Int // nid
var timeStamp: Int // revision_timestamp
var imageURL: String // image_url
var author: String // author
var issueNumber: String // issue_int
var volumeNumber: String // volume_int
var articleContent: String // html_content
/* To get an NSDate objec from Unix timestamp
var date = NSDate(timeIntervalSince1970: timeStamp) */
init(title: String, nodeID: Int, timeStamp: Int, imageURL: String, author: String, issueNumber: String, volumeNumber: String, articleContent: String) {
self.title = title
self.nodeID = nodeID
self.timeStamp = timeStamp
self.imageURL = imageURL
self.author = author
self.issueNumber = issueNumber
self.volumeNumber = volumeNumber
self.articleContent = articleContent
}
override func isEqual(object: AnyObject!) -> Bool {
return (object as! EditorialElement).nodeID == self.nodeID
}
override var hash: Int {
return (self as EditorialElement).nodeID
}
}

Couple things. 1. You should only be force unwrapping them if you know that there is going to be something there. But if you're super confident in that (or you don't expect/want the app to work without them) then you should just have them be forced unwrapped from the get go. Theres no point in doing var imageURL: String? only to write imageURL! everywhere in the code. The point of optionals is to allow you to gracefully handle situations wherein the object might be nil.
Anyway, assuming this is the correct structure for you the first thing I would do is if let the return of object at index. So writing:
if let element = editorialObjects.objectAtIndex(indexPath.row) as? EditorialElement
Now you can use element as an EditorialElement throughout cell for row. From there you can decide how/when to unwrap everything else.
So let author : String! = (editorialObjects.objectAtIndex(indexPath.row) as! EditorialElement).author! would become either
let author = element.author! or you could if let to avoid a crash and handle the case where it is nil. if let author = element.author { // do something }

My opinion, change your variable declaration to not nil variable, example: var author: String!
Then, when you set value to your variable, set it is empty character (or default value) if it's nil:
editorialElement.author = String(node.1["author"]) ?? ""
After that, you can use your variable without unwrap everywhere.

In most of the cases forced unwrapping is a code smell. Don't use it unless you're linking IBOutlets or in some other exceptional cases. Try to properly unwrap your variables.
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let row = indexPath.row
guard let cell = tableView.dequeueReusableCellWithIdentifier(EditorialTableCellIdentifier, forIndexPath: indexPath) as? EditorialsTableViewCell else {
// if we fall here cell isn't a EditorialsTableViewCell
// handle that properly
}
if let editorialObject = editorialObjects.objectAtIndex(indexPath.row) as? EditorialElement {
// editorialObject here is unwrapped
let title = editorialObject.title ?? "" // if editorialObject.title == nil we store empty string
let timeStampDateObject = NSDate(timeIntervalSince1970: NSTimeInterval(editorialObject.timeStamp))
let timeStampDateString = dateFormatter.stringFromDate(timeStampDateObject)
cell.editorialHeadlineLabel.font = UIFont.preferredFontForTextStyle(UIFontTextStyleHeadline)
cell.editorialHeadlineLabel.text = title
cell.editorialPublishDateLabel.font = UIFont.preferredFontForTextStyle(UIFontTextStyleSubheadline)
cell.editorialPublishDateLabel.text = timeStampDateString
} else {
// no such editorial object - log error.
// return empty cell or do more sofisticated error handling
}
return cell
}

Related

Put Tableview Cells in Sections Alphabetically- Swift 3 and Firebase Database

I am using firebase to load data into tableview cells, and here is the structure of my data.
struct postStruct {
let title : String!
let author : String!
let bookRefCode : String!
let imageDownloadString : String!
let status : String!
let reserved : String!
let category : String!
let dueDate : String!
}
Now I have the posts sorted alphabetically using
self.posts.sort(by: { $0.title < $1.title })
but I do not know how to place the cells that start with A in a section "A", and "B", and so on.
class DirectoryTableView: UITableViewController {
var posts = [postStruct]()
override func viewDidLoad() {
let databaseRef = Database.database().reference()
databaseRef.child("Books").queryOrderedByKey().observe(.childAdded, with: {
snapshot in
var snapshotValue = snapshot.value as? NSDictionary
let title = snapshotValue!["title"] as? String
snapshotValue = snapshot.value as? NSDictionary
let author = snapshotValue!["author"] as? String
snapshotValue = snapshot.value as? NSDictionary
let bookRefCode = snapshotValue!["bookRefCode"] as? String
snapshotValue = snapshot.value as? NSDictionary
let status = snapshotValue!["status"] as? String
snapshotValue = snapshot.value as? NSDictionary
let reserved = snapshotValue!["reserved"] as? String
snapshotValue = snapshot.value as? NSDictionary
let category = snapshotValue!["category"] as? String
snapshotValue = snapshot.value as? NSDictionary
let dueDate = snapshotValue!["dueDate"] as? String
snapshotValue = snapshot.value as? NSDictionary
self.posts.insert(postStruct(title: title, author: author, bookRefCode: bookRefCode, status: status, reserved: reserved, category: category, dueDate: dueDate) , at: 0)
self.tableView.reloadData()
})
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return posts.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCell(withIdentifier: "cell")
let databaseRef = Database.database().reference()
let label1 = cell?.viewWithTag(1) as! UILabel
label1.text = posts[indexPath.row].title
let label2 = cell?.viewWithTag(2) as! UILabel
label2.text = posts[indexPath.row].author
let label3 = cell?.viewWithTag(3) as! UILabel
label3.text = posts[indexPath.row].bookRefCode
let label4 = cell?.viewWithTag(4) as! UILabel
label4.text = posts[indexPath.row].status
let label5 = cell?.viewWithTag(5) as! UILabel
label5.text = posts[indexPath.row].category
let image1 = cell?.viewWithTag(6) as! UILabel
image1.text = posts[indexPath.row].imageDownloadString
let label6 = cell?.viewWithTag(7) as! UILabel
label6.text = posts[indexPath.row].reserved
let label9 = cell?.viewWithTag(9) as! UILabel
label9.text = posts[indexPath.row].dueDate
return cell!
}
Any ideas, please help! I have tried to sort them with different methods, but I'm confused!
First, you'll need a new structure to save your posts grouped by it's first title letter: let postsAlphabetically: [String:[Post]]
You didn't specify the Swift version, so assuming you'll migrate to Swift 4, you can sort the data and group it using a single line:
let postsAlphabetically = Dictionary(grouping: self.posts) { $0.title.first! }
// E.g: ["A" : ["A book", "Another book"], "B" : ["Blue Book", "Black Book"]]
Later on, you'll use postsAlphabetically instead of self.posts in your cellForRowAt method.
P.S: Type names in Swift are written in upper camel case (PostSimple not postSimple). And the type itself is omitted (Author instead of AuthorClass).

how to use getter setter method for search bar in swift 3.1.1?

I have implemented search bar in my app. I have a JSON data of some hotels. (name, image, address, mobile number, email). I am getting the name in the search result, but I want to get the entire data of a particular hotel. So, please help me to implement search functionality. I want to use getter setter method. Please help. Thanks in advance!!
let str = self.catArr [indexPath.row]
let url1 = "myapi"
let url2 = url1+str
let allowedCharacterSet = (CharacterSet(charactersIn: " ").inverted)
if let url = url2.addingPercentEncoding(withAllowedCharacters: allowedCharacterSet)
{
Alamofire.request(url).responseJSON { (responseData) -> Void in
if (responseData.result.value) == nil
{
print("Error!!")
}
//if (responseData.result.value) != nil
else
{
let response = JSON(responseData.result.value!)
if let resData = response["data"].arrayObject
{
self.arrResponse1 = resData as! [[String: AnyObject]]
for item in self.arrResponse1
{
let name = item["name"] as! String
self.arrName.append(name)
let add = item["address"] as! String
self.arrAddress.append(add)
let web = item["website"] as! String
self.arrWebsite.append(web)
let email = item["email"] as! String
self.arrEmail.append(email)
let mob = item["mobile"] as! String
self.arrMobile.append(mob)
let city = item["city"] as! String
self.arrCity.append(city)
let state = item["state"] as! String
self.arrState.append(state)
let dist = item["district"] as! String
self.arrDistrict.append(dist)
let area = item["area"] as! String
self.arrArea.append(area)
let img = item["image"] as! String
self.arrImage.append(img)
let rating = item["rating"] as! String
self.arrRating.append(rating)
let id = item["id"] as! String
self.arrId.append(id)
}
}
}
}
}
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String)
{
filteredName = self.nameArr.filter({ (String) -> Bool in
let tmp: NSString = String as NSString
let range = tmp.range(of: searchText, options: NSString.CompareOptions.caseInsensitive)
return range.location != NSNotFound
})
if(filteredName.count == 0)
{
searchActive = false;
}
else
{
searchActive = true;
print("Search Array = \(filteredName)")
}
self.infoTableView.reloadData()
}
//showing in tableview
if(searchActive)
{
cell.lblName.text = self.filteredName [indexPath.row]
}
else
{
let urlImage = str1+self.imageArr [indexPath.row]+".jpg"
cell.imgView.sd_setImage(with: URL(string: urlImage), placeholderImage: UIImage(named: ""))
cell.lblName.text = self.nameArr [indexPath.row]
cell.lblAddress.text = self.addressArr [indexPath.row]
cell.lblCity.text = self.cityArr [indexPath.row]
cell.lblPhone.text = self.mobileArr [indexPath.row]
cell.lblEmail.text = self.emailArr [indexPath.row]
cell.lblStar.text = self.ratingArr [indexPath.row]
cell.phoneString = self.mobileArr [indexPath.row]
cell.emailString = self.emailArr [indexPath.row]
}
Instead of having multiple array to store each value what you need is array of custom class/struct object and then its easy to use filter with it.
struct Item { //Make some suitable name
let id: String
let name: String
let address: String
let website: String
let email: String
let mobile: String
let city: String
let state: String
let district: String
let area: String
let image: String
let rating: String
init(dictionary: [String:Any]) {
self.id = dictionary["id"] as? String ?? "Default Id"
self.name = dictionary["name"] as? String ?? "Default name"
self.address = dictionary["address"] as? String ?? "Default address"
self.website = dictionary["website"] as? String ?? "Default website"
self.email = dictionary["email"] as? String ?? "Default email"
self.mobile = dictionary["mobile"] as? String ?? "Default mobile"
self.city = dictionary["city"] as? String ?? "Default city"
self.state = dictionary["state"] as? String ?? "Default state"
self.district = dictionary["district"] as? String ?? "Default district"
self.area = dictionary["area"] as? String ?? "Default area"
self.image = dictionary["image"] as? String ?? "Default image"
self.rating = dictionary["rating"] as? String ?? "Default rating"
}
}
Now you need only two array of type [Item] one for normal data and one for filter data, so declare two array like this.
var items = [Item]()
var filterItems = [Item]()
Initialized this items array like this way in your Alamofire request.
let response = JSON(responseData.result.value!)
if let resData = response["data"].arrayObject as? [[String: Any]] {
self.items = resData.map(Item.init)
}
Now use this two array in tableView method like this.
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if searchActive {
return filterItems.count
}
return items.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! YourCustomCell
let item = searchActive ? filterItems[indexPath.row] : items[indexPath.row]
cell.lblName.text = item.name
cell.lblAddress.text = item.address
//...Set others detail same way
return cell
}
Now with textDidChange filter your items array this way.
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String)
{
filterItems = self.items.filter({
$0.name.localizedCaseInsensitiveContains(searchText)
})
if(filterItems.count == 0) {
searchActive = false
}
else {
searchActive = true
print("Search Array = \(filterItems)")
}
self.infoTableView.reloadData()
}

How to Unwrap a Modal class object in Swift 3

Model Class:
class CountryModel: NSObject {
var name:NSString!
var countryId:NSString!
init(name: NSString, countryId: NSString) {
self.name = name
self.countryId = countryId
}
}
ViewController:
var nameArr = [CountryModel]() // Model Class object
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var cell = UITableViewCell()
cell = tableView.dequeueReusableCell(withIdentifier: "cell")!
let arr = nameArr[indexPath.row] // How to do if let here
let str:String? = "\(arr.countryId) \(arr.name)"
if let sstr = str{
cell.textLabel?.text = sstr
}
return cell
}
How should one unwrap this because output is an optional, if I try to unwrap nameArr[indexPath.row] gives an error initializer for conditional binding must have optional type, not "country modal"
It works fine I am not concatenating arr.countryId with arr.name
This works fine
var nameArr = [CountryModal]()
nameArr.append(CountryModal.init(name: "harshal", countryId: "india"))
nameArr.append(CountryModal.init(name: "james", countryId: "australia"))
let arr = nameArr[0]
let str:String? = "\(arr.countryId!) \(arr.name!)"
if let sstr = str{
print(sstr)
}
let arr2 = nameArr[1]
let str2:String? = "\(arr2.countryId!) \(arr2.name!)"
if let sstr2 = str2{
print(sstr2)
}
You can try this library https://github.com/T-Pham/NoOptionalInterpolation. It does exactly that
import NoOptionalInterpolation
let n: Int? = 1
let t: String? = nil
let s: String? = "string1"
let o: String?? = "string2"
let i = "\(n) \(t) \(s) \(o)"
print(i) // 1 string1 string2
NoOptionalInterpolation gets rid of "Optional(...)" and "nil" in Swift's string interpolation. This library ensures that the text you set never ever includes that annoying additional "Optional(...)". You can also revert to the default behaviour when needed.
Also, please note that this does not affect the print function. Hence, print(o) (as opposed to print("(o)"), o as in the example above) would still print out Optional(Optional("string2"))
Avoid forced unwrapping as much as possible. i.e. using '!'. Whenever you see !, try and think of it as code smell.
For json parsing you would not know for sure if the values for countryId and name exists or not. So, it makes sense to keep them as optionals and gracefully try to unwrap them later.
class CountryModel {
var name: String?
var countryId: String?
init(name: String?, countryId: String?) {
self.name = name
self.countryId = countryId
}
}
var nameArr = [CountryModel]()
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: "cell") else {
fatalError("cell should always be initialised")
}
var str: String?
let arr = nameArr[indexPath.row] // How to do if let here
if let countryId = arr.countryId, let name = arr.name {
str = "\(countryId) \(name)"
}
if let unwrappedString = str {
cell.textLabel?.text = unwrappedString
}
return cell
}
Bonus:
You could simplify your json parsing a lot using the Object Mapper library. It simplifies a lot of your parsing logic.

Implementing Search Functionality

hello I have implemented an auto complete search on my app. I have cities stored in my mysql database and in app when user types any character or word, the app fetches result from the database and shows it. Now there is a small programming problem I am having and I don't know how to solve it.
The problem is in the same Array in which I am getting a City, I am getting country name and state name as well. As I have implemented a search only on cities not on state and country, I actually need the other columns(state,country) of those rows which are displaying based on user search city. I'll paste the code here for better understanding
class CityTableViewController: UITableViewController, UISearchResultsUpdating {
var dict = NSDictionary()
var filterTableData = [String]()
var resultSearchController = UISearchController()
var newTableData = [String]()
override func viewDidLoad() {
super.viewDidLoad()
self.resultSearchController = ({
let controller = UISearchController(searchResultsController: nil)
controller.searchResultsUpdater = self
controller.dimsBackgroundDuringPresentation = false
controller.searchBar.sizeToFit()
self.tableView.tableHeaderView = controller.searchBar
return controller
})()
self.tableView.reloadData()
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if(self.resultSearchController.active){
return self.filterTableData.count
}else {
return dict.count
}
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! CountryTableViewCell
if(self.resultSearchController.active){
cell.cityNameLabel.text = filterTableData[indexPath.row]
cell.countryNameLabel.text = get the country name
cell.stateNameLabel.text = get stateName
return cell
}else{
cell.cityNameLabel.text = (((self.dict["\(indexPath.row)"] as?NSDictionary)!["City"] as?NSDictionary)!["name"] as?NSString)! as String
return cell
}
}
func updateSearchResultsForSearchController(searchController: UISearchController) {
filterTableData.removeAll(keepCapacity: false)
let searchWord = searchController.searchBar.text!
getCityNamesFromServer(searchWord)
let searchPredict = NSPredicate(format: "SELF CONTAINS[c] %#", searchController.searchBar.text!)
print("searchPredict is \(searchController.searchBar.text!)")
for var i = 0; i < self.dict.count; i++ {
let cityName = (((self.dict["\(i)"] as?NSDictionary)!["City"] as?NSDictionary)!["name"] as?NSString)! as String
let countryName = (((self.dict["\(i)"] as?NSDictionary)!["Country"] as?NSDictionary)!["name"] as?NSString)! as String
let stateName = (((self.dict["\(i)"] as?NSDictionary)!["State"] as?NSDictionary)!["name"] as?NSString)! as String
newTableData.append(cityname)
}
let array = (newTableData as NSArray).filteredArrayUsingPredicate(searchPredict)
print("array is\(array)")
filterTableData = array as! [String]
self.tableView.reloadData()
}
func getCityNamesFromServer(searchWord:String){
let url:String = "http://localhost/"
let params = ["city":searchWord]
ServerRequest.postToServer(url, params: params) { result, error in
if let result = result {
print(result)
self.dict = result
}
}
}
}
If I try to setup new array of state and country then data doesn't shows up correctly. cities don't belong to his own state shows up. So How I can keep the order correctly.
Array:
dict
0 = {
City = {
code = 10430;
"country_id" = 244;
id = 8932;
name = Laudium;
"state_id" = 4381;
"updated_at" = "<null>";
};
Country = {
id = 244;
name = "South Africa";
};
State = {
"country_id" = 244;
id = 4381;
name = Gauteng;
};
}; etc
newTableData
["Lynnwood", "Lyndhurst", "Laudium"] etc
filterTableData
["Laudium", "La Lucia", "Lansdowne"] etc
You should search the dictionary for the matches and store the matched keys in an array and reference to these keys in the results.
func updateSearchResultsForSearchController(searchController: UISearchController) {
let searchWord = searchController.searchBar.text!
getCityNamesFromServer(searchWord)
self.filteredKeys.removeAll()
for (key, value) in self.dict {
let valueContainsSearchWord: Bool = (((value as? NSDictionary)?["City"] as? NSDictionary)?["name"] as? String)?.uppercaseString.containsString(searchWord.uppercaseString) ?? false
if valueContainsSearchWord {
self.filteredKeys.append(key as! String)
}
}
self.tableView.reloadData()
}
Fill the tableview with this filtered keys:
let key = self.filteredKeys[indexPath.row]
let dictionary = self.dict[key] as! NSDictionary
cell.cityNameLabel.text = ((dictionary["City"] as? NSDictionary)!["name"] as? NSString)! as String
cell.countryNameLabel.text = ((dictionary["Country"] as? NSDictionary)!["name"] as? NSString)! as String
cell.stateNameLabel.text = ((dictionary["State"] as? NSDictionary)!["name"] as? NSString)! as String
return cell
Just save this filtered dictionary (self.filteredDictionary) and use that to populate the tableView.
I think the other problem is, when you call the server's search method (getCityNamesFromServer:) from updateSearchResultsForSearchController: the response from the server comes asynchronously and the process afterwards is using the old dictionary data, because the new one is not ready at the time of the processing.
You should try modifying the getCityNamesFromServer: method with a block completion like this:
func updateSearchResultsForSearchController(searchController: UISearchController) {
// Get search word
getCityNamesFromServer(searchWord) { () -> Void in
// Rest of the code comes here
}
}
func getCityNamesFromServer(searchWord:String, completionHandler: (() -> Void) ) {
let url:String = "http://localhost/"
let params = ["city":searchWord]
ServerRequest.postToServer(url, params: params) { result, error in
if let result = result {
print(result)
self.dict = result
}
completionHandler()
}
}

Swift JSON with Alamofire - Unexpectedly found nil while unwrapping an Optional value

I'm trying to parse a JSON in my app and it's not working.
Here is a look at the JSON I'm trying to parse:
Check out the following screenshots. I get the error "Unexpectedly found nil while unwrapping an optional value" on the line
editorialElement.title = nodeElement!.valueForKey("title") as! String
Can anyone help me properly parse this JSON ?
EDIT 1: Here is more code. It shows what class I'm using (i.e. what objects I am creating based on the JSON file). I imagine this is what you meant by where I initialize JSON objects.
Here is my router as well (build using Alamofire). I feel like something might be wrong with it but at the same time, it makes a lot of sense and I don't know what missing:
EDIT 2: Here is the actual code:
func populateEditorials() {
if populatingEditorials {
return
}
populatingEditorials = true
Alamofire.request(GWNetworking.Router.Editorials(self.currentPage)).responseJSON() { response in
if let JSON = response.result.value {
/*
if response.result.error == nil {
*/
/* Creating objects for every single editorial is long running work, so we put that work on a background queue, to keep the app very responsive. */
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0)) {
/*
// Making an array of all the node IDs from the JSON file
let nodeIDArray : [String]
var nodeCounter : Int = 0 */
for node in JSON as! NSDictionary {
var nodeElement = JSON.valueForKey(String(node))
self.nodeIDArray.addObject(String(node.key))
var editorialElement : EditorialElement = EditorialElement(title: "init", nodeID: 0, timeStamp: 0, imageURL: "init", author: "init", issueNumber: "init", volumeNumber: "init", articleContent: "init")
editorialElement.title = nodeElement!.valueForKey("title") as! String
editorialElement.nodeID = nodeElement!.valueForKey("nid") as! Int
editorialElement.timeStamp = nodeElement!.valueForKey("revision_timestamp") as! Int
editorialElement.imageURL = nodeElement!.valueForKey("image_url") as! String
editorialElement.author = nodeElement!.valueForKey("author") as! String
editorialElement.issueNumber = nodeElement!.valueForKey("issue_int") as! String
editorialElement.volumeNumber = nodeElement!.valueForKey("volume_int") as! String
editorialElement.articleContent = nodeElement!.valueForKey("html_content") as! String
self.editorialObjects.addObject(editorialElement)
/*
nodeIDArray[nodeCounter] = jsonValue{nodeCounter}.string
let editorialInfos : EditorialElement = ((jsonValue as! NSDictionary).valueForKey("\(nodeIDArray[nodeCounter])") as! [NSDictionary]).map { EditorialElement(title: $0["title"] as! String, nodeID: $0["nid"] as! Int, timeStamp: $0["revision_timestamp"] as! Int, imageURL: $0["image_url"] as! String, author: $0["author"], issueNumber: $0["issue_int"] as! Int, volumeNumber: $0["volume_int"] as! Int, articleContent: $0["html_content"] as! String) /* I am going to try to break this line down to simplify it and fix the build errors */
*/
}
print(self.editorialObjects)
}
/* Sorting the elements in order of newest to oldest (as the array index increases) */
self.editorialObjects.sort({ $0.timeStamp > $1.timeStamp })
let lastItem = self.editorialObjects.count
let indexPaths = (lastItem..<self.editorialObjects.count).map { NSIndexPath(forItem: $0, inSection: $0) }
dispatch_async(dispatch_get_main_queue()) {
self.editorialsTableView.insertRowsAtIndexPaths(indexPaths, withRowAnimation: UITableViewRowAnimation.Automatic) // Animation implemented for testing, to be removed for version 1.0
}
self.currentPage++
/* } */
}
self.populatingEditorials = false
}
}
Here is the code for my Class and Router:
class EditorialElement: NSObject {
var title: String // title
var nodeID: Int // nid
var timeStamp: Int // revision_timestamp
var imageURL: String? // image_url
var author: String // author
var issueNumber: String // issue_int
var volumeNumber: String // volume_int
var articleContent: String // html_content
/* To get an NSDate objec from Unix timestamp
var date = NSDate(timeIntervalSince1970: timeStamp) */
init(title: String, nodeID: Int, timeStamp: Int, imageURL: String, author: String, issueNumber: String, volumeNumber: String, articleContent: String) {
self.title = title
self.nodeID = nodeID
self.timeStamp = timeStamp
self.imageURL = imageURL
self.author = author
self.issueNumber = issueNumber
self.volumeNumber = volumeNumber
self.articleContent = articleContent
}
override func isEqual(object: AnyObject!) -> Bool {
return (object as! EditorialElement).nodeID == self.nodeID
}
override var hash: Int {
return (self as EditorialElement).nodeID
}
}
enum Router : URLRequestConvertible {
static let baseURLString = MyGlobalVariables.baseURL
case Issue
case Editorials(Int)
case News(Int)
case Random(Int)
case Pictures(Int)
var URLRequest: NSMutableURLRequest {
let path : String
let parameters: [String: AnyObject]
(path) = {
switch self {
case .Issue:
return ("/issue")
case .Editorials (let editorialsSection): /* If section == 0, this will return the first ten editorials. If section == 1, then section * 10 = 10, and we will get the ten editorials after that. */
return ("/list/editorials/\(editorialsSection * 10)")
case .News (let newsSection):
return ("/list/news/\(newsSection * 10)")
case .Random (let randomSection):
return ("/list/random/\(randomSection * 10)")
case .Pictures (let page):
return ("/list/pictures/\(page)")
}
}()
let URL = NSURL(string: Router.baseURLString)
let GoldenWordsURLRequest = NSMutableURLRequest(URL: URL!.URLByAppendingPathComponent(path))
/* let encoding = Alamofire.ParameterEncoding.URL */
return GoldenWordsURLRequest
/* return encoding.encode(URLRequest, parameters: parameters).0 */
}
}
}
That's because you take an optional reference (editorialElement returned from a failable initializer) and call valueForKey("title") on it. It stumbles over the access to "title" because it goes first in you code while the target of the call is nil. I would recommend organizing your code as follows:
if let editorialElement = EditorialElement(title:..., nodeID: and such)
{
... assigning new values to the properties
}
and you will notice that you don't enter the if-let scope. You will avoid the crash and make sure the problem is inside the arguments you initialize the editorialElement with.

Resources