I am writing my application in Swift. Here is the viewDidLoad() function:
override func viewDidLoad() {
super.viewDidLoad()
if !self.check_file() {
print("Missing file to read")
performSegueWithIdentifier("missingfileViewController", sender: nil)
}
}
Here is check_file() function:
func check_file() -> Bool {
let fileManager = FileManager.default
return fileManager.fileExists(atPath:"/Applications/Readfile.app/Contents/Resources/textfile/readthis.txt")
}
I'm receiving this error Cannot find 'performSegueWithIdentifier' in scope.
Can anyone help me with this? Great thanks
EDIT: I have even tried to use self.performSegueWithIdentifier, but another error comes up: Value of type 'ViewController' has no member 'performSegueWithIdentifier'
self.performSegue(withIdentifier: "missingfileViewController", sender: self)
try this
Related
I'm making a login screen using firebase auth UI and swift.
When I run the app, currently it only shows a welcome screen like this welcome screen.
But I want to display the page the firebase prebuilt UI with a text field that a user can type their email address, something like this. login-with email
Here is my current code.
import UIKit
import FirebaseUI
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
//MARK: - sign in / sign up
#IBAction func playPressed(_ sender: UIButton) {
let authUI = FUIAuth.defaultAuthUI()
guard authUI != nil else {
return
}
authUI?.delegate = self
authUI?.providers = [FUIEmailAuth()]
let authViewController = authUI!.authViewController()
present(authViewController, animated: true, completion: nil)
}
}
extension ViewController: FUIAuthDelegate{
func authUI(_ authUI: FUIAuth, didSignInWith authDataResult: AuthDataResult?, error: Error?) {
if error != nil {
return
}
performSegue(withIdentifier: K.loginSegue, sender: self)
}
}
and I get error on the code written as "authUI?.providers = [FUIEmailAuth()]". The error message is
Use of unresolved identifier 'FUIEmailAuth'FUIEmailAuth
I saw some other people run into the same problem, and I tried pod update, but I still get the error message.
Also, on the firebase document, they use 'FUIEmailAuth' in their sample code, so I wonder why I'm getting that error and how to fix it.
I just add pod 'FirebaseUI/Email' in my pod file and it solved the problem.
I am trying to work on a chat application using swift and when I build and ran the project during the run when I try to go to the ChatVewController it shows this EXC Bad Instruction Error.
The console says
Fatal Error: Unexpectedly found nil while unwrapping an optional value.
Here is my code.
import UIKit
import Firebase
class SportsLogin: UIViewController {
// MARK: Properties
let ref: Firebase = Firebase(url: BASE_URL2)
override func viewDidLoad() {
super.viewDidLoad()
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
super.prepareForSegue(segue, sender: sender)
let navVC2 = segue.destinationViewController as! UINavigationController
let chatVC2 = navVC2.viewControllers.first as! SportsViewController
chatVC2.senderId = ref.authData.uid // Assign the local user’s ID to chatVc.senderId; this is the local ID that JSQMessagesViewController uses to coordinate messages.
chatVC2.senderDisplayName = "" // chatVc.senderDisplayName is set to empty string, since this is an anonymous chat room.
}
#IBAction func loginDidTouch(sender: AnyObject) {
ref.authAnonymouslyWithCompletionBlock { (error, authData) -> Void in
if error != nil { // Check for an authentication error.
print(error.description)
return
}
// Inside of the closure, trigger the segue to move to ChatViewController.
print(self.ref.authData)
self.performSegueWithIdentifier("SportChat", sender: self)
}
}
}
The Line with the error is
chatVC2.senderId = ref.authData.uid // Assign the local user’s ID to chatVc.senderId; this is the local ID that JSQMessagesViewController uses to coordinate messages.
Thanks!
What are the SportsViewController ? if SportViewController is delcare by yourself, you should have the member of variable is sendId, is class memeber and NSString type.
Here is the line of code causing the error
class ViewController: UIViewController, UITextFieldDelegate {
I'm following a tutorial so I can't imagine why this went wrong.
It was working just fine until I added this function into my code:
//Check if user is already logged in when opening app if so display dashboard page
override func viewDidAppear(animated: Bool){
if PFUser.currentUser() != nil{
if self.theUser.user_db_obj.email == "mike#chicagodrumlessons.com"{
self.performSegueWithIdentifier("adminLogin", sender: self) //dislay admin page
}
else{
self.performSegueWithIdentifier("userLogin", sender: self) //display user page
}
}
}
Here is the only lines of code where the delegate is referenced I believe
override func viewDidLoad() {
super.viewDidLoad()
self.email_tf.delegate = self
self.pass_tf.delegate = self
}
Any idea what caused this error and how to fix it?
My guess is you declare non-optional properties and they aren't initialized when the object is. The compiler is telling you you need an init or two to provide the non-optional properties values before the object can be initialized (or make the properties optionals).
First of all let me say that I am very new to programming. What I'm trying to do is add a button that when pressed plays music, and when pressed again the music stops. Ideally when the button is pressed for a third time the music will have reset. Whilst trying to achieve this I'm getting the error message "Expression resolves to an unused function", as I am very new all the help I find online doesn't make any sense to me.
import UIKit
import AVFoundation
class ViewController: UIViewController {
#IBOutlet weak var janitor: UIImageView!
var pianoSound = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("C", ofType: "m4a")!)
var audioPlayer = AVAudioPlayer()
override func viewDidLoad() {
super.viewDidLoad()
audioPlayer = AVAudioPlayer(contentsOfURL: pianoSound, error: nil)
audioPlayer.prepareToPlay()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
#IBAction func PianoC(sender: AnyObject) {
audioPlayer.play()
if audioPlayer.playing { audioPlayer.stop} else { audioPlayer.play}
}
}
Swooping in here after Martin R's comment ...
if audioPlayer.playing { audioPlayer.stop} else { audioPlayer.play}
On this line, you are not calling the stop and play functions, but simply accessing them. Resolving to an unused function is trying to tell you that you have an expression that is returning a function type, but you are never calling it (audioPlayer.stop and audioPlayer.play are the expressions in question here).
To rid yourself of this error, and probably produce the correct behavior, try calling the functions.
if audioPlayer.playing {
audioPlayer.stop()
} else {
audioPlayer.play()
}
Swift 4:
Just add the braces '()' next to the method name
override func viewWillAppear(_ animated: Bool) {
addView //error: Expression resolves to an unused function
}
func addView(){
}
Solution:
override func viewWillAppear(_ animated: Bool) {
addView()
}
Here's a simplified version of Brian's answer:
audioPlayer.playing
? audioPlayer.stop()
: audioPlayer.play()
if audioPlayer.playing { audioPlayer.stop} else { audioPlayer.play} is just accessing the member variables. It's not doing anything.
I am sure you're intention was to do audioPlayer.stop() and audioPlayer.start().
However just to explain the ERROR and not to solve your issue:
had you done something like
if audioPlayer.playing { self.playerStatus= audioPlayer.stop} else { self.playerStatus = audioPlayer.play}
Then you were actually doing something you were setting a parameter. accessing or just getting a parameter is stupid in the eyes of the compiler :)
#IBAction func getNewPhotoAction(sender: AnyObject) {
println("getNewPhotoAction")
}
override func viewDidLoad() {
super.viewDidLoad()
self.getNewPhotoAction(sender: AnyObject) // Error
}
I just want to call the getNewPhotoAction IBAction method in viewDidLoad.
Which parameter to enter in this line -> self.getNewPhotoAction(?????) ?
I don't have any parameter. I just need to call.
I used in Objective-C style:
[self getNewPhotoAction:nil]
but I don't know Swift style.
The parameter sender indicates who are calling the action method.
When calling from viewDidLoad, just pass self to it.
override func viewDidLoad() {
super.viewDidLoad()
getNewPhotoAction(self)
}
By the way, if the sender parameter of the getNewPhotoAction method wasn’t used, the parameter name can be omitted.
#IBAction func getNewPhotoAction(AnyObject) {
println("getNewPhotoAction")
}
You could always create a separate func that you call on in your viewDidLoad or in your IBAction
override func viewDidLoad() {
super.viewDidLoad()
self.getNewPhoto()
}
func getNewPhoto(){
//do whatever you want here.
println("getnewphotoaction")
println("whatever you want")
}
#IBAction func getNewPhotoAction(sender: AnyObject) {
self.getNewPhoto()
}
Swift 4.2
#IBAction func getNewPhotoAction(sender: Any) {
println("getNewPhotoAction")
}
override func viewDidLoad() {
super.viewDidLoad()
self.getNewPhotoAction(AnyObject.self)
}
If you still need to reference the UIButton or whatever is sending the action and want to call it from code at the same time - you can do this too:
onNext(UIButton())
Wasteful, but less code.
#IBAction func getNewPhotoAction(sender: AnyObject?){
......
}
**AnyObject** means that you have to pass kind of Object which you are using, nil is not a AnyObject.
But **AnyObject?**, that is to say AnyObject is Optional, nil is a valid value.
meaning the absence of a object.
self .getNewPhotoAction(nil)
You actually don't need to pass any object at all. If you have no need to use the sender, then declare the function without it like this:
#IBAction func getNewPhotoAction() { ... }
And use it like this:
self.getNewPhotoAction()
You may need to re-connect the outlet in interface builder when you make this change (remove it and then add it back) if this method is connected to an event in interface builder.
#IBAction func getNewPhotoAction(sender: AnyObject? = nil) {
print("getNewPhotoAction")
}
override func viewDidLoad() {
super.viewDidLoad()
self.getNewPhotoAction(nil)
}
Since you don't have any sender, hand over nil.
self.getNewPhotoAction(nil)