I have 2 UIViewControllers(A,B). A is set as the delegate for B and then B is presented. The protocol is set up in B's header:
#protocol BDelegate <NSObject>
- (IBAction)finishOrder:(id)sender;
#end
delegate property is declared:
#property (nonatomic, assign) id<BDelegate> delegate;
A sets B.delegate = self;
Then on a button press B calls:
if (self.delegate) {
[self.delegate finishOrder:nil];
}
However, on first run lldb gives me:
-[UITextInteractionAssistant finishOrder:]: unrecognized selector sent to instance
second run:
-[NSInvocation delegate]: unrecognized selector sent to instance
third run:
-[__NSCFDictionary delegate]: unrecognized selector sent to instance
forth run:
-[UITextTapRecognizer finishOrder:]: unrecognized selector sent to instance
So... no code changed, but self changed from A to NSInvocation and __NSCFDictionary and the delegate for B (self of A) changed to UITextInteractionAssistant and UITextTapRecognizer... Never seen anything like it. Any ideas? Thanks!
You are over-releasing somewhere. Try turning on Zombies, and use Instruments to track down the over-release.
Related
I am new in Swift,
I have my appDelegate file in Objective-C and my current controller is in Swift. The issue is I have a property in my appdelegate as USER_ID to contain login user id and a token property as AUTH_TOKEN.
#property (nonatomic, strong) NSString * USER_ID;
#property (nonatomic, strong) NSString * AUTH_TOKEN;
I want these values on my Swift controller. For this I have an instance of current appDelegate like:
let APP_DELEGATE_SWIFT = UIApplication.shared.delegate as! AppDelegate
and now when I print these to value on Swift controller like:
print("AuthToken - \(APP_DELEGATE_SWIFT.auth_TOKEN!)")
print("user id - \(APP_DELEGATE_SWIFT.user_ID!)")
I am able to get value of auth_TOKEN, but in the case of user_ID, it gives me error like:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFNumber length]: unrecognized selector sent to instance 0xb000000000036783'
Error indicates you are using a NSString method on a NSNumber. Please, check if your data are strings.
I'm doing some cocoa programming follows the Cocoa programming for Mac OS X, I'm trapped in Chapter 8(NSArrayController), I'm following the guide defined in that book, but I don't know why the app always raising unrecognized selector sent to instance error.
My code is here RaiseMan, If you have any suggestion, Thanks very much.
I've fixed it myself. The behaviour of NSArrayController add method is to copy an object, not holding the pointer of the object, What I've encountered is I haven't implement NSCoping protocol in class Person, I fixed it like this:
in Person.h declar NSCoping:
#interface Person : NSObject <NSCopying> {
NSString *personName;
float expectedRaise;
}
and in Person.m implement it:
-(id)copyWithZone:(NSZone *)zone
{
return self;
}
Getting an NSException error. Looked around and it may be how I am calling the method but can't troubleshoot it, I'm a beginner so a good explanation is very much appreciated.
Here is my AddListingViewController.h
#import <UIKit/UIKit.h>
#import "ListingTableViewController.h"
#import "ListingManager.h"
#interface AddListingViewController : UIViewController
#property (nonatomic) ListingTableViewController *manager;
#property (nonatomic) ListingManager *add;
#end
Here is my AddListingViewController.m
#import "AddListingViewController.h"
#interface AddListingViewController ()
#property (weak, nonatomic) IBOutlet UITextField *title;
#property (weak, nonatomic) IBOutlet UITextView *desc;
#property (weak, nonatomic) IBOutlet UITextField *price;
#end
#implementation AddListingViewController
#synthesize manager = _manager;
#synthesize add = _add;
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.manager = [[ListingTableViewController alloc] init];
self.add = [[ListingManager alloc] init];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
//cancel posting on tap
- (IBAction)cancelListing:(UIBarButtonItem *)sender {
NSLog(#"cancel tapped thpugh");
[self dismissViewControllerAnimated:YES completion:nil];
}
//add item on tap
- (IBAction)addListing:(UIBarButtonItem *)sender {
NSLog(#"Add button tapped");
self.add.listingTitle = self.title.text;
self.add.listingDescription = self.desc.text;
self.add.listingPrice = self.price.text;
[self.manager.listings addObject:self.add];
[self dismissViewControllerAnimated:YES completion:nil];
}
#end
The error I am getting (I'm sure it says is clearly here but I don't know how to troubleshoot it)
2014-06-30 21:37:44.825 Wildcat Exchange[1981:180450] Add button tapped
2014-06-30 21:37:44.827 Wildcat Exchange[1981:180450] -[UITextInputTraits text]: unrecognized selector sent to instance 0xc193e90
2014-06-30 21:37:44.831 Wildcat Exchange[1981:180450] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UITextInputTraits text]: unrecognized selector sent to instance 0xc193e90'
First the exception. The exception is an NSInvalidArgumentException, which most often means you tried to call a function or method that doesn't exist on the object you tried to call it on, it could also be the case that an erroneous parameter was passed to a function.
Imagine for example you write a function that takes a string and turns it into an integer (lets ignore the fact that NSString already provides this). What should happen if someone passes in a string that can't be converted, say they pass the string "hello world". It's possible that your function would decide it can't do anything and so throws an InvalidArgmentException (this case is more rare in the Cocoa world and most often nil is returned instead of an exception being thrown).
In this case however it's the former and the error goes on further to say "-[UITextInputTraits text]: unrecognized selector sent to instance 0xc193e90". The part "unrecognized selector sent to instance" means just confirms what we think NSInvalidArgumentException means, that you tried to call a method (otherwise known as a selector) to some object stored at memory address 0xc193e90. What kind of object is at that address and what method did you call? That's the -[UITextInputTraits text]: part, it means you had an object of type UITextInputTraits and you tried to call the 'text' method on it.
Next step is finding where the crash occurred. To do this you need to set a global exception breakpoint (though given your crash happened right after you logged "Add button tapped" it likely on the following line, though we aren't sure. I'm not going to beat a dead horse on this one, it's been asked before and explained by Apple.
Once you have an exception breakpoint set Xcode will stop your app on the line that caused the exception to be thrown. At this point you can use some debugger commands or look at the surrounding code to figure out more. For example if it breaks on the third line in your addListing function it might be useful to know the kind of class so at the lldb console prompt you might try
po self.title
or
po [self.title class]
If it's not a UITextfield or UITextView then you may have something wired incorrectly in you storyboard on XIB file, in which case you should check the connections inspector in interface builder.
Good luck
It looks like you're calling text when the name of the selector is text: (with the colon). Try adding the colon at the end and see if that works
I have an NSString declared like this :
.h:
#interface ViewController : UIViewController
{
NSString * aString;
}
#property(nonatomic,copy)NSString *aString;
.m :
#synthesize aString;
......
aString=[[NSString alloc]init];
aString=#"Hello World";
NSLog(#"%#",aString);//The app crashes here
The app crashes with this stack trace:
-[CFString respondsToSelector:]: message sent to deallocated instance
remove the line:
aString=[[NSString alloc]init];
and set values to the property:
self.aString=#"Hello World";
Doing: aString=#"Hello World"; means you are setting value to the instance variable, without using the accessor methods of the property, then you are responsible to the memory management and it's more complicated. Get the value by: self.aString also.
P.S. Always work though properties, almost never use the instance variables, (only in the dealloc method release the ivar, otherwise if you are not good at memory management you will always have problems, but properties do everything for you)
In this part of my code I get a warning message saying it doesn't implement the custom protocol I made.
detailViewController.delegate = self;
How do I implement the protocol?
Then when I run the program it crashes saying
'-[DetailViewController setDelegate:]: unrecognized selector sent to instance 0x6a1c450'
Declare your class like this:
#interface DetailViewController : UIViewController <MyProtocol> {
// Class stuff
}