*** Terminating app due to uncaught exception 'NSInvalidArgumentException', - ios

i am newbie to iOS and i want to implement a splash screen and load the data from database then transmit to another view controller to display data in a UITableView
here is my code
#import "SplashViewController.h"
#import "DataLoader.h"
#import "UISessionTable.h"
#interface SplashViewController ()
#end
#implementation SplashViewController
#synthesize sessionsDataFromDatabase;
-(void) viewDidLoad{
[super viewDidLoad];
double currentTime = [[NSDate date] timeIntervalSince1970];
dispatch_queue_t downloadQueue = dispatch_queue_create("session data loader", NULL);
dispatch_async(downloadQueue, ^{
//code to load session into array
self.sessionsDataFromDatabase = [DataLoader getSessions];
dispatch_async(dispatch_get_main_queue(), ^{
double differance = 5000.0 - ([[NSDate date] timeIntervalSince1970] - currentTime) ;
differance = differance<0? 0:differance;
[[NSTimer scheduledTimerWithTimeInterval: differance target:self
selector: #selector(pushToSessionTableViewController:) userInfo: nil repeats: NO]fire];
});
});
dispatch_release(downloadQueue);
}
-(void) viewDidUnload{
[super viewDidUnload];
self.sessionsDataFromDatabase = nil;
}
-(void) pushToSessionTableViewController{
UISessionTable * obj = [[UISessionTable alloc]init ];
[obj setSessionsData:self.sessionsDataFromDatabase];
[self.navigationController pushViewController:obj animated:YES];
}
#end
i got the following error when run
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason:
'-[SplashViewController pushToSessionTableViewController:]: unrecognized selector sent
to instance 0x6e45740'
any suggestion ???

The colon at the end is for methods that receive parameters, yours doesn't receive anything. That's why it can't find the method (it assumes it is another undeclared method).
Replace
pushToSessionTableViewController:
with
pushToSessionTableViewController

Related

Objective-c calling a method from class method

I am trying to access an instance method from a class method. I am getting this error
+[ActiveVC goToDashBoard]: unrecognized selector sent to class 0x112010
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '+[ActiveVC goToDashBoard]:
unrecognized selector sent to class 0x112010'
My code
+ (void) removeClosedVisitor:(NSString *) visitorID{
for (NSInteger i = activelist.count - 1; i >= 0 ; i--) {
ActiveItemObject *item = [activelist objectAtIndex:i];
if ([visitorID isEqualToString:item.VisitorId]) {
NSLog(#"Removing Visitor from Active List -- %#", visitorID);
[activelist removeObjectAtIndex:i];
//[self.incommingTable reloadData];
// NSDictionary *activeDictionary = [[NSDictionary alloc] init];
// activeDictionary = [activelist mutableCopy];
//
// [[NSNotificationCenter defaultCenter]
// postNotificationName:#"PassData"
// object:nil
// userInfo:activeDictionary];
[[self class] goToDashBoard];
}
}
}
- (void) goToDashBoard{
NSLog(#"Segue to Dashboard");
UITabBarController *dvc = [self.storyboard instantiateViewControllerWithIdentifier:#"id_tabView"];
[dvc setModalTransitionStyle:UIModalTransitionStyleCoverVertical];
[self presentViewController:dvc animated:YES completion:nil];
}
can some one help me to fix this issue . tnx.
You need to create an instance of your class or convert your class to a singleton. For example: [[ActiveVC sharedInstance] goToDashBoard];
Here's how you create a Singleton Class:
First, create a New file and subclass it from NSObject. Name it anything, we will use CommonClass here. Xcode will now generate CommonClass.h and CommonClass.m files for you.
In your CommonClass.h file:
#import <Foundation/Foundation.h>
#interface CommonClass : NSObject {
}
+ (CommonClass *)sharedObject;
#property NSString *commonString;
#end
In your CommonClass.m File:
#import "CommonClass.h"
#implementation CommonClass
+ (CommonClass *)sharedObject {
static CommonClass *sharedClass = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedClass = [[self alloc] init];
});
return sharedClass;
}
- (id)init {
if (self = [super init]) {
self.commonString = #"this is string";
}
return self;
}
#end
If you want to call instance method then you will need an instance variable so create instance variable of this class and call it.
Make goToDashBoard a class method. Since you are not creating any instance here, if it is not a class method then it can't be executed.
+ (void) goToDashBoard
Do you actually have an instance anywhere? If not you will have to create one:
[self.sharedInstance goToDashBoard]
[[self alloc] init] goToDashBoard]
I assume you do have an instance, because its looks like its a view controller. In which case I suggest you pass the instance into the static method.
+ (void) removeClosedVisitor:(NSString *) visitorID viewController: (xxx) viewController {

App crashes when trying to load data from a singleton

I have ViewControllerA and ViewControllerB. In each I have this property
#property (retain, nonatomic) NSMutableArray *racersArray;
In ViewControllerA I'm filling the racersArray with custom objects. When I press the burger button on my ViewControllerA, I store my filled _racersArray to singleton array and then I send a notification to ViewControllerB that my _racesArray content is yet in singleton array. I use this method:
- (void)burgerMenu
{
[ArraySingleton sharedManager].sharedArray = _racersArray;
// Send a notification to burger menu view controller to reload it's tableview
[[NSNotificationCenter defaultCenter] postNotificationName:#"ReloadTableViewData" object:nil];
// Opens the burger menu
[self.frostedViewController presentMenuViewController];
}
In ViewControllerB when I receive the notification, I call this method:
- (void)reloadTableviewData
{
_racersArray = [ArraySingleton sharedManager].sharedArray;
[self.tableView reloadData];
}
But after I try to load the data from singleton array to _racersArray, my app crashes with error:
[Racer count]: unrecognized selector sent to instance 0x7fe46aef7330
2016-10-03 23:58:44.091 Stopwatch[67948:6727940] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Racer count]: unrecognized selector sent to instance 0x7fe46aef7330'
This is how my singleton looks
#synthesize sharedArray;
+ (ArraySingleton *)sharedManager
{
static ArraySingleton *sharedMyManager = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedMyManager = [[ArraySingleton alloc] init];
});
return sharedMyManager;
}
- (id)init
{
if (self = [super init]) {
sharedArray = [[NSMutableArray alloc] initWithObjects:#"picee", nil];
}
return self;
}
Can anyone tell me what am I doing wrong?
Thanks

App crashes with unrecognized selector sent to instance

This is the error Xcode shoy my:
2013-10-25 11:43:35.059 ChineseCheckers[7220:c07] -[HomeViewController
play:]: unrecognized selector sent to instance 0x8a2de20 2013-10-25
11:43:35.062 ChineseCheckers[7220:c07] * Terminating app due to
uncaught exception 'NSInvalidArgumentException', reason:
'-[HomeViewController play:]: unrecognized selector sent to instance
0x8a2de20'
* First throw call stack:
(0x1af7012 0x14a4e7e 0x1b824bd 0x1ae6bbc 0x1ae694e 0xeed2c0 0x1ab6376
0x1ab5e06 0x1a9da82 0x1a9cf44 0x1a9ce1b 0x278b7e3 0x278b668 0x3e8ffc
0x28fd 0x2def725 0x1) libc++abi.dylib: terminate called throwing an
exception (lldb) 0x8a2de20
I`m traying to put a background sound to my app , I put the sourece code
-(IBAction)play
{
NSString *soundFilePath =[[NSBundle mainBundle] pathForResource: #"larga"ofType: #"mp3"];
NSURL *fileURL = [[NSURL alloc] initFileURLWithPath: soundFilePath];
AVAudioPlayer *newPlayer =[[AVAudioPlayer alloc] initWithContentsOfURL: fileURL error: Nil];
self.player = newPlayer;
[self.player prepareToPlay];
[self.player setDelegate: self];
[self.player play];
[NSTimer scheduledTimerWithTimeInterval: 30 target: self
selector: #selector(play:) userInfo: nil repeats: YES];
}
- (void)viewDidLoad
{
[super viewDidLoad];
.............................
...............................
[self.player play];
}
With this:
[NSTimer scheduledTimerWithTimeInterval: 30 target: self
selector: #selector(play:) userInfo: nil repeats: YES];
You are calling the method play on your view controller, at that method doesn't exist.
Try creating it (remove the : from play: in the selector):
[NSTimer scheduledTimerWithTimeInterval: 30 target: self
selector: #selector(play) userInfo: nil repeats: YES];
-(void)play
{
[self.player play];
}

IOS - Terminating app due to uncaught exception 'NSInvalidArgumentException unrecognized selector sent to instance 0x7a9a2c0'

I'm very new to ios developing, and i don't really undertand yet the errors that console shows.
This is the error:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NVViewController Play:]: unrecognized selector sent to instance 0x7a9a2c0'
I put my code below, the error occurs when I touch the Play Button.
#import "NVViewController.h"
#implementation NVViewController
#synthesize reproductor;
- (void)viewDidLoad
{
[super viewDidLoad];
NSError* error;
NSString* ruta = [[NSBundle mainBundle] pathForResource:#"BackgroundMusic" ofType:#"mp3"];
NSURL* url = [[NSURL alloc] initFileURLWithPath:ruta];
self.reproductor = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
[self.reproductor prepareToPlay];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)playBackgroundMusic:(id)sender {
[self.reproductor play];
}
- (IBAction)stopBackgroundMusic:(id)sender {
[self.reproductor stop];
}
#end
and the header.
#interface NVViewController : UIViewController
#property (nonatomic,strong) AVAudioPlayer * reproductor;
- (IBAction)playBackgroundMusic:(id)sender;
- (IBAction)stopBackgroundMusic:(id)sender;
#end
Thanks.
Somewhere in your code (elsewhere), you called Play: on your view controller instead of playBackgroundMusic:. The error message clearly states that you sent Play: to an NVViewController instead which didn't understand that message.

Xcode: Invalid parameter not satisfying

I keep running into a pretty frustrating error in Xcode after implementing a date picker. The error in the debugger is: "Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid parameter not satisfying: date"
I've been going through my code for hours now, and can't find the issue. It may be because I'm not checking for nil, there is no date the first time the app installs and launches, so that may be causing the crash. If it is, how do I check for nil in this code? I'm still very new at programming, any help would be much appreciated. Here is the code:
#import "DatePickerViewController.h"
#implementation DatePickerViewController
#synthesize datePicker;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
// Initialization code
}
return self;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
// Release anything that's not essential, such as cached data
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"mm'/'dd'/'yyyy"];
NSDate *eventDate = [[NSUserDefaults standardUserDefaults] objectForKey:#"DatePickerViewController.selectedDate"];
localNotif.fireDate = [eventDate dateByAddingTimeInterval:-13*60*60];
localNotif.timeZone = [NSTimeZone defaultTimeZone];
localNotif.alertBody = #"Tomorrow!";
localNotif.alertAction = nil;
localNotif.soundName = UILocalNotificationDefaultSoundName;
localNotif.applicationIconBadgeNumber = 0;
[[UIApplication sharedApplication]presentLocalNotificationNow:localNotif];
return YES;
}
- (void)viewDidLoad {
NSDate *storedDate = [[NSUserDefaults standardUserDefaults]
objectForKey:#"DatePickerViewController.selectedDate"];
[self.datePicker setDate:storedDate animated:NO];
}
- (IBAction)dateChanged:(id)sender {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSDate *selectedDate = [self.datePicker date];
[defaults setObject:selectedDate forKey:#"DatePickerViewController.selectedDate"];
}
You don't check if date is null, before using it, in ex.
(void)viewDidLoad {
NSDate *storedDate = [[NSUserDefaults standardUserDefaults]
objectForKey:#"DatePickerViewController.selectedDate"];
// add this check and set
if (storedDate == nil) {
storedDate = [NSDate date];
}
// ---
[self.datePicker setDate:storedDate animated:NO];
}
I've find a Debug way may help you debug where is the exception occurred and may help someone to debug with exception.
navigate to breakpoint
Click the add button, and choose exception Breakpoint
Add breakpoint and make exception type to Objective-C
Run the code
It will stop at the line which make crash happened!
Hope this help~

Resources