how to use emojis in my project in Xcode and declare them in a dictionary array? - swift

simply I have a uiimage and want to use one of the emojis in that . how can I get some of these emojis and place into my project? this is the place I ll get them from.
https://apps.timwhitlock.info/emoji/tables/iso3166
one more question . next to my uiimage I have a uilabel for currency codes. so to use the image along with the corresponding code do I need to use a dictionary like :
var dict = ["EUR": "imagenameinString"]
or
var dict = ["code" : "EUR", "image": "imagenameinString"
i want to call the currency code and related image/emoji together. This is my code below using a string value for emoji but I get error in this way naturally. how do I edit this code to work? Thank you!
class ViewController: UIViewController {
#IBOutlet weak var amountText: UITextField!
#IBOutlet weak var amountText2: UITextField!
#IBOutlet weak var fromLabel: UILabel!
#IBOutlet weak var fromImage: UIImageView!
#IBOutlet weak var toImage: UIImageView!
#IBOutlet weak var toLabel: UILabel!
let flag = "\u{1F1F8}\u{1F1EA}"
var currencyManager = CurrencyManager()
var from: String = "EUR"
var to: String = "TRY"
var amount: String = "0"
override func viewDidLoad() {
super.viewDidLoad()
amountText.delegate = self
currencyManager.delegate = self
fromImage.image = flag
}

Related

macOS Swift TextField.stringValue not returning updated value until I click on something else

I apologize if this has been asked before but I couldn't find what I was looking for online over the last few hours. I'm still functional a noob with swift.
I am trying to store the stringValue of a TextField when I click a NSButton. If I click anywhere and then click on the NSButton the code works perfect but if I don't click the stringValue is still reporting the previous value.
#IBOutlet weak var NameText: NSTextField!
#IBOutlet weak var SaveChangesAccountButton: NSButton!
var selectedAccountItemNumber = NSInteger()
#IBAction func SaveAccountChanges(_ sender: Any)
{
let AccountName = NameText.stringValue
AccountingData.instance.book.account[selectedAccountItemNumber].name = AccountName
}
You have to call validateEditing() on the text field.
And please conform to the naming convention that variable and function names start with a lowercase letter and don't use NSInteger in Swift.
#IBOutlet weak var nameText: NSTextField!
#IBOutlet weak var saveChangesAccountButton: NSButton!
var selectedAccountItemNumber = 0
#IBAction func saveAccountChanges(_ sender: Any)
{
nameText.validateEditing()
let accountName = nameText.stringValue
AccountingData.instance.book.account[selectedAccountItemNumber].name = accountName
}

How to bind Object's property to an UIlabel using RXSwift?

I have a Model class like below:
class Model{
var currentTemparature:String?
var minTemparature:String?
var maxTemparature:String?
init(currentTemparature:String,minTemparature:String,maxTemparature:String) {
self.currentTemparature = currentTemparature
self.maxTemparature = maxTemparature
self.minTemparature = minTemparature
}
}
I want to bind each of these properties to an UILabel in the ViewController using RXSwift.
For Example:
class ViewController:UIViewController{
#IBOutlet var currentTemparatureLabel: UILabel!
#IBOutlet var minTemparatureLabel: UILabel!
#IBOutlet var maxTemparatureLabel: UILabel!
func UpdateLabels(){
let model = Model.init(currentTemparature: "25", minTemparature: "20", maxTemparature: "30")
currentTemparatureLabel.text = model.currentTemparature
minTemparatureLabel.text = model.minTemparature
maxTemparatureLabel.text = model.maxTemparature
}
}
When ever the value of the model's properties changes, i want to update the label. How can i achieve that?
You should have an Observable of your data Model (that might be fetch from a server for example) and then, you can directly bind the result of it to your label using the .bind(yourlabel.rx.text)
Every time the value will change it will also update your view with the binding to your label
Try using BehaviorRelay
Don't forget to import RxSwift, RxCocoa
class Model{
var currentTemparature: BehaviorRelay<String?>
var minTemparature: BehaviorRelay<String?>
var maxTemparature: BehaviorRelay<String?>
init(currentTemparature:String,minTemparature:String,maxTemparature:String) {
self.currentTemparature = .init(value: currentTemparature)
self.maxTemparature = .init(value: maxTemparature)
self.minTemparature = .init(value: minTemparature)
}
}
class ViewController:UIViewController{
#IBOutlet var currentTemparatureLabel: UILabel!
#IBOutlet var minTemparatureLabel: UILabel!
#IBOutlet var maxTemparatureLabel: UILabel!
private var bag = DisposeBag()
func UpdateLabels(){
let model = Model.init(currentTemparature: "25", minTemparature: "20", maxTemparature: "30")
model.currentTemparature.bind(to: currentTemparatureLabel.rx.text).disposed(by: bag)
model.minTemparature.bind(to: minTemparatureLabel.rx.text).disposed(by: bag)
model.maxTemparature.bind(to: maxTemparatureLabel.rx.text).disposed(by: bag)
}
}

Displaying an Array of type Double on UILabel in Xcode

I am trying to make a basic shopping cart app where you can add an item to your cart, store the cost of the item in an array and then have the array displayed when you click "Receipt". However, every time I click "Receipt" on my app, the simulator crashes and displays
"Thread1: EXC_BAD_INSTRUCTION (code=EXC_1386_INVOP, subcode=0x0)
beside my line of code that reads "allItems += item". If anyone has any ideas as to how I can my array of numbers displayed in a UILabel using a for in loop, please let me know. I will post source code below. Thanks in advance
class ViewController: UIViewController {
var priceOfApple = 1.75
var cart = [Double]()
var total = 0.00
#IBOutlet weak var appName: UILabel!
#IBOutlet weak var Balance: UILabel!
#IBOutlet weak var Instructions: UILabel!
#IBOutlet weak var redApple: UIButton!
#IBOutlet weak var appleInfo: UILabel!
#IBOutlet weak var addToCart: UIButton!
#IBOutlet weak var itemsPurchased: UILabel!
#IBAction func selectedApple(sender: AnyObject) {
Instructions.hidden = true
appleInfo.hidden = false
addToCart.hidden = false
}
#IBAction func purchaseApple(sender: AnyObject) {
cart.append(priceOfApple)
total += priceOfApple
Balance.text = "Balance : \(total)"
Instructions.hidden = false
appleInfo.hidden = true
addToCart.hidden = true
}
#IBAction func viewReceipt(sender: AnyObject) {
redApple.hidden = true
Instructions.hidden = true
itemsPurchased.hidden = false
for item in cart {
var allItems = Double(itemsPurchased.text!)!
allItems += item
allItems = Double(itemsPurchased.text!)!
}
}
}
If I understand correctly, this is what you should do:
First, create a variable outside the for-loop,
then inside the loop that variable will start storing every value in the array separated by " ". Once It is done, you can display its value in the Label.
var itemsInCart = ""
for items in cart{
itemsInCart += String(items) + " "
}
itemsPurchased.text = itemsInCart
I hope it helps!!!
A cleaner and safer way to do it:
let result = cart.flatMap { Double(itemsPurchased.text ?? String()) }.reduce(0: combine: +)
Doing that, you shouldn't get EXC_BAD_INSTRUCTION anymore, but you should still check if is the text convertible to a Double.

Textfield and floats in Swift

I have 2 textfields. If they both has a float value bigger than 100, when you click on my button it should allow you to go to another page.
So far so good, however in my code the text field can't have either int or float or doubles...
What can I do?
As Lukas says you need to convert it to a string. If you are capturing the value in the textfield on button click, you need to convert it, like so:
if let doubleValue = Double(textField.text!) {
}
I think based on what you have said, you need to do something like this:
class ViewController: UIViewController {
#IBOutlet weak var box1: UITextField!
#IBOutlet weak var box2: UITextField!
#IBOutlet weak var Check: UIButton!
#IBOutlet weak var Page1: UILabel!
#IBAction func didPressCheckButton(sender: UIButton) {
if let stringValue = box1.text {
if let doubleValue = Double(stringValue) {
if doubleValue > 100 {
print("Navigate to next page")
}
}
}
}
}
You will need to modify the check so that you check if both text boxes have values over 100, but this is a starting point.

swift fatal error: unexpectedly found nil while unwrapping an Optional value(lldb)from email -in my code?

This is my code here found the fatal error.i try to solve this problem but lot of times it shows,and it shows the EXC_BAD_INSTRUCTION(code=EXC_i386_INVOP,subcode=0*0)
import UIKit
class VerifyProfileViewController: UIViewController,UITextFieldDelegate,FBLoginViewDelegate {
var liveurl = demo_url
#IBOutlet weak var f_name: UITextField!
#IBOutlet weak var l_name: UITextField!
#IBOutlet weak var email_id: UITextField!
#IBOutlet weak var mobilenumber: UITextField!
#IBOutlet weak var myLabel: UILabel!
#IBOutlet weak var nextOutlet: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
// NSUserDefaults.standardUserDefaults().synchronize()
var mobile_Number = mobilenumber.text
var email_Address = email_id.text
var first_name = f_name.text
var last_name = l_name.text
var email1 = isValidEmail(email_Address)
let email = NSUserDefaults.standardUserDefaults().objectForKey("fb_email") as NSString
println("The email :\(email)")
let first_name = NSUserDefaults.standardUserDefaults().objectForKey("fb_name") as NSString
println("The firstname :\(first_name)")
let last_name = NSUserDefaults.standardUserDefaults().objectForKey("fb_lname") as NSString
println("The firstname :\(last_name)")
//NSUserDefaults.standardUserDefaults().setObject(facebookid, forKey:"id")
//let facebook_id = NSUserDefaults.standardUserDefaults().objectForKey("fb_id")! as NSString
//println("The fbID :\(facebook_id)")
email_id.text = email
f_name.text = first_name
l_name.text = last_name
NSUserDefaults.standardUserDefaults().setValue("abc#def.com", forKey: "fb_email")
if let email = NSUserDefaults.standardUserDefaults().stringForKey("fb_email") {
println(email)
}