"unrecognized selector sent to instance" when selector is in other file - ios

Here is the problematic ViewController class.
class ViewController: UIViewController {
#IBOutlet weak var photoSelectActionSheet: UIButton!
#IBOutlet weak var selectedImageView: UIImageView!
var imagePicker: UIImagePickerController!
var selectedImage: UIImage? = ni
var iCloud = ICloud() // see below for code of that file
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
// Must use this system to check each time the view appears.
// This is checked each time so if user goes into settings and come back in same session
// then this will be updated => viewDidLoad is cached and not reloaded each time.
NotificationCenter.default.addObserver(
self,
selector:#selector(ICloud.checkIfUserIsLoggedInIcloud), // PROBLEM IS HERE
name: UIApplication.didBecomeActiveNotification,
object: nil)
}
// more code not shown
}
Problem is in viewWillAppear: the notification center calls a selector that is in an other Model file ICloud.swift:
import UIKit
import CloudKit
class ICloud {
var isLoggedInIcloud: Bool? = nil
var userIcloudId: String? = nil
func getUserIcloudId() {
// Run this only if user is logged into icloud, and we don't have yet its iCloud id.
if isLoggedInIcloud ?? false && userIcloudId == nil {
CKContainer.default().fetchUserRecordID(completionHandler: { (recordId, error) in
if let id = recordId?.recordName {
print("userIcloudId set to: " + id)
self.userIcloudId = id
}
else if let error = error {
print(error.localizedDescription)
}
})
}
}
#objc func checkIfUserIsLoggedInIcloud() {
// Check if user is logged into iCloud
CKContainer.default().accountStatus { accountStatus, error in
if accountStatus == .available {
self.isLoggedInIcloud = true
print("isLoggedInIcloud set to true")
self.getUserIcloudId()
return
}
print("User is not logged into icloud")
}
}
}
I expected "ICloud.checkIfUserIsLoggedInIcloud" passed in selector to work. It doesn't:
2020-07-01 11:46:12.931419+0200 QDog[53348:3422745] -[QDog.ViewController checkIfUserIsLoggedInIcloud]: unrecognized selector sent to instance 0x7fcad5f0c1d0
2020-07-01 11:46:12.938567+0200 QDog[53348:3422745] *** Terminating app due to uncaught exception
If I add back the functions and variables from ICloud.swift directly into the ViewController file and pass to the selector "checkIfUserIsLoggedInIcloud" it WILL WORK properly.
I wanted to put all icloud code into a separate model file for separation of concerns and to make the code more clear.
Question is: why does 'ICloud.checkIfUserIsLoggedInIcloud' passed as selector doesn't work? And how to make this work with ICloud code in its own file, and not in ViewController file?

According to the documentation, the first parameter should be the observer, which in your case you're pointing to self.
In this case, self is your ViewController, which doesn't have the method checkIfUserIsLoggedInIcloud. In order to make it work, as an observer, you need to pass the property iCloud instead of self.

Related

Store my custom Class in UserDefaults, and casting(parsing) this UserDefault to reach values (Swift 4.2)

I have created a dummy IOS Application to explain my questions well. Let me share it with all details:
There are 2 Pages in this dummy IOS Application: LoginPageViewController.swift and HomepageViewController.swift
Storyboard id values are: LoginPage, Homepage.
There is login button in Login page.
There are 3 labels in Homepage.
App starts with Login page.
And i have a class file: UserDetail.swift
And there is one segue from login page to home page. Segue id is: LoginPage2Homepage
UserDetail.swift file
import Foundation
class UserDetail {
var accountIsDeleted = false
var userGUID : String?
var userAge: Int?
}
LoginPageViewController.swift file
import UIKit
class LoginPageViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
#IBAction func loginButtonPressed(_ sender: UIButton) {
var oUserDetail = UserDetail()
oUserDetail.accountIsDeleted = true
oUserDetail.userAge = 38
oUserDetail.userName = "Dirk Kuyt"
UserDefaults.standard.set(oUserDetail, forKey: "UserCredentialUserDefaults")
UserDefaults.standard.synchronize()
performSegue(withIdentifier: "LoginPage2Homepage", sender: nil)
}
}
HomepageViewController.swift file
import UIKit
class HomepageViewController: UIViewController {
var result_userGUID = ""
var result_userAge = 0
var result_isDeleted = false
#IBOutlet weak var labelUserGuidOutlet: UILabel!
#IBOutlet weak var labelAgeOutlet: UILabel!
#IBOutlet weak var labelAccountIsDeletedOutlet: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
self.setVariablesFromUserDefault()
labelUserGuidOutlet.text = result_userGUID
labelAgeOutlet.text = String(result_userAge)
labelAccountIsDeletedOutlet.text = String(result_isDeleted)
}
func setVariablesFromUserDefault()
{
if UserDefaults.standard.object(forKey: "UserCredentialUserDefaults") != nil
{
// I need a help in this scope
// I have checked already: My UserDefault exists or not.
// I need to check type of the value in UserDefault if UserDefault is exists. I need to show print if type of the value in UserDefault is not belongs to my custom class.
// And then i need to cast UserDefault to reach my custom class's properties: userGUID, userAge, isDeleted
}
else
{
print("there is no userDefault which is named UserCredentialUserDefaults")
}
}
}
My purposes:
I would like to store my custom class sample(oUserDetail) in UserDefaults in LoginPageViewController with login button click action.
I would like to check below in home page as a first task: My UserDefault exists or not ( I did it already)
I would like to check this in home page as a second task: if my UserDefault exists. And then check type of the UserDefault value. Is it created with my custom class? If it is not. print("value of userdefault is not created with your class")
Third task: If UserDefault is created with my custom class. And then parse that value. Set these 3 variables: result_userGUID, result_userAge, result_isDeleted to show them in labels.
I get an error after I click the login button in Login Page. Can't I store my custom class in UserDefaults? I need to be able to store because I see this detail while I am writing it:
UserDefaults.standart.set(value: Any?, forKey: String)
My custom class is in Any scope above. Isn't it?
You can't store a class instance without conforming to NSCoding / Codable protocols
class UserDetail : Codable {
var accountIsDeleted:Bool? // you can remove this as it's useless if the you read a nil content from user defaults that means no current account
var userGUID : String?
var userAge: Int?
}
store
do {
let res = try JSONEncoder().encode(yourClassInstance)
UserDefaults.standard.set(value:res,forKey: "somekey")
}
catch { print(error) }
retrieve
do {
if let data = UserDefaults.standard.data(forKey:"somekey") {
let res = try JSONDecoder().decode(UserDetail.self,from:data)
} else {
print("No account")
}
}
catch { print(error) }

viewDidLoad, is it called only once?

I am a beginner in iOS development, and I am following one tutorial using firebase database to make a simple chat app. Actually I am confused with the use of viewDidLoad method.
Here is the screenshot of the app: https://ibb.co/gqD4Tw
I don't understand why retrieveMessage() method is put on viewDidLoad when I want to send data (chat message) to firebase database, I used sendButtonPressed() method (which is an IBAction) and when I want to retrieve data from the database, I use retrieveMessage().
The retrieveMessage() method is called on viewDidLoad, as far as I know the viewDidLoad method is called only once after the view is loaded into memory. We usually use it for initial setup.
So, if viewDidLoad is called only once in initial setup, why the retrieveMessage() method can retrieve all the message that I have sent to my own database over and over again, after I send message data to the database ?
I don't understand why retrieveMessage() method is put on viewDidLoad below is the simplified code:
class ChatViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
var messageArray = [Message]()
#IBOutlet var messageTextfield: UITextField!
#IBOutlet var messageTableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
//Set as the delegate and datasource :
messageTableView.delegate = self
messageTableView.dataSource = self
//the delegate of the text field:
messageTextfield.delegate = self
retrieveMessage()
///////////////////////////////////////////
//MARK: - Send & Recieve from Firebase
#IBAction func sendPressed(_ sender: AnyObject) {
// Send the message to Firebase and save it in our database
let messageDB = FIRDatabase.database().reference().child("message")
let messageDictionary = ["MessageBody":messageTextfield.text!, "Sender": FIRAuth.auth()?.currentUser?.email]
messageDB.childByAutoId().setValue(messageDictionary) {
(error,ref) in
if error != nil {
print(error!)
} else {
self.messageTextfield.isEnabled = true
self.sendButton.isEnabled = true
self.messageTextfield.text = ""
}
}
}
//Create the retrieveMessages method :
func retrieveMessage () {
let messageDB = FIRDatabase.database().reference().child("message")
messageDB.observe(.childAdded, with: { (snapshot) in
let snapshotValue = snapshot.value as! [String:String]
let text = snapshotValue["MessageBody"]!
let sender = snapshotValue["Sender"]!
let message = Message()
message.messsageBody = text
message.sender = sender
self.messageArray.append(message)
self.messageTableView.reloadData()
})
}
}
viewDidLoad method is called only once in ViewController lifecycle.
The reason retrieveMessage() is called in viewDidLoad because it's adding observer to start listening for received and sent message. Once you receive or send message then this block(observer) is called and
then adding that text in array self.messageArray.append(message) and updating tableview.
viewDidLoad gets called only once but the firebase functions starts a listener, working in background and syncronizeing data.
Its called in viewDidLoad because it tells -> When this view loads, start listening for messages.
ViewDidLoad() is only called upon initializing the ViewController.
If you want to have a function called every time the user looks at the VC again (e.g. after a segue back from another VC) you can just use ViewDidAppear().
It is also called when ViewDidLoad() is called.

Referencing IBOutlet in another View Controller

So, I have been having some major trouble figuring this out and I have searched extensively for a solution but I surprisingly could not find one. I am attempting to create a multiple page (5, to be exact) Sign-Up for users.
I'll start off by showing you the layout of page 1 and 5 (since solving that issue will solve the issue for page 2-4):
Sign Up Page #1
Sign Up Page #5
As you may see (from the page control dots), I am using a page view controller to allow users to scroll from page to page. What I am trying to accomplish is giving the user the ability to enter their sign-up information in pages 1-5 before submitting it all at once (which can be located on page 5).
Here is the current code I am using for page #1:
class SignUpInfoViewController: UIViewController {
#IBOutlet weak var emailTextField: UITextField!
#IBOutlet weak var passwordTextField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Here is the current code I am using for page #5:
class TermsOfUseViewController: UIViewController {
let minPasswordCharCount = 6
#IBAction func signUpAction(_ sender: Any) {
let providedEmailAddress = SignUpInfoViewController().emailTextField.text!
let providedPassword = SignUpInfoViewController().passwordTextField.text!
let trimmedPassword = providedPassword.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines)
if !(validEmail(enteredEmail: providedEmailAddress) && validPassword(enteredPassword: trimmedPassword)) {
invalidCredentialsAlert()
}
else {
FIRAuth.auth()?.createUser(withEmail: providedEmailAddress, password: providedPassword) { user, error in
if error == nil {
FIRAuth.auth()!.signIn(withEmail: providedEmailAddress,
password: providedPassword)
}
else {
let alertController = UIAlertController(title: "Error", message: error?.localizedDescription, preferredStyle: .alert)
let defaultAction = UIAlertAction(title: "OK", style: .cancel, handler: nil)
alertController.addAction(defaultAction)
self.present(alertController, animated: true, completion: nil)
}
}
}
}
// Email is valid if it has a standard email format
func validEmail(enteredEmail: String) -> Bool {
let emailFormat = "[A-Z0-9a-z._%+-]+#[A-Za-z0-9.-]+\\.[A-Za-z]{2,64}"
let emailPredicate = NSPredicate(format:"SELF MATCHES %#", emailFormat)
return emailPredicate.evaluate(with: enteredEmail)
}
// Password is valid if it is not empty or greater than a specified number of characters
func validPassword(enteredPassword: String) -> Bool {
if (enteredPassword != "" && enteredPassword.characters.count >= minPasswordCharCount) {
return true
}
return false
}
In the TermsOfUseViewController class, I am attempting to use the emailTextField and passwordTextField outlets from the SignUpInfoViewController, but I am receiving the following error:
fatal error: unexpectedly found nil while unwrapping an Optional value
I debugged the error and saw that the emailTextField property from SignUpInfoViewController is nil and so force unwrapping it will cause the app to crash (Note: I have correctly connected the IBOutlets to the SignUpInfoViewController, so no issue there).
How can I safely transfer the usage of the IBOutlets from the SignUpInfoViewController class to the TermsOfUseViewController class without it crashing? In other words, how can I make it to where the IBOutlets are no longer nil when I reference them in the TermsOfUseViewController class?
Thank you!
That is a perfect scenario for delegate pattern
protocol SignUpProtocol: class {
func didProvideUserData(username: String ,password: String)
}
In your signup class declare a delegate: public weak var delegate:SignUpProtocol?
I am assuming when the user has provided the require info, they need to press some button to go to the next step: Thus in that button you should raise the delegate
#IBAction func nextButton(sender:UIButton) {
guard let username = usernameTextfield?.text, let password = passwordTextField?.text, else { fatalError("textfields were empty") }
if delegate != nil { // this saying when someone is listening to me, I will expose any method associated to me
delegate?.didProvideUserData(username:username, password:password) // passing the username and password from textfield
}
}
if you don't have a button, then look at property observer, where you could have some property
var didFulfill:Bool? = nil {
didSet {
if didFulfill != nil && didFulfill == true {}
// here you check if your textfields are sets then raise the delegate
}
}
set this property didFulfill = when both textfields are not empty :)
Now in your Terms class, just subscribe to that delegate
class TermsOfUseViewController: UIViewController, SignUpProtocol {
var signUpVc: SignUpInfoViewController?
override func viewDidLoad() {
super.viewDidLoad()
signUpVc = SignUpInfoViewController()
signUpVc?.delegate = self
}
func didProvideUserData(username: String, password:String) {
// there is your data
}
}
You have to take in account that you don't have all references for all UIPageViewControllers all the time. That being said, I would suggest either to keep object in UIPageViewController with updated information or using Singleton Pattern to use it to store info into it and later use it. UIPageViewController are being reused and you might have one before and one after and relying onto having them would be wrong.
You can use UIPageViewController as self.parentViewController or something like that.

SIGABRT error in swift 2

I recently updated my app to Swift 2, and had one error, which I solved thanks to you guys.
And now, I have a second error, which is at runtime, when I press the one button to play a sound. It is a signal SIGABRT error.
Here is the error message I get in the debug console:
2016-01-25 09:16:09.019 WarningShot1.0.0[291:19030] -[WarningShot1_0_0.ViewController playMySound:]: unrecognized selector sent to instance 0x135547d30
2016-01-25 09:16:09.021 WarningShot1.0.0[291:19030] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[WarningShot1_0_0.ViewController playMySound:]: unrecognized selector sent to instance 0x135547d30'
*** First throw call stack:
(0x182835900 0x181ea3f80 0x18283c61c 0x1828395b8 0x18273d68c 0x18755fe50 0x18755fdcc 0x187547a88 0x18755f6e4 0x18755f314 0x187557e30 0x1875284cc 0x187526794 0x1827ecefc 0x1827ec990 0x1827ea690 0x182719680 0x183c28088 0x187590d90 0x10005e2e0 0x1822ba8b8)
libc++abi.dylib: terminating with uncaught exception of type NSException
Also, this is the part of the code where it throws this error, in the second line, where the class is declared:
import UIKit
#UIApplicationMain
-> class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
What is happening here? What am I missing / misnaming? What do I need to change in my code to get ot running again. This app is ridiculously simple, and worked for months under the last version of Swift. Why is it now giving me errors?
Thank you for your help.
Here is the code for my ViewController.swift file:
import UIKit
import AVFoundation
import CoreMotion
class ViewController: UIViewController {
var myPlayer = AVAudioPlayer()
var mySound = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("RemSound_01", ofType: "wav")!)
func initYourSound() {
do {
try myPlayer = AVAudioPlayer(contentsOfURL: mySound, fileTypeHint: nil)
myPlayer.prepareToPlay()
// myPlayer.volume = 1.0 // < for setting initial volume, still not perfected.
} catch {
// handle error
}
var motionManager = CMMotionManager()
var currentMaxAccelY : Double = 0.0
func viewDidLoad() {
super.viewDidLoad()
initYourSound()
// Do any additional setup after loading the view, typically from a nib.
//set motion manager properties
motionManager.accelerometerUpdateInterval = 0.17
//start recording data
// motionManager.startAccelerometerUpdatesToQueue(NSOperationQueue.currentQueue(), withHandler: {
// (accelerometerData: CMAccelerometerData!,error:NSError!) -> Void in
// self.outputAccelerationData(accelerometerData.acceleration)
// if(error != nil) {
// print("\(error)")
// }
// })
motionManager.startAccelerometerUpdatesToQueue(NSOperationQueue.currentQueue()!, withHandler: {
(accelerometerData,error) in outputAccelerationData(accelerometerData!.acceleration)
if(error != nil) {
print("\(error)", terminator: "")
}
})
}
//func outputAccelerationData(acceleration : CMAcceleration){
// accY?.text = "\(acceleration.y).2fg"
//if fabs(acceleration.y) > fabs(currentMaxAccelY)
//{
// currentMaxAccelY = acceleration.y
//}
// maxAccY?.text = "\(currentMaxAccelY) .2f"
//}
func outputAccelerationData(acceleration : CMAcceleration){
if fabs(acceleration.y) >= 1.25 {
myPlayer.play()
}
}
func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func playMySound(sender: AnyObject) {
myPlayer.play()
}
// self.resetMaxValues()
// #IBOutlet var accY: UILabel!
// #IBOutlet var maxAccY: UILabel!
// #IBAction func resetMaxValues() {
// currentMaxAccelY = 0
// }
}
}
"unrecognized selector sent to instance" means that there is a mismatch between the action-name in "addTarget()" and the name of the function you want to call. Probably something with the parameters of the function.. It's hard to say without seeing any code.
action: Selector("playMySound:")
would expect to find a function:
func playMySound(sender: AnyObject?) {};
To easier track what's happening, you might add a symbolic breakpoint for all exceptions. You do that in Xcode on the "exceptions" tab (left part of the window) and when an exception is thrown, Xcode stops like usual and you might look up the stack trace. If the call is synchronous, you should easily find and see your mistake.
EDIT: Oh well, you seem to have done the user interface using a XIB file. The mistake could be, you wired the button's action in the XIB file to the view controller. If you later change the method's signature (parameter, name, etc.), UIKit can't find the method. Open the XIB and fix your error. ;-)

Xcode 6: UITapGesture is crashing with (lldb)

I have a class, clickableImage. clickableImage has a callback variable for a function.
When you assign the callback function, I add a gesture recoginizer.
clickableImage has a function 'tapped' which just listens for the tap event as illustrated below.
private func tapped(tap:UITapGestureRecognizer)
{
println("Here")
if(_touchCallback != nil)
{
touchCallback(self)
}
}
var touchCallback:((K_PreviewImage)->Void)
{
set{
if(_touchCallback == nil)
{
var tap:UIGestureRecognizer = UITapGestureRecognizer(target: self, action:"tapped:")
self._image.addGestureRecognizer(tap)
}
_touchCallback = newValue
}
get{
return _touchCallback
}
}
When I tap this image, the app crashes, with only (llb). The println() does not get called. I tried enabling zombies and I 'SOMETIMES' get a message "message sent to deallocated instance".
The image is NOT delloacted, otherwise I wouldn't be able to CLICK on it!
If you have ANY idea what is going on, you would be a live saver
I will give you couple examples that will cause "message sent to deallocated instance".
class ViewController : UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Some other class that has gestureRecognizer it
// Along with views
var otherClass = OtherClass()
self.view.addSubview(otherClass.view)
otherClass.bindGestures()
}
}
Tapping will give you an error.
To fix it:
class ViewController : UIViewController {
var otherClass : OtherClass!
override func viewDidLoad() {
super.viewDidLoad()
self.otherClass = OtherClass()
self.view.addSubview(self.otherClass.view)
self.otherClass.bindGestures()
}
}
Putting your object as a viewController's property solves the problem.
Unfortunately, your example is not comprehensive enough, but the idea is more or less clear.
I would advise you playing with scopes.

Resources