How to access text from UITextField Swift 2 - swift

I have Swift 2 and I can't access my textfield's text, what should I do?
#IBOutlet weak var CoolField: UITextField!
let texts = Int(CoolField.text?)
There is always the error message that Instance member CoolField cannot be used with type ViewController

You cannot define the following as class variable let texts = Int(CoolField.text?) , it doesn't work like that in Swift :
Keep this defined as it is : #IBOutlet weak var CoolField: UITextField!
Define under it as class variable : var texts : Int!
Then use it in viewDidLoad for example like : texts = Int(CoolField.text!)
Thats should work fine, Good luck !

Related

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

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
}

MacOS Swift Xcode NSTextField! convert integerValue to doubleValue

I would like to have a user input numbers and then have a calculation as the result. I have succeeded with this but I would like the user to be able to input doubles if they want too and also have the result be a double. This is my issue. I have an input TextField box and a Label result. So two Outlets. So the code I have pasted below works but only for whole numbers (ints) Hope this makes sense. Any ideas? Keep in mind I am a newbie to Swift Programming. cheers
using Xcode:
#IBOutlet weak var userInput: NSTextField!
#IBOutlet weak var result: NSTextField!
var value = 100
#IBAction func pushButtonforResult(_ sender: Any) {
result.integerValue = value / userInput.integerValue
}

I am stuck in an xcode 8 situation

I am currently making a calculator, and I am using Swift, with XCode 8. I have already defined a variable, but when I want to connect it to my button and text field, it says use of unresolved identifier. Can anyone help me?
Screenshot:
Your sum variable is defined inside the Solve function scope, so it can only be used inside that scope. You have to make your variable global (i.e. inside the UIViewController scope) for it to be visible inside all the functions.
Define var sum outside the function, under Oranges.
class ViewController : UIViewController {
#IBOutlet weak var Apples :UITextField!
#IBOutlet weak var Oranges :UITextField!
var sum : String!
#IBAction func Solve(_ sender: AnyObject)
{
var apples = Int(Apples.text!)
var oranges = Int(Oranges.text!)
sum = String(oranges! + apples!)
}
}

Reference non-initiated variable at top of file

this is a bit odd to say but essentially here is my code below:
import UIKit
class BarcodeScanPopover: UIViewController, UIPopoverPresentationControllerDelegate {
#IBOutlet weak var navbar: UINavigationItem!
#IBOutlet weak var product: UILabel!
#IBOutlet weak var productimage: UIImageView!
#IBOutlet weak var scanner: UIView!
var scan: MTBBarcodeScanner = MTBBarcodeScanner(previewView: scanner);
override func viewDidLoad() {
So the issue I'm having is I can't declare "scan" without initiating the MTBBarcodeScanner object, but at the same time, I can't initiate the MTBBarcodeScanner object without calling "scanner" which is not possible at the top of the file. Unfortunately MTBBarcodeScanner() is not a valid init and causes crashes so that is not possible either.
I need to do this because I need to access "scan" at different points in the code - not just in one code method.
Any suggestions?
If you are sure you will always have an instance of the MTBBarcodeScanner after the view loaded, declare it as MTBBarcodeScanner!: var scan: MTBBarcodeScanner!. That makes it an implicitly unwrapped optional, which is allowed to be nil unless you try to access some property/function on it.
You should therefore then make sure that you always assign something to it before ever accessing it in any other way. That can and should be done in viewDidLoad:
scan = MTBBarcodeScanner(previewView: scanner)
You can let your MTBBarCodeScanner instance be an optional, and initially set it to nil.
var scan: MTBBarcodeScanner? = nil
Thereafter call your initializer to update its value as soon as scanner instance is available to you (e.g. in viewDidLoad).

Could not find initializer for type 'TableCell' that accepts an argument list of type

XCODE: Swift
Full Error:
Could not find initializer for type TableCell that accepts an argument list of type caseImage: (UIImge, caseName: String, caseDate: String)
var patientCaseArray = [TableCell]()
func loadSampleData() {
let Photo1 = UIImage(named: "retino1")!
let case1 = TableCell(caseImage: Photo1, caseName: "John Smith", caseDate: "2015-10-18" )
//error
patientCaseArray = [case1]
}
Working on a table cell class defined elsewhere, with this set of IBOutlets:
#IBOutlet weak var caseImage: UIImageView!
#IBOutlet weak var caseName: UILabel!
#IBOutlet weak var caseDate: UILabel!
I haven't done anything to the class except add the outlets.
I'm very new to swift and I'm going through a tutorial which says this is supposed to compile fine. It isn't supposed to input to the table yet. What am I missing?
Thanks in advance.
You have a discrepancy in types:
Could not find initializer for type 'TableCell' that accepts an
argument list of type caseImage: '(UIImge, caseName: String,
caseDate: String)'
and
#IBOutlet weak var caseImage: UIImageView!
UIImageView is not the same as UIImage, you will need to decide which one you want to use.