Getting a let value outside a function - swift

Hello I'm new at swift programming and i want to get label value from loadData() to use use for my path (reference) to my database on dbRef!.child(place+"/placeLabel"). What I mean?! I read data that are on "Dio Con Dio" node. That happens because the value place is let place = "Dio Con Dio". So, my application doesn't load data from "Paradosiako - Panorama" node. I want to load everything from database and i thought that if i could make place value to change to the next node, it could read all the data.
import UIKit
import FirebaseDatabase
class PlacesTableViewController: UITableViewController {
//MARK: Properties
#IBOutlet weak var placesTableView: UITableView!
//database reference
var dbRef:FIRDatabaseReference?
var places = [Places]()
private var loadedLabels = [String: String]()
private var loadedRatings = [String: Int]()
//handler
var handle:FIRDatabaseHandle?
override func viewDidLoad() {
super.viewDidLoad()
dbRef = FIRDatabase.database().reference()
// Loads data to cell.
loadData()
}
private func loadData() {
let place = "Dio Con Dio"
dbRef!.child(place+"/placeLabel").observe(.childAdded, with: {
(snapshot) in
let label = snapshot.value as! String
self.updatePlace(snapshot.key, label: label)
})
dbRef!.child(place+"/rating").observe(.childAdded, with: {
(snapshot) in
let rating = snapshot.value as! Int
self.updatePlace(snapshot.key, rating: rating)
})
}
private func updatePlace(_ key: String, label: String? = nil, rating: Int? = nil) {
if let label = label {
loadedLabels[key] = label
}
if let rating = rating {
loadedRatings[key] = rating
}
guard let label = loadedLabels[key], let rating = loadedRatings[key] else {
return
}
if let place = Places(name: label, rating: rating) {
places.append(place)
placesTableView.reloadData()
}
}
}
(This question is a follow up from this one.)

It's difficult to explain this in a comment, so I'll address your current issue here, but Paulo answered your question, I believe. You could even open a new question on this issue.
Here's the code you're having an issue with:
for (key, rating) in ratings.value as! [String: Int] {
self.updatePlace(key, rating: rating)
}
There's nothing wrong with this code, as long as ratings.value is filled with string: integer pairs. Your error occurs because somehow a there is a key missing. (the runtime found a NSNull, so it couldn't convert the data to a dictionary)
You need to set a breakpoint where you are constructing the array in:
placeSnapshot.childSnapshot(forPath: "rating")
(which is called ratings here/locally). If is ok to ignore entries with no key, then skip them and don't append them to your array. But it's more likely you have an error and need to find out why you're adding a null value...
Does that make more sense? Please accept Paulo's answer if he solved an issue, and open a new question with more information about childSnapshot (the code), and someone will be sure help you. (I know I'll try.)

For your new database schema, try this instead:
private func loadData() {
dbRef!.observe(.childAdded) {
(placeSnapshot) in
print("Adding place \(placeSnapshot.key)...")
// Load each label on the "placeLabel" list.
let labels = placeSnapshot.childSnapshot(forPath: "placeLabel")
for (key, label) in labels.value as! [String: String] {
self.updatePlace(key, label: label)
}
// Load each rating in the "rating" list.
let ratings = placeSnapshot.childSnapshot(forPath: "rating")
for (key, rating) in ratings.value as! [String: Int] {
self.updatePlace(key, rating: rating)
}
}
}
The updatePlace function simply groups the label and rating properties, of a given place, that were separately loaded by loadData.
Places removal. Of course, the above code won't handle place removal. To do so, you will need to listen for the childRemoved event as well.

Related

How to pass uiviewcontroller data to swiftui widget class

How to pass uiviewcontroller data to swiftui widget class i am unable to do it with app groups also write now i am sending it with userdefaults but unable to do it
if let userDefaults = UserDefaults(suiteName: "group.com.soup.ios.app") {
UserDefaults.standard.set(self.dat ?? "", forKey: "date")
UserDefaults.standard.set(self.textData.text ?? "", forKey: "text")
}
if let userDefaults = UserDefaults(suiteName: "group.com.soup.ios.app") {
let text = UserDefaults.standard.object(forKey: "text") as? String
Text(text ?? "Add Data").lineLimit(2)
.truncationMode(.middle)
}
I managed it to do it like this and it worked
extension UserDefaults {
static let group = UserDefaults(suiteName: "group.com.your.domain")!
}
setting data:
UserDefaults.group.set("objectToShare", forKey: "keyForTheObject")
getting data:
let object = UserDefaults.group.object(forKey: "keyForTheObject")
This solution is in swift 5.
In order to pass data between targets, you need to:
create your sharedDefaults structure
declare your global data variable
change the variable's data from your target\viewcontroller using support variables.
First of all create a new .swift file. Go on inspector>file>target membership and select the targets you want to comunicate.
SharedDefaults helper
//This goes in your new swift file
let sharedUserdefaults = UserDefaults(suiteName: SharedDefault.suitName)
struct SharedDefault {
static let suitName = "group.com.soup.ios.app"
struct Keys{
static let Key = "text"
}
}
Global data variable
//This goes in your new swift file
//This is an array of strings change the type by changing what's after "data:"
var data: [String] {
get {
return sharedUserdefaults?.stringArray(forKey: Key) as? [String] ?? [String]()
} set {
}
}
So what's above is a get/set variable which stores and receives the data you save/get from your widget and your parent app.
Actually saving/retrieving stuff
Now to use this stuff either in your app or target, you have to
Declare the support variables:
var myData: [String] = [] //This is not required but if you need to display contents in real time I'd suggest you use this.
var currentData = data //Helper variable to pass data to the sharedVariable
override func viewDidLoad() {
super.viewDidLoad()
myData.append(contentsOf: data)
}
Save stuff:
//Update both data and current data
myData.append("ok")
currentData.append("ok")
sharedUserdefaults?.set(currentData, forKey: SharedDefault.Keys.Key)
Delete stuff:
//Update both data and current data
mydata.remove(at: 0)
currentData.remove(at: 0)
sharedUserdefaults?.set(currentData, forKey: SharedDefault.Keys.Key)
That's it!

How to update a list of objects in Realm DB Swift

I'm trying to find the right way to update a list in a Realm object. For example:
class Person: Object {
#objc dynamic var id: String = ""
let children = List<Child>()
}
//Adding an item to the list
func add(child: Child, _ completion: (DBError?) -> Void) {
do {
let ctx = try Realm()
if let person = ctx.objects(Person.self).last {
try ctx.write({
person.children.append(child)
completion(nil)
})
} catch {
completion(DBError.unableToAccessDatabase)
}
}
This seems to work for adding an element. So how do I update an individual element in the array as well as replace the whole array to ensure it persists?

UISearchBar textDidChange data from plist

I'd like to search through items of my plist. The plist consists of an array of dictionaries. Each key/value represents Strings/Ints, etc but that isn't important.
As you'll see in the tableViewController class below, I've currently got an array that I have typed. I know I need to make an array of objects/items from my plist but I can't work out how to reference objects from the plist in the view controller.
View controller.swift file:
import UIKit
class TableViewController: UITableViewController, UISearchResultsUpdating {
var array = ["Example 1", "Example 2", "Example 3"]
var filteredArray = [String]()
var searchController = UISearchController()
var resultsController = UITableViewController()
override func viewDidLoad() {
super.viewDidLoad()
searchController = UISearchController(searchResultsController: resultsController)
tableView.tableHeaderView = searchController.searchBar
searchController.searchResultsUpdater = self
resultsController.tableView.delegate = self
resultsController.tableView.dataSource = self
}
//Added func to update search results
func updateSearchResults(for searchController: UISearchController) {
filteredArray = array.filter({ (array:String) -> Bool in
if array.contains(searchController.searchBar.text!) {
return true
} else {
return false
}
})
resultsController.tableView.reloadData()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
extension TableViewController {
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if tableView == resultsController.tableView {
return filteredArray.count
} else {
return array.count
}
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell()
if tableView == resultsController.tableView {
cell.textLabel?.text = filteredArray[indexPath.row]
} else {
cell.textLabel?.text = array[indexPath.row]
}
return cell
}
}
I've tried solving this by creating an object class from a tutorial on plists. It uses the example of a periodic table of elements:
import UIKit
struct Element {
enum State: String {
case Solid, Liquid, Gas
}
let atomicNumber: Int
let atomicWeight: Float
let discoveryYear: String
let group: Int
let name: String
let period: Int
let radioactive: Bool
let state: State
let symbol: String
// Position in the table
let horizPos: Int
let vertPos: Int
}
extension Element {
enum ErrorType: Error {
case noPlistFile
case cannotReadFile
}
/// Load all the elements from the plist file
static func loadFromPlist() throws -> [Element] {
// First we need to find the plist
guard let file = Bundle.main.path(forResource: "Element", ofType: "plist") else {
throw ErrorType.noPlistFile
}
// Then we read it as an array of dict
guard let array = NSArray(contentsOfFile: file) as? [[String: AnyObject]] else {
throw ErrorType.cannotReadFile
}
// Initialize the array
var elements: [Element] = []
// For each dictionary
for dict in array {
// We implement the element
let element = Element.from(dict: dict)
// And add it to the array
elements.append(element)
}
// Return all elements
return elements
}
/// Create an element corresponding to the given dict
static func from(dict: [String: AnyObject]) -> Element {
let atomicNumber = dict["atomicNumber"] as! Int
let atomicWeight = Float(dict["atomicWeight"] as! String) ?? 0
let discoveryYear = dict["discoveryYear"] as! String
let group = dict["group"] as! Int
let name = dict["name"] as! String
let period = dict["period"] as! Int
let radioactive = dict["radioactive"] as! String == "True"
let state = State(rawValue: dict["state"] as! String)!
let symbol = dict["symbol"] as! String
let horizPos = dict["horizPos"] as! Int
let vertPos = dict["vertPos"] as! Int
return Element(atomicNumber: atomicNumber,
atomicWeight: atomicWeight,
discoveryYear: discoveryYear,
group: group,
name: name,
period: period,
radioactive: radioactive,
state: state,
symbol: symbol,
horizPos: horizPos,
vertPos: vertPos)
}
}
And in the viewController class, instead of having
var array = ["Example 1", "Example 2", "Example 3"]
I've tried variations of
var array = Element["name"]
and
var array = elements.name
But they obviously don't work because the reference to the plist is in the object class.
If anyone has any idea on how to solve this using swift 3/xcode 8 I would be very appreciative!!
I hope your question is still relevant. As I understand, you can't filter your array, right? If so, I recommend you to take a look at this and this tutorials. Both of them are representing a little bit different approaches to load filtered array, but it doesn't matter much, they work.
P.S. I don't recommend you to make a special tableView for searching, if you want to customize it hereafter, because you will have to do it programmaticly later.
It will be more efficiant to do like this:
If searchController.isActive {
// do some stuff
} else { // another stuff }
But it is just my opinion. I hope my question will help ;).
Thanks for the links Oleg. They were really good!!
It wasn't so much the filtering I had a problem with but actually parsing objects from my plist into a tableview. It took me a few days but I found an answer in case other people were also having the same problem. Keep in mind I'm reasonably new to this and so it might not be the best/perfect way of doing it but it works.
In viewDidLoad, I made a reference to the plist using the following code:
let path = Bundle.main.path(forResource: "Elements", ofType: "plist")
let dictArray = NSArray(contentsOfFile: path!)
I'm not sure of the relevance but I'm pretty sure this next bit is needed if the plist ever needed to be updated. (Also in viewDidLoad)
for elementItem in dictArray! {
let newElement : Element = Element(state:((elementItem as AnyObject).object(forKey: "state")) as! String,
atomicNumber:((elementItem as AnyObject).object(forKey: "atomicNumber")) as! Int,
atomicWeight:((elementItem as AnyObject).object(forKey: "atomicWeight")) as! Float,
discoveryYear:((elementItem as AnyObject).object(forKey: "discoveryYear")) as! String
etc. for each of the keys in the dictionary in the same order as in the Element object in the question. Then (also in viewDidLoad):
elementsArray.append(newElement)
Then its pretty easy, in cellForRow I just have to make a new variable that refers back to the object and I can call up each/any of the associated dictionary keys. E.g.:
let element : Element
cell.textLabel!.text = element.name
cell.detailTextLabel?.text = element.symbol
return cell
Like I said, I know its not perfect but it worked for me. I've heard its not best practice to put too much information in viewDidLoad, so someone else might be able to confirm or provide a better answer.

Completionhandling in webrequest session

I´ve a webrequest with jsonserialization, after that, a for-in fetch process.
In whole this takes approximately 5-7 seconds.
After that i want to refersh my tableview in Viewcontroller.
The scheme of the function looks like this.
public struct Container {
let name: String
let symbol: String
let rank: String
}
public var dataArray = [Container]()
func fetchNewData() {
var view = ViewController()
// WebbRquest...
// Json serialization...
// the following list is much longer, will take a while...
for items in json {
let name = items["name"] as? AnyObject;
let symbol = items["symbol"] as? AnyObject;
let rank = items["rank"] as? AnyObject;
let result = Container(name: name! as! String, symbol: symbol! as! String,rank: rank! as! String)
dataArray.append(result)
}
// Now, after alle the work is done, i want to reload the tableview in Viewcontrller:
view.reload()
// Here i´m getting error, because nothing will be executed after return.
}
How can I call the reload function, after the webrequest process is finished? Because after the return, the function doesn´t execute anything anymore.
And no other function will "know" when the fetchNewData() function is finished.
Thanks for any help!
#IBAction func updateButton(_ sender: Any) {
fetchNewData()
}
According Phillipps suggestion, I had to modify the #IBAction func a little bit.
But now it´s working. Awesome!
Here the full working version:
public struct Container {
let name: String
let symbol: String
let rank: String
}
public var dataArray = [Container]()
func fetchNewData(completion:#escaping ([Container])->()) {
var view = ViewController()
// WebbRquest...
// Json serialization...
// the following list is much longer, will take a while...
for items in json {
let name = items["name"] as? AnyObject;
let symbol = items["symbol"] as? AnyObject;
let rank = items["rank"] as? AnyObject;
let result = Container(name: name! as! String, symbol: symbol! as! String,rank: rank! as! String)
dataArray.append(result)
}
completion(dataArray)
}
This is the actionFunc:
#IBAction func upDateButton(_ sender: Any) {
let data = dataArray
fetchNewData() {_ in (data)
DispatchQueue.main.async {
self.tableView.reloadData()
}
}
Here's a start. It will be vague because I'm making guesses about code I can't see, but you may be able to convert it to your own needs.
Change the fetch function so that it takes a closure as a parameter:
func fetchNewData(completion:([Container])->()) {
...note that the closure will accept the data array when it's called.
After you have your json all parsed, you then invoke the closure:
dataArray.append(result)
}
completion(dataArray)
The "magic" is in the view controller where you tell fetchNewData what to do when it's finished. Something like:
#IBAction func updateButton(_ sender: Any) {
fetchNewData() {(data)
// Save the data where the view controller can use it
self.tableArray = data
// Main queue for UI update
DispatchQueue.main.async {
self.tableView.reloadData()
}
}
}
Note that the closure is written in the view controller, so self is the view controller. This means no need to create a second (useless) controller inside the fetch.

Make a list of firebase users

I am trying to make a list of all my users from Firebase with their username.
My problem is that I am trying to make an array of all the users.
import UIKit
import Firebase
var memberRef = Firebase(url: "\(BASE_URL)/users")
//var currentUser = DataService.dataService.USER_REF.authData.uid
var currentUsers: [String] = [String]()
class dataTableView: UITableViewController
{
override func viewDidLoad()
{
loadUsers()
}
func loadUsers()
{
// Create a listener for the delta additions to animate new items as they're added
memberRef.observeEventType(.ChildAdded, withBlock: { (snap: FDataSnapshot!) in
print(currentUsers)
// Add the new user to the local array
currentUsers.append(snap.value as! String)
// Get the index of the current row
let row = currentUsers.count - 1
// Create an NSIndexPath for the row
let indexPath = NSIndexPath(forRow: row, inSection: 0)
// Insert the row for the table with an animation
self.tableView.insertRowsAtIndexPaths([indexPath], withRowAnimation: .Top)
})
}
But I get the error cant cast value of type NSDictionary to NSString.
The Firebase snapshot is a dictionary, not a string which is why you are receiving that error.
To access the elements of the snapshot:
if let name = snapshot.value["name"] as? String {
print(name)
}
you can also use
if let name = snapshot.value.objectForKey("name") as? String {