Unable to get text to appear in label. I can see the text in the console but I get an error that says Nil - swift3

I am passing data from one view controller to another. If I print the text to the console it will show. However when I try to add the text to my label on the view controller I get an error about Nil being found when unwrapping the optional.
Here is my code from the initial View Controller
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let selectedLady = DataService.instance.getLadies()[indexPath.row]
performSegue(withIdentifier: "LadyView", sender: selectedLady)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let ladyView = segue.destination as? LadyView {
assert(sender as? Lady != nil)
ladyView.initLadies(selectedLady: sender as! Lady)
//ladyView.selectedLadyName = selectedLady["name"]
}
}
And this is the code that is waiting in the second controller
func initLadies(selectedLady: Lady) {
print(selectedLady.name)
//This is the line that says it's Nil
LadyName.text = selectedLadyName
}
This is my lady struct
struct Lady {
private(set) public var name: String
private(set) public var imageName: String
private(set) public var subTitle: String
private(set) public var body: String
init(name: String, imageName: String, subTitle: String, body: String) {
self.name = name
self.imageName = imageName
self.subTitle = subTitle
self.body = body
}
}
Thank you for your help.
I have looked at the other answer in this post Passing data from table view to view controller and although it's similar his answer seems to be slightly different.

func initLadies(selectedLady: Lady) {
print(selectedLady.name)
LadyName.text = selectedLadyName.name
}
try this #CL Maciel

Related

Convert HTML text to displayable text in swiftUI [duplicate]

I was wondering how can HTML tags be stripped out of JSON from a web url. Do I have to use NSString of something similar.
So I am looking to strip out the html tags that are in the summary value. I looked around abit and it says NSString can be used but I was not sure if that was something that could be implemented into Swift 3. Any Help would be appreciated.
My code:
import UIKit
import Alamofire
struct postinput {
let mainImage : UIImage!
let name : String!
let author : String!
let summary : String!
}
class TableViewController: UITableViewController {
var postsinput = [postinput]()
var mainURL = "https://www.example.com/api"
typealias JSONstandard = [String : AnyObject]
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
callAlamo(url: mainURL)
}
func callAlamo(url : String){
Alamofire.request(url).responseJSON(completionHandler: {
response in
self.parseData(JSONData: response.data!)
})
}
func parseData(JSONData : Data) {
do {
var readableJSON = try JSONSerialization.jsonObject(with: JSONData, options: .mutableContainers) as! JSONstandard
// print(readableJSON)
if let posts = readableJSON["posts"] as? [JSONstandard] {
for post in posts {
let title = post["title"] as! String
let author = post["author"] as! String
guard let dic = post["summary"] as? [String: Any], let summary = dic["value"] as? String else {
return
}
print(author)
if let imageUrl = post["image"] as? String {
let mainImageURL = URL(string: imageUrl )
let mainImageData = NSData(contentsOf: mainImageURL!)
let mainImage = UIImage(data: mainImageData as! Data)
postsinput.append(postinput.init(mainImage: mainImage, name: title, author: author, summary: summary))
}
}
DispatchQueue.main.async {
self.tableView.reloadData()
}
}
}
catch {
print(error)
}
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return postsinput.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell")
// cell?.textLabel?.text = titles[indexPath.row]
let mainImageView = cell?.viewWithTag(2) as! UIImageView
mainImageView.image = postsinput[indexPath.row].mainImage
//(cell?.viewWithTag(2) as! UIImageView).image = postsinput[indexPath.row].mainImage
let mainLabel = cell?.viewWithTag(1) as! UILabel
mainLabel.text = postsinput[indexPath.row].name
mainLabel.font = UIFont(name: "Helvetica", size:14)
let autLabel = cell?.viewWithTag(3) as! UILabel
autLabel.text = postsinput[indexPath.row].author
autLabel.font = UIFont(name: "Helvetica", size:12)
let sumLabel = cell?.viewWithTag(4) as! UILabel
sumLabel.text = postsinput[indexPath.row].summary
sumLabel.font = UIFont(name: "Helvetica", size:12)
//(cell?.viewWithTag(3) as! UILabel).text = postsinput[indexPath.row].author
return cell!
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
You can use this code for stripping html tags
From your previous question
guard let dic = post["summary"] as? [String: Any], let summary = dic["value"] as? String else {
return
}
let str = summary.replacingOccurrences(of: "<[^>]+>", with: "", options: .regularExpression, range: nil)
print(str)
Edit
I have checked it and it is working
let summary = "<p>Latin text here</p>"
let str = summary.replacingOccurrences(of: "<[^>]+>", with: "", options: .regularExpression, range: nil)
print(str)
Latin text here

Expandable cell with firebase database?

I'm new to swift and I've been watching so many tutorials and looking at other peoples code to see how I can create a expandable cell in my table view controller. But, i've already created a firebase database in the same view controller. The problem is, is that I need the information from my database to be in the expandable view cell with the exception of one label. Does anybody know how i can do this? (It'll make a bit more sense when you take a look at the code below)
//
// Database.swift
// intern
//
// Created by Lani Daniels on 8/20/17.
// Copyright © 2017 Lani Daniels. All rights reserved.
//
import UIKit
import Firebase
import FirebaseDatabase
struct PostStruct {
let title: String
let message: String // How would I put this in the expandable cell?
let company: String // ...Also this in the expandable cell, but i assume it would be the same method as "let message"
}
class DatabaseViewController: UITableViewController {
var selectedCellIndexPath: NSIndexPath?
var posts: [PostStruct] = []
override func viewDidLoad() {
super.viewDidLoad()
//
let databaseRef = Database.database().reference()
databaseRef.child("Posts").queryOrderedByKey().observe(.childAdded, with: {
snapshot in
let snapshotValue = snapshot.value as? NSDictionary
let title = snapshotValue?["title"] as? String
let message = snapshotValue?["message"] as? String
let company = snapshotValue?["company"] as? String
self.posts.insert(PostStruct(title: title ?? "", message: message ?? "", company: company ?? ""), at: 0)
self.tableView.reloadData()
})
post()
}
func post(){
let title = "Title"
let message = "Message"
let company = "Company"
let post : [String : AnyObject] = ["title" : title as AnyObject, "message" : message as AnyObject, "company" : company as AnyObject]
let databaseRef = Database.database().reference()
databaseRef.child("Posts").childByAutoId().setValue(post)
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return posts.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
let label3 = cell.viewWithTag(3)as? UILabel; label3?.text=posts[indexPath.row].title
label3?.text=posts[indexPath.row].title
let textView2 = cell.viewWithTag(2) as? UITextView; textView2?.text=posts[indexPath.row].message
textView2?.text=posts[indexPath.row].message
let label4 = cell.viewWithTag(4)as? UILabel; label4?.text=posts[indexPath.row].company
label4?.text=posts[indexPath.row].company
return cell
}
#IBAction func appliedDataTapped(_ sender: Any) {
self.performSegue(withIdentifier: "segue", sender: self)
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
}
}

UserDefaults.standard not saving swift3 xcode 8

Hello everybody I'm trying to build a simple one view program to call and navigate a voice menu, I cannot get the UserDefaults to properly save between closing of the app. I've also had issue with getting text to display in the UITextField, I would like for it to display if saved on launch, here is a description of vars:
UserInput - text field for employee ID (what needs saving)
Switch - if Save it is 1 save data, if 0 do not
Submit - submit button that should save the data and call.
This is my first iPhone app and any help would be much appreciated!
enter code here
import UIKit
class ViewController: UIViewController, UITextFieldDelegate {
#IBOutlet weak var UserInput: UITextField!
var SaveIt = true
var employID = ""
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.UserInput.delegate = self;
UserInput.keyboardType = .numberPad
if let current_eid = UserDefaults.standard.string(forKey: "emp_ID") {
UserInput.text = current_eid
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func loadDefaults() {
let defaults = UserDefaults.standard
if let UserInput = defaults.string(forKey: "emp_ID") {
print(UserInput)
} else {
// focus on the text field if it's empty
UserInput.becomeFirstResponder() }
UserInput.text = defaults.object(forKey: "emp_ID") as! String?
}
func textFieldShouldReturn(textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true
}
#IBAction func Switch(_ sender: UISwitch) {
if (sender.isOn == true) {
SaveIt = true
}
else{
SaveIt = false
}
}
#IBAction func Submit(_ sender: Any) {
if (SaveIt) {
let defaults = UserDefaults.standard
defaults.set(UserInput.text, forKey: "emp_ID")
defaults.synchronize()
}
let phone = "telprompt://1888247889pppp" + UserInput.text! + "p1p1p1p1";
let url = URL(string:phone)!
if #available(iOS 10.0, *) {
UIApplication.shared.open(url, options: [:], completionHandler: nil)
} else { UIApplication.shared.openURL(url)
}
}
}

Getting Values from UISliders alongside UserID (linking User to section number?)

I'm working on creating a 'Rate' feature in my app where it pulls the users to rate from an external API. I then create a TableView which adds cells that contain a section name (the name of the User) and a UISlider.
What I'm trying to do is gather the data from the UISliders along with the User ID associated with the User who's section it is so that I can call a POST action to the API to send the rating data too.
Here's the code I have: (For the record, first time with Swift, the comments are me trying to figure out how to do this). Any ideas?
import UIKit
import Alamofire
class RateViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
#IBOutlet weak var submitButton: UIButton!
#IBOutlet weak var activeSurveyLabel: UILabel!
private var myTableView: UITableView!
var usersToRate = [User]()
var activeSurveyId: String?
var rateData = [String: Int]()
var sectionIdToUserId = [Int: String]()
var userIdSection = [Int: String]()
override func viewDidLoad() {
super.viewDidLoad()
submitButton.isHidden = true
submitButton.addTarget(self, action: #selector(pressSubmitButton(button:)), for: UIControlEvents.touchUpInside)
activeSurveyLabel.isHidden = false
self.loadUsersToRate()
Alamofire.request(RateRouter.getSurvey()).responseJSON { response in
debugPrint(response)
if let string = response.result.value as? [[String: Any]]{
if string.count == 0 {
return
}
self.submitButton.isHidden = false
self.activeSurveyLabel.isHidden = true
for survey in string {
if let survey = Survey(json: survey) {
self.activeSurveyId = survey._id!
print(survey._id!)
}
}
//if there's a result, show slider bars for each person on the team with rating scales (except for the person logged in)
//have a submit button with a post request for the votes
let barHeight: CGFloat = UIApplication.shared.statusBarFrame.size.height
let displayWidth: CGFloat = self.view.frame.width
let displayHeight: CGFloat = barHeight * CGFloat(self.usersToRate.count * 5)
self.myTableView = UITableView(frame: CGRect(x: 0, y: barHeight, width: displayWidth, height: displayHeight - barHeight))
self.myTableView.register(UITableViewCell.self, forCellReuseIdentifier: "MyCell")
self.myTableView.dataSource = self
self.myTableView.delegate = self
self.view.addSubview(self.myTableView)
} else {
print(response.result.error!)
let label = UILabel()
label.center = self.view.center
label.text = "No Active Surveys"
self.view.addSubview(label)
}
}
// Do any additional setup after loading the view.
}
func loadUsersToRate() {
Alamofire.request(RateRouter.getUsers()).responseJSON { response in
guard let jsonArray = response.result.value as? [[String: Any]] else {
print("didn't get array, yo")
return
}
for item in jsonArray {
if let user = User(json: item) {
self.usersToRate.append(user)
self.rateData.updateValue(5, forKey: user.userId!) //initialize user average data at 5 in case they don't change it
print (self.rateData)
}
}
}
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print("Num: \(indexPath.row)")
print("Value: \(usersToRate[indexPath.row])")
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
self.sectionIdToUserId.updateValue(usersToRate[section].userId!, forKey: section)
print(self.sectionIdToUserId)
return "\(usersToRate[section].name!)"
}
func numberOfSections(in tableView: UITableView) -> Int {
return usersToRate.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "MyCell", for: indexPath as IndexPath)
let slider = UISlider(frame: CGRect(x: 0, y:0, width: 400, height: 44))
slider.isUserInteractionEnabled = true
slider.minimumValue = 1
slider.maximumValue = 10
slider.value = 5
slider.tag = indexPath.section
slider.addTarget(self, action: #selector(sliderValueChange(sender:)), for: UIControlEvents.valueChanged)
cell.addSubview(slider)
return cell
}
func sliderValueChange(sender: UISlider) {
//get slider value
var currentValue = Int(sender.value)
print(currentValue)
//self.rateData.updateValue(currentValue, forKey: self.sectionIdToUserId.values[sender.])
//get user id for that value
//self.rateData.updateValue(currentValue, rateData[section])
// self.rateData.updateValue(currentValue, forKey: 0)
}
func pressSubmitButton(button: UIButton) {
for user in usersToRate {
// Alamofire.request(RateRouter.vote(voteFor: user.userId!, survey: activeSurveyId, rating:))
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
}
To answer my own question, what I did was create a different model called 'Votes' that stored the information for each vote.
Then to fill that array I created a tag for the slider in the tableView based on the section ID and used that to update the current value.
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "MyCell", for: indexPath as IndexPath)
let slider = UISlider(frame: CGRect(x: 0, y:0, width: cell.frame.width , height: 44))
slider.tag = indexPath.section
slider.addTarget(self, action: #selector(sliderValueChange(sender:)), for: UIControlEvents.valueChanged)
cell.addSubview(slider)
return cell
}
func sliderValueChange(sender: UISlider) {
//get slider value
var currentValue = Int(sender.value)
print(currentValue)
voteData[sender.tag].rating = currentValue
}

Swift 2.0 if statement in table view cell

I am trying to display the contents of an array in table view cells.
I created the arrays (I have one array of images and three arrays of strings).
I managed to display the contents of the arrays without any issues.
Here is my viewController.swift :
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
#IBOutlet weak var tableView: UITableView!
var shopOpen: Bool = false
var openSign: UIImage = UIImage(named: "open")!
var closedSign: UIImage = UIImage(named: "closed")!
var logos = [UIImage(named: "shop1"), UIImage(named: "shop2"), UIImage(named: "shop3")]
var programWorkingDays = ["Luni-Vineri:09:00-20:00", "Luni-Vineri::10:00-21:00", "Luni-Vineri:09:30-19:30"]
var programSambata = ["Sambata:10:00-16:00","Sambata:10:30-13:00" ,"Sambata: 09:00-13:00"]
var programDuminica = ["Duminica:10:00-15:00","Duminica:09:00-14:00","Duminica:10:30-15:00"]
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 3
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = self.tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! Cell
cell.logo.image = logos[indexPath.row]
cell.programWorkingDays.text = programWorkingDays[indexPath.row]
cell.programSambata.text = programSambata[indexPath.row]
cell.programDuminica.text = programDuminica[indexPath.row]
return cell
}
}
My next goal is to display one image or another based on a true/false value of a variable (shopOpen)
If the variable is true i want to have picture 1, if it's false i want to have picture 2.
The image view is placed in the main.storyboard and the connection is made in the cell class.
The images will be storet in UIImage variables (given the fact that i only have two images for this part, i don't think it makes any sense to put them inside an array)
Where should i write the if statement and how do i refresh the cells to display the correct image ?
First of all, rather than multiple arrays for the parameters use a struct including an boolean open property
struct Shop {
var workingDays : String
var sambata : String
var duminica : String
var logo : UIImage?
var open = false
}
In the ViewController class declare a variable shops as an empty Shop array
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
#IBOutlet weak var tableView: UITableView!
let openSign: UIImage = UIImage(named: "open")!
let closedSign: UIImage = UIImage(named: "closed")!
var shops = [Shop]()
In viewDidLoad populate the shops array with appropriate Shop instances
override func viewDidLoad() {
super.viewDidLoad()
shops.append(Shop(workingDays: "09:00-20:00", sambata: "10:00-16:00", duminica: "10:00-15:00", logo: UIImage(named: "shop1"), open: true))
shops.append(Shop(workingDays: "10:00-21:00", sambata: "10:30-13:00", duminica: "09:00-14:00", logo: UIImage(named: "shop2"), open: false))
shops.append(Shop(workingDays: "09:30-19:30", sambata: "09:00-13:00", duminica: "10:30-15:00", logo: UIImage(named: "shop3"), open: true))
}
In numberOfRowsInSection return the number of shops rather than a hard-coded integer
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return shops.count
}
In cellForRowAtIndexPath display the values from the Shop instances. Assuming there is an image view openImage the open or close image is displayed depending on the state of the open property.
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = self.tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! Cell
let shop = shops[indexPath.row]
cell.logo.image = shop.logo
cell.programWorkingDays.text = "Luni-Vineri: \(shop.workingDays)"
cell.programSambata.text = "Sambata: \(shop.sambata)"
cell.programDuminica.text = "Duminica: \(shop.duminica)"
cell.openImage.image = shop.open ? openSign : closedSign
return cell
}
}