How to use a variable outside a class - swift

I want to use variables (depAirportLat and depAirportLong) outside ViewController class in order to display coordinates on a map but it doesn't works, could you help me ? This is my program :
import UIKit
import MapKit
class ViewController: UIViewController, UITextFieldDelegate {
#IBOutlet weak var resultWeather: UIImageView!
#IBOutlet weak var arrairport: UITextField!
#IBOutlet weak var depairport: UITextField!
#IBOutlet weak var resultAirport: UILabel!
#IBOutlet weak var fontAirports: UIImageView!
#IBOutlet weak var resultTemp: UILabel!
#IBOutlet weak var resultTempfar: UILabel!
#IBOutlet weak var resultHum: UILabel!
#IBOutlet weak var resultCeiling: UILabel!
#IBOutlet weak var resultClouds: UILabel!
#IBOutlet weak var resultWind: UILabel!
#IBOutlet weak var resultPressure: UILabel!
#IBOutlet weak var resultMetar: UILabel!
#IBOutlet weak var resultTaf: UILabel!
#IBOutlet weak var arrMap: MKMapView!
#IBOutlet weak var depMap: MKMapView!
#IBAction func getWeather(_ sender: UIButton) {
sender.pulsate()
if let url = URL(string: "http://fr.allmetsat.com/metar-taf/france.php?icao=" + depairport.text!.replacingOccurrences(of: " ", with: "-"))
{ let request = NSMutableURLRequest(url: url)
let task = URLSession.shared.dataTask(with: request as URLRequest) { data, response, error in
var message = ""
if error != nil { print(error!) } else { if let unwrappedData = data {
let dataString = NSString(data: unwrappedData, encoding: String.Encoding.utf8.rawValue); var stringSeparator = "METAR:</b> "
if let contentArray = dataString?.components(separatedBy: stringSeparator) {
if contentArray.count > 1 { stringSeparator = "</p></div><div class=\"c1\""
let newContentArray = contentArray[1].components(separatedBy: stringSeparator); if newContentArray.count > 1 { message = newContentArray[0]
print(message)
if (dataString?.contains("CAVOK"))!{
DispatchQueue.main.async { self.resultWeather.image = UIImage(named: "sun.png") }}
else if (dataString?.contains("SKC"))!{
DispatchQueue.main.async { self.resultWeather.image = UIImage(named: "sun.png")}
}
else if (dataString?.contains("SCT"))!{
DispatchQueue.main.async { self.resultWeather.image = UIImage(named: "scattered.png")} }
else if (dataString?.contains("OVC"))!{
DispatchQueue.main.async { self.resultWeather.image = UIImage(named: "overcast.png")} }
else if (dataString?.contains("BKN"))!{
DispatchQueue.main.async { self.resultWeather.image = UIImage(named: "broken.png")} }
else if (dataString?.contains("FEW"))!{
DispatchQueue.main.async { self.resultWeather.image = UIImage(named: "few.png")} }
else if (dataString?.contains("NSC"))!{
DispatchQueue.main.async { self.resultWeather.image = UIImage(named: "sun.png")} }
else if (dataString?.contains("RAIN"))!{
DispatchQueue.main.async { self.resultWeather.image = UIImage(named: "rain.png")}
}}}}}}
}
task.resume();
if let url = URL(string: "http://fr.allmetsat.com/metar-taf/france.php?icao=" + self.depairport.text!.replacingOccurrences(of: " ", with: "-"))
{ let requesta = NSMutableURLRequest(url: url)
let taska = URLSession.shared.dataTask(with: requesta as URLRequest) { data, response, error in
var messagea = ""
if error != nil { print(error!) } else { if let unwrappedData = data {
let dataStringa = NSString(data: unwrappedData, encoding: String.Encoding.utf8.rawValue); var stringSeparatora = "<div class=\"c1b\""
if let contentArray = dataStringa?.components(separatedBy: stringSeparatora) { if contentArray.count > 1 { stringSeparatora = "</h1><p>"
let newContentArray = contentArray[1].components(separatedBy: stringSeparatora); if newContentArray.count > 1 { messagea = newContentArray[0]
print(messagea) }}}};
}
if messagea == "" { messagea = "No data..." }
DispatchQueue.main.sync(execute: { self.resultAirport.text = self.depairport.text! })}; taska.resume() } else { self.resultAirport.text = "0" };
//latitude variable (depAirportLat)
if let urlcoord = URL(string:"https://flyawaysimulation.com/airports/" + self.depairport.text!.replacingOccurrences(of: " ", with: "-"))
{ let requestad = NSMutableURLRequest(url: urlcoord)
let taskad = URLSession.shared.dataTask(with: requestad as URLRequest) { data, response, error in
var depAirportLat = ""
if error != nil { print(error!) } else { if let unwrappedData = data {
let dataStringad = NSString(data: unwrappedData, encoding: String.Encoding.utf8.rawValue); var stringSeparatorad = "<dt>Latitude</dt>"
if let contentArray = dataStringad?.components(separatedBy: stringSeparatorad) { if contentArray.count > 1 { stringSeparatorad = "</dd>"
let newContentArray = contentArray[1].components(separatedBy: stringSeparatorad); if newContentArray.count > 1 { depAirportLat = newContentArray[0]
print(depAirportLat) }}}}}};
taskad.resume()
}
}
override func viewDidLoad() {
super.viewDidLoad()
//Do any additional setup after loading the view, typically from a nib.
class gloVarDep {
var depAirportLatReal = depAirportLat.dropFirst(5)
var depAirportLatRealCoord = String(depAirportLatReal)
var depAirportLatRealCoordFloat = (depAirportLatRealCoord as NSString).floatValue

If you want to be able to use those variables outside the class, then you have to declare them as global variables. Just declare them outside of the class scope and you should be fine.
Alternatively, you could create a structure and define static variables that can be used everywhere. The aforementioned makes things a bit more readable and retainable.

Related

How to save user preference of dark or light mode

I have these buttons in my app so user can switch from light to dark mode in the app, how can I save this preference so whichever mode the user selected will still be active the next time they open the app?
I'm on Xcode 14.2
Code example please.
#IBAction func darkButton(_ sender: Any) {
overrideUserInterfaceStyle = .dark
}
#IBAction func lightButton(_ sender: Any) {
overrideUserInterfaceStyle = .light
}
Full code below
import UIKit
class ViewController: UIViewController {
// All Input Texts
#IBOutlet weak var machineODInputText: UITextField!
#IBOutlet weak var pipeODInputText: UITextField!
#IBOutlet weak var pipeLengthInputText: UITextField!
#IBOutlet weak var driveLengthInputText: UITextField!
#IBOutlet weak var muckUpInputText: UITextField!
#IBOutlet weak var jackingSpeedInputText: UITextField!
#IBOutlet weak var weightOfBenoBagInputText: UITextField!
#IBOutlet weak var noOfBenoBagsInputText: UITextField!
#IBOutlet weak var benoQtyForTanksInputText: UITextField!
#IBOutlet weak var noOfBenoBagsPerPalletInputText: UITextField!
// All Result Texts
#IBOutlet weak var lubricationPumpSpeedResult: UILabel!
#IBOutlet weak var volumePerMeterResult: UILabel!
#IBOutlet weak var volumePerPipeResult: UILabel!
#IBOutlet weak var volumeForDriveResult: UILabel!
#IBOutlet weak var benoQtyForLubricationResult: UILabel!
#IBOutlet weak var benoQtyForDriveResult: UILabel!
#IBOutlet weak var noOfPalletsForDriveResult: UILabel!
// All Buttons
#IBAction func lightButton(_ sender: Any) {
overrideUserInterfaceStyle = .light
UserDefaults.standard.set("light", forKey: "mode")
}
#IBAction func darkButton(_ sender: Any) {
overrideUserInterfaceStyle = .dark
UserDefaults.standard.set("dark", forKey: "mode")
}
#IBAction func calculateButton(_ sender: Any) {
let formatter = NumberFormatter()
formatter.numberStyle = .decimal
formatter.maximumFractionDigits = 0
let formatter2 = NumberFormatter()
formatter2.numberStyle = .decimal
formatter2.maximumFractionDigits = 2
let pi = Double.pi / 4
let TBMOD = (Double(machineODInputText.text!) ?? 1) / 1000.0
let pipeOD = (Double(pipeODInputText.text!) ?? 1) / 1000.0
let muckup = (Double(muckUpInputText.text!) ?? 2.5)
let advanceSpeed = (Double(jackingSpeedInputText.text!) ?? 1)
let volPerPipe = (Double(pipeLengthInputText.text!) ?? 1)
let volForDrive = (Double(driveLengthInputText.text!) ?? 1)
let noOfBenoBagsForLub = (Double(noOfBenoBagsInputText.text!) ?? 1)
let weightOfEachBenoBag = (Double(weightOfBenoBagInputText.text!) ?? 1)
let amountOfBenoBagsInTamks = (Double(benoQtyForTanksInputText.text!) ?? 1)
let noOfBenoBagsPerPallet = (Double(noOfBenoBagsPerPalletInputText.text!) ?? 1)
// Calculate Volume per Meter (Ltr per meter)
let volPerMtrResults1 = ((pi * (TBMOD) * (TBMOD)) - (pi * (pipeOD) * (pipeOD))) * muckup * 1000
let volPerMtrResult = formatter.string (from: NSNumber(value: volPerMtrResults1))
volumePerMeterResult.text = volPerMtrResult
// Calculate Lubrication Pump Capacity (Ltr per min)
let pumpCapacityResults1 = (advanceSpeed / 1000) * volPerMtrResults1
let lubPerMtrResult = formatter.string(from: NSNumber(value: pumpCapacityResults1))
lubricationPumpSpeedResult.text = lubPerMtrResult
// Calculate Volume per pipe (M³ per pipe)
let volPerPipeResults1 = (volPerMtrResults1 / 1000) * volPerPipe
let volPerPipeResult = formatter2.string(from: NSNumber(value: volPerPipeResults1))
volumePerPipeResult.text = volPerPipeResult
// Calculate Volume for drive (M³ for drive)
let volPerDriveResults1 = (volPerMtrResults1 / 1000) * volForDrive
let volPerDriveResult = formatter.string(from: NSNumber(value: volPerDriveResults1))
volumeForDriveResult.text = volPerDriveResult
// Calculate Amount of beno for lubrication
let ammountOfBenoForLubResults1 = noOfBenoBagsForLub * volPerDriveResults1 * weightOfEachBenoBag
let ammountOfBenoForLubResult = formatter.string(from: NSNumber(value: ammountOfBenoForLubResults1))
benoQtyForLubricationResult.text = ammountOfBenoForLubResult
// Calculate beno quantity for drive
let amountOfBenoForDriveResults1 = ammountOfBenoForLubResults1 + (amountOfBenoBagsInTamks * 1000)
let amountOfBenoForDriveResult = formatter.string(from: NSNumber(value: amountOfBenoForDriveResults1))
benoQtyForDriveResult.text = amountOfBenoForDriveResult
// Calculate number of pallets for drive
let ammountOfBenoPerPalletResults1 = amountOfBenoForDriveResults1 / weightOfEachBenoBag / noOfBenoBagsPerPallet
let ammountOfBenoPerPalletResult = formatter2.string(from: NSNumber(value: ammountOfBenoPerPalletResults1))
noOfPalletsForDriveResult.text = ammountOfBenoPerPalletResult
UserDefaults.standard.set(machineODInputText.text, forKey: "TBMOD")
UserDefaults.standard.set(pipeODInputText.text, forKey: "pipeOD")
UserDefaults.standard.set(muckUpInputText.text, forKey: "muckUp")
UserDefaults.standard.set(jackingSpeedInputText.text, forKey: "speed")
UserDefaults.standard.set(pipeLengthInputText.text, forKey: "pipeLength")
UserDefaults.standard.set(driveLengthInputText.text, forKey: "driveLength")
UserDefaults.standard.set(noOfBenoBagsInputText.text, forKey: "noOfBenoBags")
UserDefaults.standard.set(weightOfBenoBagInputText.text, forKey: "weightOfBenoBag")
UserDefaults.standard.set(benoQtyForTanksInputText.text, forKey: "benoQtyForTanks")
UserDefaults.standard.set(noOfBenoBagsPerPalletInputText.text, forKey: "benoBagsPerPallet")
}
// Adding toolbar to top of keyboard with "Done" button (toolbar.swift file)
override func viewDidLoad() {
super.viewDidLoad()
machineODInputText.inputAccessoryView = toolBar()
pipeODInputText.inputAccessoryView = toolBar()
pipeLengthInputText.inputAccessoryView = toolBar()
driveLengthInputText.inputAccessoryView = toolBar()
muckUpInputText.inputAccessoryView = toolBar()
jackingSpeedInputText.inputAccessoryView = toolBar()
weightOfBenoBagInputText.inputAccessoryView = toolBar()
noOfBenoBagsInputText.inputAccessoryView = toolBar()
benoQtyForTanksInputText.inputAccessoryView = toolBar()
noOfBenoBagsPerPalletInputText.inputAccessoryView = toolBar()
}
// load old data into input feilds
override func viewDidAppear(_ animated: Bool) {
if let a = UserDefaults.standard.object(forKey: "TBMOD") as? String {
machineODInputText.text = a
}
if let a = UserDefaults.standard.object(forKey: "pipeOD") as? String {
pipeODInputText.text = a
}
if let a = UserDefaults.standard.object(forKey: "muckUp") as? String {
muckUpInputText.text = a
}
if let a = UserDefaults.standard.object(forKey: "speed") as? String {
jackingSpeedInputText.text = a
}
if let a = UserDefaults.standard.object(forKey: "pipeLength") as? String {
pipeLengthInputText.text = a
}
if let a = UserDefaults.standard.object(forKey: "driveLength") as? String {
driveLengthInputText.text = a
}
if let a = UserDefaults.standard.object(forKey: "noOfBenoBags") as? String {
noOfBenoBagsInputText.text = a
}
if let a = UserDefaults.standard.object(forKey: "weightOfBenoBag") as? String {
weightOfBenoBagInputText.text = a
}
if let a = UserDefaults.standard.object(forKey: "benoQtyForTanks") as? String {
benoQtyForTanksInputText.text = a
}
if let a = UserDefaults.standard.object(forKey: "benoBagsPerPallet") as? String {
noOfBenoBagsPerPalletInputText.text = a
}
// load dark/light mode last selected by user
let mode = UserDefaults.standard.string(forKey: "mode")
if mode == "dark" {
overrideUserInterfaceStyle = .dark
} else {
overrideUserInterfaceStyle = .light
}
machineODInputText.clearsOnBeginEditing = true
pipeODInputText.clearsOnBeginEditing = true
pipeLengthInputText.clearsOnBeginEditing = true
driveLengthInputText.clearsOnBeginEditing = true
muckUpInputText.clearsOnBeginEditing = true
jackingSpeedInputText.clearsOnBeginEditing = true
weightOfBenoBagInputText.clearsOnBeginEditing = true
noOfBenoBagsInputText.clearsOnBeginEditing = true
benoQtyForTanksInputText.clearsOnBeginEditing = true
noOfBenoBagsPerPalletInputText.clearsOnBeginEditing = true
}
}
you can save it in UserDefaults as follows.
#IBAction func darkButton(_ sender: Any) {
overrideUserInterfaceStyle = .dark
UserDefaults.standard.set("dark", forKey: "mode")
}
#IBAction func lightButton(_ sender: Any) {
overrideUserInterfaceStyle = .light
UserDefaults.standard.set("light", forKey: "mode")
}
When reopening the application do as follow.
let mode = UserDefaults.standard.string(forKey: "mode")
if mode == "dark" {
overrideUserInterfaceStyle = .dark
} else {
overrideUserInterfaceStyle = .light
}
Where you need to add the above code depends on your scenario. I suggest to add it inside AppDelegate -> didFinishLaunchingWithOptions.

How can I reuse a variable later on in Swift

I'm trying to capture a user input (textfield + button) and reuse the result later in the program but I don't know how to do that.
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var resultLabel: UILabel!
#IBOutlet weak var moneyTextField: UITextField!
#IBAction func convert(_ sender: Any) {
let convertion:Double = Double(moneyTextField.text!)!
print(convertion)
}
override func viewDidLoad() {
super.viewDidLoad()
let url = URL(string: "https://www.x-rates.com/calculator/?from=EUR&to=USD&amount=1")!
let request = NSMutableURLRequest(url : url)
let task = URLSession.shared.dataTask(with: request as URLRequest) {
data, response, error in
var rateValue:Double = 0.0;
if let error = error {
print(error)
} else {
if let unwrappedData = data {
let dataString = NSString(data: unwrappedData, encoding: String.Encoding.utf8.rawValue)
var stringSeperator = "<span class=\"ccOutputRslt\">"
if let contentArray = dataString?.components(separatedBy: stringSeperator){
if contentArray.count > 0 {
stringSeperator = "<span"
let newContentArray = contentArray[1].components(separatedBy: stringSeperator)
if newContentArray.count > 0 {
rateValue = Double(newContentArray[0])!
print(newContentArray[0])
}
}
}
}
}
//
print("Rate is \(rateValue)");
DispatchQueue.main.sync(execute: {
self.resultLabel.text = "the value of the dollar is " + String(rateValue)
}
)}
task.resume()
}
}
What I want to do is take the let convertion and multiply it by rateValue at the end of the code. I tried different thing but without any results.
after the advice from Joakim Danielson
I did that :
import UIKit
class ViewController: UIViewController {
var fxRate: Double?
#IBOutlet weak var resultLabel: UILabel!
#IBOutlet weak var moneyTextField: UITextField!
#IBAction func convert(_ sender: Any) {
let convertion:Double = Double(moneyTextField.text!)!
print(convertion)
var convertedAmount = 0.0
if let rate = fxRate, let money = Double(moneyTextField.text) {
convertedAmount = rate * money
}
print(convertedAmount)
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let url = URL(string: "https://www.x-rates.com/calculator/?from=EUR&to=USD&amount=1")!
let request = NSMutableURLRequest(url : url)
let task = URLSession.shared.dataTask(with: request as URLRequest) {
data, response, error in
var rateValue:Double = 0.0;
if let error = error {
print(error)
} else {
if let unwrappedData = data {
let dataString = NSString(data: unwrappedData, encoding: String.Encoding.utf8.rawValue)
var stringSeperator = "<span class=\"ccOutputRslt\">"
if let contentArray = dataString?.components(separatedBy: stringSeperator){
if contentArray.count > 0 {
stringSeperator = "<span"
let newContentArray = contentArray[1].components(separatedBy: stringSeperator)
if newContentArray.count > 0 {
rateValue = Double(newContentArray[0])!
print(newContentArray[0])
rateValue = Double(newContentArray[0])!
self.fxRate = rateValue
}
}
}
}
}
//
print("Rate is \(rateValue)");
DispatchQueue.main.sync(execute: {
self.resultLabel.text = "the value of the dollar is " + String(rateValue)
}
)}
task.resume()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
but I have the error : Cannot invoke initializer for type 'Double' with an argument list of type '(String?)' on line 26. Can you please help me? thx!
create a variable outside of your function
var anyVariableYouWantToAccessLater: Double?
And use this variable anywhere you want.
Since you're downloading the rate during viewDidLoad I am assuming this is what you want to keep.
Add a new property to the class
class ViewController: UIViewController {
var fxRate: Double?
...
In viewDidLoad update this property with the downloaded value
rateValue = Double(newContentArray[0])!
fxRate = rateValue
In the convert func (or wherever you want to use the rate)
#IBAction func convert(_ sender: Any) {
var convertedAmount = 0.0
if let rate = fxRate, let money = Double(moneyTextField.text ?? "0") {
convertedAmount = rate * money
}
print(convertedAmount)
}

how do i fix this error- Cannot convert value of type 'UIImageView' to expected argument type 'UIImage' in the following code

import UIKit
class UserVC: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
#IBOutlet weak var userImagePicker: UIImageView!
#IBOutlet weak var usernameField: UITextField!
#IBOutlet weak var completeSignInBtn: UIButton!
var userUid: String!
var emailField: String!
var passwordField: String!
var imagePicker: UIImagePickerController!
var imageSelected = false
var username: String!
func uploadImg() {
if usernameField.text == nil {
print("Must have username")
completeSignInBtn.isEnabled = false
}else {
username = usernameField.text
completeSignInBtn.isEnabled = true
}
guard let img = userImagePicker, imageSelected == true else {
print("image must be selected")
return
}
// below is where the error is can you help me?
if let imgData = UIImageJPEGRepresentation(img, 0.2) {
let imgUid = NSUUID().uuidString
let metadata = StorageMetadata()
metadata.contentType = "img/jpeg"
Storage.storage().reference().child(imgUid).put(imgData, metadata: metadata) {
(Metadata, error) in
if error != nil {
print("did not upload image")
}else {
print("uploaded")
//
}
}
}
}
}
I have no Idea on how to fix this error. If there is anyone out there that can help, I would greatly appreciate it.
As I see from the outlet userImagePicker is a UIImageView not UIImage
so to fix the issue I would suggest taking the image from the view by:
changing this line
guard let img = userImagePicker, imageSelected == true else {
to this
guard let img = userImagePicker.image, imageSelected == true else {
assuming that the image is loaded to view after picking it from UIImagePickerController

I'm getting the error "unrecognized selector sent to instance"

When I try to press on any button on my calculator it gives me the error unrecognized selector sent to instance 0x7f7f85e04e40 I updated this code from Swift 2 to Swift 4 but back then it used to work. What's wrong with the code?
import UIKit
class Valuta: UIViewController {
#IBOutlet weak var euro: UILabel!
#IBOutlet weak var zloty: UILabel!
#IBOutlet weak var topButton: UIButton!
#IBOutlet weak var bottomButton: UIButton!
var valutas : [ValutaWaarde] = []
var number = 0;
var isTypingNumber = false
var currentNumber = 0;
var currency = "PLN";
#IBAction func numberTapped(sender: AnyObject) {
let number = sender.currentTitle!
var disable = false
if number == "."{
if euro.text!.range(of: ".") != nil{
disable = true
}
}
if(disable == false){
if isTypingNumber {
euro.text = euro.text!+number!
} else {
euro.text = number
isTypingNumber = true
}
var output : Float;
if(currency == "EUR"){
output = (euro.text! as NSString).floatValue*Float((valutas[0].rates?.pLN!)!)
}
else{
output = (euro.text! as NSString).floatValue/Float((valutas[0].rates?.pLN!)!)
}
zloty.text = String(format: "%.2f", output)
}
}
#IBAction func resetCalculator(sender: AnyObject) {
isTypingNumber = false;
zloty.text = "0";
euro.text = "0";
}
override func viewDidLoad() {
super.viewDidLoad()
UINavigationBar.appearance().isTranslucent = false;
UINavigationBar.appearance().barTintColor = UIColor(red: CGFloat(51/255.0), green: 51/255, blue: 51/255, alpha: 1)
UINavigationBar.appearance().titleTextAttributes = [NSAttributedStringKey.foregroundColor:UIColor.white]
//Get list
let url = URL(string: "https://api.fixer.io/latest?symbols=PLN&base=EUR")
var request = NSMutableURLRequest(url: url! as URL, cachePolicy: NSURLRequest.CachePolicy.returnCacheDataElseLoad, timeoutInterval: Double.infinity)
if Reachability.isConnectedToNetwork(){
request = NSMutableURLRequest(url: url! as URL, cachePolicy: NSURLRequest.CachePolicy.useProtocolCachePolicy, timeoutInterval: Double.infinity);
}
let session = URLSession.shared
let task = session.dataTask(with: request as URLRequest,
completionHandler: { data, response, error -> Void in
do {
if let json = try JSONSerialization.jsonObject(with: data!) as? [[String: String]] {
for valutaData in json {
let valuta = ValutaWaarde(dictionary: valutaData as NSDictionary)
self.valutas.append(valuta!)
}
}
} catch { print(error) }
})
task.resume()
}
...
}
Try to re-connect #IBOutlets and #IBActions in storyboard.

Retrieving image from firebase? (swift)

So i have a firebase structure like the pic below
Now i want to retrieve that image file that i've uploaded. to decode the base64String and show it. Every user can make a post and the information that will be sended to firebase has a description etc. and also have an image. now i tried to retrieve it whit this codes but nothing did work.
var REF_LIST = Firebase(url: "\(URL_BASE)/listItems")
REF_LIST.observeEventType(FEventType.Value, withBlock: { snapshot in
let image = snapshot.value.objectForKey("images") as! String
but this already gave me a nil error on that line, so i couldn't even decode. i think i understand why it's giving me a nil error, there is no images in listItems on firebase, you first have the unique ID and then the specs with images in. now i don't now how i can retrieve that information from that unique ID?
UPDATE:
the tableViewController what will receive the data from firebase:
import UIKit
import FBSDKLoginKit
import Alamofire
import Firebase
class ListVC: UIViewController, UITableViewDataSource, UITableViewDelegate {
#IBOutlet weak var tableView: UITableView!
var lists = [List]()
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
dispatch_async(backgroundQueue, {
self.initObservers()
})
}
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
self.tableView.reloadData()
}
func initObservers() {
LoadingOverlay.shared.showOverlay(self.view)
DataService.ds.REF_LISTS.observeEventType(.Value, withBlock: { snapshot in
print(snapshot.value)
self.lists = []
if let snapshots = snapshot.children.allObjects as? [FDataSnapshot] {
for snap in snapshots {
print("SNAP: \(snap)")
if let listDict = snap.value as? Dictionary<String, AnyObject> {
let key = snap.key
let list = List(listKey: key, dictionary: listDict)
self.lists.insert(list, atIndex:0)
}
}
}
self.tableView.reloadData()
LoadingOverlay.shared.hideOverlayView()
})
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return lists.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
if let cell = tableView.dequeueReusableCellWithIdentifier("ListCell") as? ListCell {
let list = self.lists[indexPath.row]
cell.request?.cancel()
cell.configureCell(list)
return cell
} else {
return ListCell()
}
}
}
the addController which post the data to firebase:
import UIKit
import Firebase
import Alamofire
import FBSDKCoreKit
class AddVC: UIViewController, UITextFieldDelegate, UITextViewDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
#IBOutlet weak var addTitle: UITextField!
#IBOutlet weak var addDescription: UITextView!
#IBOutlet weak var addLocation: UITextField!
#IBOutlet weak var placeholderLbl: UILabel!
#IBOutlet weak var freeSwitch: UISwitch!
#IBOutlet weak var tradeSwitch: UISwitch!
#IBOutlet weak var imageSelectorImg: UIImageView!
#IBOutlet weak var overlayView: UIView!
var currentUsername = ""
var imageSelected = false
var imagePicker: UIImagePickerController!
var base64String: NSString = ""
override func viewDidLoad() {
super.viewDidLoad()
addTitle.delegate = self
addDescription.delegate = self
addLocation.delegate = self
imagePicker = UIImagePickerController()
imagePicker.delegate = self
getCurrentUser()
hideKeyboardWhenTappedAround()
}
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
addTitle.text = ""
addDescription.text = ""
addLocation.text = ""
freeSwitch.setOn(false, animated: false)
tradeSwitch.setOn(false, animated: false)
placeholderLbl.hidden = false
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func getCurrentUser() {
DataService.ds.REF_USER_CURRENT.observeEventType(FEventType.Value, withBlock: { snapshot in
let currentUser = snapshot.value.objectForKey("username") as! String
print("Username: \(currentUser)")
self.currentUsername = currentUser }, withCancelBlock: { error in
print(error.description)
})
}
func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage, editingInfo: [String : AnyObject]?) {
imagePicker.dismissViewControllerAnimated(true, completion: nil)
imageSelectorImg.image = image
dispatch_async(backgroundQueue, {
let uploadImage = image
let imageData = UIImageJPEGRepresentation(uploadImage, 0.5)
self.base64String = imageData!.base64EncodedStringWithOptions(NSDataBase64EncodingOptions.Encoding64CharacterLineLength)
})
imageSelected = true
}
#IBAction func selectImage(sender: UITapGestureRecognizer) {
presentViewController(imagePicker, animated: true, completion: nil)
}
func postToFirebase() {
// LoadingOverlay.shared.showOverlay(self.overlayView)
var post: Dictionary<String, AnyObject> = ["username": self.currentUsername, "description": self.addDescription.text!, "title": self.addTitle.text!, "location": self.addLocation.text!, "images": self.base64String]
if self.freeSwitch.on && self.tradeSwitch.on {
post["tradeOption"] = "Gratis/Te ruil"
} else if self.freeSwitch.on {
post["tradeOption"] = "Gratis"
} else if self.tradeSwitch.on {
post["tradeOption"] = "Te ruil"
}
let firebasePost = DataService.ds.REF_LISTS.childByAutoId()
firebasePost.setValue(post)
}
#IBAction func postListItem(sender: AnyObject) {
if let addTitle = addTitle.text where addTitle != "", let addDescription = addDescription.text where addDescription != "", let addLocation = addLocation.text where addLocation != "" {
dispatch_async(backgroundQueue, {
self.postToFirebase()
dispatch_async(dispatch_get_main_queue(), { () -> Void in
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let listVC = storyboard.instantiateViewControllerWithIdentifier("TBC") as! UITabBarController
listVC.selectedIndex = 1
self.presentViewController(listVC, animated: false, completion: nil)
})
})
}
}
func textViewDidBeginEditing(textView: UITextView) {
placeholderLbl.hidden = true
}
func textViewDidEndEditing(textView: UITextView) {
if textView.text == "" {
placeholderLbl.hidden = false
}
}
func textFieldShouldReturn(textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true
}
}
and the swift file to configure the cell:
import UIKit
import Alamofire
import Firebase
class ListCell: UITableViewCell {
#IBOutlet weak var listImg: UIImageView!
#IBOutlet weak var listTitle: UILabel!
#IBOutlet weak var listTradeOption: UILabel!
#IBOutlet weak var listLocation: UILabel!
#IBOutlet weak var headImg: UIImageView!
var list: List!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
func retrieveImages() {
DataService.ds.REF_LISTS.observeEventType(FEventType.Value, withBlock: { snapshot in
if let snapshots = snapshot.children.allObjects as? [FDataSnapshot] {
for snap in snapshots {
let image = snap.value.objectForKey("images") as! String
let decodedData = NSData(base64EncodedString: image, options: NSDataBase64DecodingOptions.IgnoreUnknownCharacters)
let decodedImage = UIImage(data: decodedData!)
self.headImg.image = decodedImage
}
}
})
}
func configureCell(list: List) {
self.list = list
self.listTitle.text = list.listTitle
self.listTradeOption.text = list.listTradeOption
self.listLocation.text = list.listLocation
retrieveImages()
}
}
also the list Model file:
import Foundation
import Firebase
class List {
private var _listTitle: String!
private var _listDescription: String!
private var _listTradeOption: String!
private var _listLocation: String!
private var _listImageURL: String?
private var _listKey: String!
private var _listRef: Firebase!
var listTitle: String? {
return _listTitle
}
var listDescription: String? {
return _listDescription
}
var listTradeOption: String? {
return _listTradeOption
}
var listLocation: String? {
return _listLocation
}
var listKey: String {
return _listKey
}
var listImageURL: String? {
return _listImageURL
}
init(title: String, description: String, tradeOption: String, location: String, listImageURL: String?) {
self._listTitle = title
self._listDescription = description
self._listTradeOption = tradeOption
self._listLocation = location
self._listImageURL = listImageURL
}
init(listKey: String, dictionary: Dictionary<String, AnyObject>) {
self._listKey = listKey
if let title = dictionary ["title"] as? String {
self._listTitle = title
}
if let desc = dictionary ["description"] as? String {
self._listDescription = desc
}
if let trade = dictionary ["tradeOption"] as? String {
self._listTradeOption = trade
}
if let loc = dictionary ["location"] as? String {
self._listLocation = loc
}
if let imgUrl = dictionary["images"] as? String {
self._listImageURL = imgUrl
}
self._listRef = DataService.ds.REF_LISTS.childByAppendingPath(self._listKey)
}
}
i've got also a DataServicefile, where i create a user by unique ID with this code:
var REF_USER_CURRENT: Firebase {
let uid = NSUserDefaults.standardUserDefaults().valueForKey(KEY_UID) as! String
let user = Firebase(url: "\(REF_BASE)").childByAppendingPath("users").childByAppendingPath(uid)
return user!
}
func createFirebaseUser(uid: String, user: Dictionary<String, String>) {
REF_USERS.childByAppendingPath(uid).setValue(user)
}
i know it's a lot but maybe the best way to help :)
Try editing this in List Cell
var imageURL = String()
func retrieveImages() {
let decodedData = NSData(base64EncodedString: imageURL, options: NSDataBase64DecodingOptions.IgnoreUnknownCharacters)
let decodedImage = UIImage(data: decodedData!)
self.headImg.image = decodedImage
}
func configureCell(list: List) {
self.list = list
self.listTitle.text = list.listTitle
self.listTradeOption.text = list.listTradeOption
self.listLocation.text = list.listLocation
self.imageURL = list.listImageURL //you already had the image url for that specific cell
retrieveImages()
}
Storing and accessing images using base64String in firebase is not an
efficient way, instead of that we can use FirebaseStorage (Google cloud storage
bucket) for uploading images to Firebase and it will provide us
download URL for a particular image. We can store that URL into our database simply in a string format and access it whenever we
required and then download the corresponding image from that URL by
using SDWebImage.
Refer below link for integrating FirebaseStorage into your project: https://firebase.google.com/docs/storage/ios/upload-files