NSEntityDescription *entity = [NSEntityDescription entityForName:#"Thread" inManagedObjectContext:managedObjectContext];
That line doesn't seem to work anymore (I'm pretty sure its that line).
I can't seem to work out whats the problem. The application worked perfectly on Xcode with iOS 4.1 and now crashes with this error in the console:
2010-12-07 17:12:27.552 SMSApp[9222:207] +[ persistentStoreCoordinator]: unrecognized selector sent to class 0x5b14580
2010-12-07 17:12:27.553 SMSApp[9222:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '+[ persistentStoreCoordinator]: unrecognized selector sent to class 0x5b14580'
*** Call stack at first throw:
(
0 CoreFoundation 0x01547be9 __exceptionPreprocess + 185
1 libobjc.A.dylib 0x0169c5c2 objc_exception_throw + 47
2 CoreFoundation 0x015497bb +[NSObject(NSObject) doesNotRecognizeSelector:] + 187
3 CoreFoundation 0x014b9366 ___forwarding___ + 966
4 CoreFoundation 0x014b8f22 _CF_forwarding_prep_0 + 50
5 CoreData 0x00e6f3ca +[NSEntityDescription entityForName:inManagedObjectContext:] + 42
6 SMSApp 0x000046b9 -[MessagesRootViewController reloadMessages:] + 146
7 SMSApp 0x00004a09 -[MessagesRootViewController viewDidLoad] + 85
8 UIKit 0x0052265e -[UIViewController view] + 179
9 UIKit 0x00520a57 -[UIViewController contentScrollView] + 42
10 UIKit 0x00531201 -[UINavigationController _computeAndApplyScrollContentInsetDeltaForViewController:] + 48
11 UIKit 0x0052f831 -[UINavigationController _layoutViewController:] + 43
12 UIKit 0x00530b4c -[UINavigationController _startTransition:fromViewController:toViewController:] + 524
13 UIKit 0x0052b606 -[UINavigationController _startDeferredTransitionIfNeeded] + 266
14 UIKit 0x00643e01 -[UILayoutContainerView layoutSubviews] + 226
15 QuartzCore 0x010b4451 -[CALayer layoutSublayers] + 181
16 QuartzCore 0x010b417c CALayerLayoutIfNeeded + 220
17 QuartzCore 0x010ad37c _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 310
18 QuartzCore 0x010ad0d0 _ZN2CA11Transaction6commitEv + 292
19 UIKit 0x0047719f -[UIApplication _reportAppLaunchFinished] + 39
20 UIKit 0x00477659 -[UIApplication _runWithURL:payload:launchOrientation:statusBarStyle:statusBarHidden:] + 690
21 UIKit 0x00481db2 -[UIApplication handleEvent:withNewEvent:] + 1533
22 UIKit 0x0047a202 -[UIApplication sendEvent:] + 71
23 UIKit 0x0047f732 _UIApplicationHandleEvent + 7576
24 GraphicsServices 0x01b2ca36 PurpleEventCallback + 1550
25 CoreFoundation 0x01529064 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 52
26 CoreFoundation 0x014896f7 __CFRunLoopDoSource1 + 215
27 CoreFoundation 0x01486983 __CFRunLoopRun + 979
28 CoreFoundation 0x01486240 CFRunLoopRunSpecific + 208
29 CoreFoundation 0x01486161 CFRunLoopRunInMode + 97
30 UIKit 0x00476fa8 -[UIApplication _run] + 636
31 UIKit 0x0048342e UIApplicationMain + 1160
32 SMSApp 0x000025c4 main + 102
33 SMSApp 0x00002555 start + 53
)
terminate called after throwing an instance of 'NSException'
Any idea where this error is coming from or what is causing it?
Just to let you know as well, upgrading to XCode with iOS 4.2 caused some of the files to be deleted from my project. I don't know why this was, whether it was because they were linked files and not actually in the folder I dunno.
Any ideas?
Thanks
// EDIT
This method is in the AppDelegate:
- (NSManagedObjectContext *)managedObjectContext {
if (managedObjectContext_ != nil) {
return managedObjectContext_;
}
NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
if (coordinator != nil) {
managedObjectContext_ = [[NSManagedObjectContext alloc] init];
[managedObjectContext_ setPersistentStoreCoordinator:coordinator];
}
return managedObjectContext_;
}
// EDIT AGAIN
- (void)reloadMessages:(NSNotification *)notification {
NSLog(#"RECIEVED NEW MESSAGES TO MessagesRootViewController");
NSLog(#"%#",managedObjectContext);
// we need to get the threads from the database...
NSFetchRequest *theReq = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:#"Thread" inManagedObjectContext:self.managedObjectContext];
[theReq setEntity:entity];
threads = [NSArray arrayWithArray:[managedObjectContext executeFetchRequest:theReq error:nil]];
[self.tableView performSelectorOnMainThread:#selector(reloadData) withObject:nil waitUntilDone:NO];
if(notification != nil){ // if its been caused by a notification
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"New Message" message:#"A new message has been received" delegate:nil cancelButtonTitle:#"Thanks" otherButtonTitles:nil];
[alert performSelectorOnMainThread:#selector(show) withObject:nil waitUntilDone:NO];
[alert release];
}
}
// EDIT 3
If I put this code in the applicationDidFinishLoading of my app delegate it works fine!
NSFetchRequest *theReq = [[NSFetchRequest alloc] init];
NSEntityDescription *theEntity = [NSEntityDescription entityForName:#"Thread" inManagedObjectContext:self.managedObjectContext];
[theReq setEntity:theEntity];
NSArray *theThreads = [NSArray arrayWithArray:[self.managedObjectContext executeFetchRequest:theReq error:nil]];
After having a quick look at your code I think I found a couple of parts that needed attention. I'll try and break them down below:
SMSAppAppDelegate
1) Notice that in your interface file, you declare your CoreData variables with an underscore at the end of the variable name. Then you create properties for each of the ivar without the underscore. That is all fine as long as you #synthesize them making the correct assignment, which seems to be missing from your implementation:
#synthesize managedObjectContext=managedObjectContext_;
#synthesize managedObjectModel=managedObjectModel_;
#synthesize persistentStoreCoordinator=persistentStoreCoordinator_;
DownloadContacts
1) In this class you are calling the managedObjectContext directly through the variable. I wouldn't recommend that but this is not your actual problem. Notice that you release the managedObjectContext on dealloc even though you have no ownership of it!
2) What I would do in this case is declare a property for the context and always access it via the property. Also if you rename your instance variable to include an underscore at the end you are minimising your chances of accessing it directly (i.e. without using self.) and it will make it easier for you to find where in this class you are actually doing that (there are a couple of places from memory).
So in your interface file, rename managedObjectContext ivar and declare a property:
NSManagedObjectContext *managedObjectContext_;
...
#property (nonatomic, retain) NSManagedObjectContext managedObjectContext;
And don't forget to make the assignment on #sythesize and release on dealloc, since you are using a retain property:
#synthesize managedObjectContext=managedObjectContext_;
...
[managedObjectContext_ release];
Then if you try and compile you will get two or three errors of unrecognised instance variable which is where you have to go and change it to self.managedObjectContext
MessagesRootViewController
And finally I'd recommend you go through the same process in your MessagesRootViewController but this one should still work fine as is!
See how you go and let me know if any of the above is not clear!
Cheers,
Rog
I'm assuming you're doing something like:
[ClassName persistentStoreCoordinator]
Make sure ClassName is defined and has the persistentStoreCoordinator class method. Perhaps the file that did these things got deleted?
Matt
Your database might be corrupted, try resetting the iOS simulator
by clicking on iOS Simulator>Reset contents and settings.
Edit:
It looks like you're trying to call a selector that doesn't exist. Try figuring out which files were deleted, as it seems like you have a method that is called that can't be found at runtime. Did your core data classes get erased, maybe?
Related
I am creating my first iPhone app in which the first screen displays a table view the user selects from. It works before localization using the method below.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
navigationController = [[UINavigationController alloc] init];
[self.window addSubview:[self.navigationController view]];
if(self.selectCategoryViewController == nil)
{
SelectCategoryViewController *viewTwo = [[SelectCategoryViewController alloc] initWithNibName:#"SelectCategoryViewController" bundle:[NSBundle mainBundle]];
self.selectCategoryViewController = viewTwo;
[viewTwo release];
}
[self.navigationController setNavigationBarHidden:YES];//this will hide the navigation bar
[self.navigationController pushViewController:self.selectCategoryViewController animated:YES];
//self.window.rootViewController = self.navigationController;
[self.window makeKeyAndVisible];
return YES;
}
After I localized it by adding the Japanese localization file and changing the above to this:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
navigationController = [[UINavigationController alloc] init];
[self.window addSubview:[self.navigationController view]];
if(self.selectCategoryViewController == nil)
{
NSBundle *myLocalizedBundle=[NSBundle bundleWithPath:[NSString stringWithFormat:[[NSBundle mainBundle]bundlePath],"en.lproj"]];
NSLog(#"the localized bundle is %#",myLocalizedBundle);
SelectCategoryViewController *viewTwo=[[SelectCategoryViewController alloc] initWithNibName:#"SelectCategoryViewController" bundle:myLocalizedBundle];
self.selectCategoryViewController = viewTwo;
[viewTwo release];
}
[self.navigationController setNavigationBarHidden:YES];//this will hide the navigation bar
[self.navigationController pushViewController:self.selectCategoryViewController animated:YES];
[self.window makeKeyAndVisible];
return YES;
}
it crashes with the following error:
√sh.app> (loaded)
2013-02-18 18:04:35.196 PictureEnglish[5529:207] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[NSCFString substringFromIndex:]: Range or index out of bounds'
*** Call stack at first throw:
(
0 CoreFoundation 0x012775a9 __exceptionPreprocess + 185
1 libobjc.A.dylib 0x013cb313 objc_exception_throw + 44
2 CoreFoundation 0x0122fef8 +[NSException raise:format:arguments:] + 136
3 CoreFoundation 0x0122fe6a +[NSException raise:format:] + 58
4 Foundation 0x00033086 -[NSString substringFromIndex:] + 133
5 PictureEnglish 0x00008524 -[SelectCategoryViewController viewDidLoad] + 937
6 UIKit 0x00378089 -[UIViewController view] + 179
7 UIKit 0x00376482 -[UIViewController contentScrollView] + 42
8 UIKit 0x00386f25 -[UINavigationController _computeAndApplyScrollContentInsetDeltaForViewController:] + 48
9 UIKit 0x00385555 -[UINavigationController _layoutViewController:] + 43
10 UIKit 0x00386870 -[UINavigationController _startTransition:fromViewController:toViewController:] + 524
11 UIKit 0x0038132a -[UINavigationController _startDeferredTransitionIfNeeded] + 266
12 UIKit 0x0049c2e9 -[UILayoutContainerView layoutSubviews] + 226
13 QuartzCore 0x010a7a5a -[CALayer layoutSublayers] + 181
14 QuartzCore 0x010a9ddc CALayerLayoutIfNeeded + 220
15 QuartzCore 0x0104f0b4 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 310
16 QuartzCore 0x01050294 _ZN2CA11Transaction6commitEv + 292
17 UIKit 0x002ca9c9 -[UIApplication _reportAppLaunchFinished] + 39
18 UIKit 0x002cae83 -[UIApplication _runWithURL:payload:launchOrientation:statusBarStyle:statusBarHidden:] + 690
19 UIKit 0x002d5617 -[UIApplication handleEvent:withNewEvent:] + 1533
20 UIKit 0x002cdabf -[UIApplication sendEvent:] + 71
21 UIKit 0x002d2f2e _UIApplicationHandleEvent + 7576
22 GraphicsServices 0x01bcf992 PurpleEventCallback + 1550
23 CoreFoundation 0x01258944 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 52
24 CoreFoundation 0x011b8cf7 __CFRunLoopDoSource1 + 215
25 CoreFoundation 0x011b5f83 __CFRunLoopRun + 979
26 CoreFoundation 0x011b5840 CFRunLoopRunSpecific + 208
27 CoreFoundation 0x011b5761 CFRunLoopRunInMode + 97
28 UIKit 0x002ca7d2 -[UIApplication _run] + 623
29 UIKit 0x002d6c93 UIApplicationMain + 1160
30 PictureEnglish 0x00001cec main + 102
31 PictureEnglish 0x00001c7d start + 53
32 ??? 0x00000001 0x0 + 1
)
terminate called after throwing an instance of 'NSException'
Any suggestions on how I should call the SelectCategoryViewController for the selected language?
This line looks wrong to me:
NSBundle *myLocalizedBundle=[NSBundle bundleWithPath:[NSString stringWithFormat:[[NSBundle mainBundle]bundlePath],"en.lproj"]];
You probably meant something like this:
NSString* path= [[NSBundle mainBundle] pathForResource:#"en" ofType:#"lproj"];
NSBundle* bundle:myLocalizedBundle = [NSBundle bundleWithPath:path];
SelectCategoryViewController *viewTwo = [[SelectCategoryViewController alloc] initWithNibName:#"SelectCategoryViewController" bundle:myLocalizedBundle];
This is discussed more here: Manually loading a different localized nib in iOs
Note that this is only required if you want to do an in-app language selection. Localization should typically use the iOS device's selected language, in which case all this is much more straightforward (you simply let iOS pick the bundle for you).
xcode 5:
i had to clean build, clean build folder,delete derived data, reset simulator, close simulator, close Xcode.
Open project again and it's working.
Problem - This is a problem of project path, I think [self.window makeWindowKeyVisible] can not be able to find a path to your first assigned controller...
Solution - It happens some time, when your project suddenly stopped work, So no worries - just shift your project to other path, or just change the name of folder where actually your project resides and it will started working fine....
It works for me successfully...
Im getting the following exception when attempting to merge a managed context (running on a background thread) with my main managed context (on mainthread). I cant seem to catch the exception in my own #try expression. Does anyone have any insight into this issue?
I'm using the default merge policy but im not sure this is correct - this issue is very intermittent - happens rarely but is causing my app to crash.
Exception Type: EXC_CRASH (SIGABRT)
Exception Codes: 0x00000000, 0x00000000
Crashed Thread: 0
Last Exception Backtrace:
0 CoreFoundation 0x37e3b8bf __exceptionPreprocess + 163
1 libobjc.A.dylib 0x319211e5 objc_exception_throw + 33
2 CoreData 0x344b7ea5 -[NSSQLiteStatement cachedSQLiteStatement] + 1
3 CoreData 0x344b774f -[NSSQLiteConnection prepareSQLStatement:] + 55
4 CoreData 0x3455b049 -[NSSQLChannel selectRowsWithCachedStatement:] + 61
5 CoreData 0x34586d63 newFetchedRowsForFetchPlan_MT + 783
6 CoreData 0x344bfb07 -[NSSQLCore newRowsForFetchPlan:] + 351
7 CoreData 0x34565011 -[NSSQLCore fetchRowForObjectID:] + 1005
8 CoreData 0x344d1a57 -[NSSQLCore newValuesForObjectWithID:withContext:error:] + 195
9 CoreData 0x344d0f83 _PFFaultHandlerLookupRow + 423
10 CoreData 0x3450e111 -[NSFaultHandler fulfillFault:withContext:] + 25
11 CoreData 0x34518999 -[NSManagedObject(_NSInternalMethods) _newPropertiesForRetainedTypes:andCopiedTypes:preserveFaults:] + 77
12 CoreData 0x345178ef -[NSManagedObject(_NSInternalMethods) _newAllPropertiesWithRelationshipFaultsIntact__] + 79
13 CoreData 0x345284db -[NSManagedObjectContext(_NSInternalChangeProcessing) _establishEventSnapshotsForObject:] + 47
14 CoreData 0x3452694b -[NSManagedObjectContext deleteObject:] + 155
15 CoreData 0x345238a1 -[NSManagedObjectContext _mergeChangesFromDidSaveDictionary:usingObjectIDs:] + 813
16 CoreData 0x34522c35 -[NSManagedObjectContext mergeChangesFromContextDidSaveNotification:] + 189
17 myapp 0x0008f0e9 0x8d000 + 8425
18 CoreFoundation 0x37d9a22b -[NSObject performSelector:withObject:] + 43
19 Foundation 0x31d75757 __NSThreadPerformPerform + 351
20 CoreFoundation 0x37e0fb03 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 15
21 CoreFoundation 0x37e0f2cf __CFRunLoopDoSources0 + 215
22 CoreFoundation 0x37e0e075 __CFRunLoopRun + 653
23 CoreFoundation 0x37d914dd CFRunLoopRunSpecific + 301
24 CoreFoundation 0x37d913a5 CFRunLoopRunInMode + 105
25 GraphicsServices 0x3790ffcd GSEventRunModal + 157
26 UIKit 0x35221743 UIApplicationMain + 1091
I init the background context in start() of a nsoperation like so:
AppDelegate *appController = [[UIApplication sharedApplication] delegate];
_managedObjectContext = [[NSManagedObjectContext alloc] init];
[appController setPersistentStore:_managedObjectContext];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(receivedDeletedObjects:) name:NSManagedObjectContextDidSaveNotification object:_managedObjectContext];
I also set up a notification event which is called when objects are deleted on the background managed context, the callback then does:
-(void)receivedDeletedObjects:(NSNotification *)note
{
AppDelegate *appController = [[UIApplication sharedApplication] delegate];
NSManagedObjectContext *mainContext = [self managedObjectContext];
[mainContext performSelectorOnMainThread:#selector(mergeChangesFromContextDidSaveNotification:) withObject:note waitUntilDone:NO];
}
Thats pretty much the code. I have 4 different background threads each with its own managed context doing the same thing merging with the main context in this manner. Im wondering multiple threads are getting into mergeChangesFromContextDidSaveNotification at the same time but this shouldnt be the case as it is always called on the mainthread.
How about this:
(all in AppDelegate, call freshContextForBackgroundTask from a background Thread and use save: to trigger the merge.) Note syncObj is a plain NSObject instance needed for synchronization within the app delegate when using many background threads (or just have bad luck with scheduling)
-(NSManagedObjectContext*) freshContextForBackgroundTask {
#synchronized(syncObj) {
NSManagedObjectContext* r = [[NSManagedObjectContext alloc] init];
[r setPersistentStoreCoordinator:self.managedObjectContext.persistentStoreCoordinator];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(backgroundContextDidSave:)
name:NSManagedObjectContextDidSaveNotification
object:r];
return r;
}
}
- (void)backgroundContextDidSave:(NSNotification *)notification {
/* Make sure we're on the main thread when updating the main context */
//NSLog(#"merging change: %#",notification);
dispatch_async(dispatch_get_main_queue(), ^{
NSManagedObjectContext *context = [self managedObjectContext];
// this for loop may not be needed for your purpose.
for(NSManagedObject *object in [[notification userInfo] objectForKey:NSUpdatedObjectsKey]) {
[[context objectWithID:[object objectID]] willAccessValueForKey:nil];
}
[self.managedObjectContext mergeChangesFromContextDidSaveNotification:notification];
});
}
Your approach seems strange. Especially when you set your app delegate's persistent store to the (nil) one of the freshly init'ed context.
I know there have been a ton of questions about the same thing, but so far I haven't been able to apply any solutions to my problem. And I still haven't figured out how to use Instruments.
I'm taking a basic tutorial for an iPhone app and just trying to tweak it slightly (I am new to Objective C). I want it to read from a plist with an array of dicts instead of an array of strings. The table initially displays the data correctly. However whenever I scroll the table up (and off the screen), I am getting Unrecognized Selector exceptions. Just populating employees with NSStrings works fine. I am lost.
Relevant portions of the ViewController:
#interface RootViewController : UITableViewController {
NSMutableArray *employees_;
}
#property (nonatomic, retain) NSMutableArray *employees;
#end
and
#implementation RootViewController
#synthesize employees=employees_;
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *path = [[NSBundle mainBundle] pathForResource:#"Employees" ofType:#"plist"];
NSMutableArray *empArray = [[NSMutableArray alloc] initWithContentsOfFile:path];
employees_ = [empArray valueForKey:#"name"];
[empArray release];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
// Configure the cell.
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.textLabel.text = [self.employees objectAtIndex:indexPath.row];//this is where it errors
return cell;
}
- (void)dealloc
{
[employees_ release];
[super dealloc];
}
#end
and plist:
array
dict
key name /key
string Employee One /string
key id /key
string T1234 /string
/dict
dict
key name /key
string Employee Two /string
key id /key
string T5678 /string
/dict
/array
Error that I received:
2011-10-18 20:02:44.313 MyApp[65148:bc03] -[NSCFString objectAtIndex:]: unrecognized selector sent to instance 0x689a050
2011-10-18 20:02:44.316 MyApp[65148:bc03] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSCFString objectAtIndex:]: unrecognized selector sent to instance 0x689a050'
*** Call stack at first throw:
(
0 CoreFoundation 0x00dc25a9 __exceptionPreprocess + 185
1 libobjc.A.dylib 0x00f16313 objc_exception_throw + 44
2 CoreFoundation 0x00dc40bb -[NSObject(NSObject) doesNotRecognizeSelector:] + 187
3 CoreFoundation 0x00d33966 ___forwarding___ + 966
4 CoreFoundation 0x00d33522 _CF_forwarding_prep_0 + 50
5 MyApp 0x00002a96 -[RootViewController tableView:cellForRowAtIndexPath:] + 326
6 UIKit 0x00089b98 -[UITableView(UITableViewInternal) _createPreparedCellForGlobalRow:withIndexPath:] + 634
7 UIKit 0x0007f4cc -[UITableView(UITableViewInternal) _createPreparedCellForGlobalRow:] + 75
8 UIKit 0x000948cc -[UITableView(_UITableViewPrivate) _updateVisibleCellsNow:] + 1561
9 UIKit 0x0008c90c -[UITableView layoutSubviews] + 242
10 QuartzCore 0x016aca5a -[CALayer layoutSublayers] + 181
11 QuartzCore 0x016aeddc CALayerLayoutIfNeeded + 220
12 QuartzCore 0x016540b4 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 310
13 QuartzCore 0x01655294 _ZN2CA11Transaction6commitEv + 292
14 QuartzCore 0x0165546d _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 99
15 CoreFoundation 0x00da389b __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 27
16 CoreFoundation 0x00d386e7 __CFRunLoopDoObservers + 295
17 CoreFoundation 0x00d011d7 __CFRunLoopRun + 1575
18 CoreFoundation 0x00d00840 CFRunLoopRunSpecific + 208
19 CoreFoundation 0x00d00761 CFRunLoopRunInMode + 97
20 GraphicsServices 0x00ffa1c4 GSEventRunModal + 217
21 GraphicsServices 0x00ffa289 GSEventRun + 115
22 UIKit 0x00022c93 UIApplicationMain + 1160
23 MyApp 0x00002249 main + 121
24 MyApp 0x000021c5 start + 53
)
terminate called throwing an exceptionCurrent language: auto; currently objective-c
(gdb)
There are two potential problems:
You need to make sure the call to employees_ = [empArray valueForKey:#"name"] is actually returning a NSArray
Once one is ruled out, and assuming you are not using ARC, your employees_ ivar is getting released before the table view gets a chance to configure itself. Try
employees_ = [[empArray valueForKey:#"name"] retain];
And then release employees_ in your viewDidUnload & dealloc methods.
Hard to tell it from the stack as it does say your ivar is a NSCFString but it could be just because it is referencing an invalid/garbage memory address. Based on your plist description though, the likely cause on on point #1.
-[NSCFString objectAtIndex:]: unrecognized selector
This means you tried to send the selector (i.e. call the method) objectAtIndex: on an instance of class NSCFString (which is the same as NSString). ("Next Step / Core Foundation String")
You can't do that.
Since you only are calling objectAtIndex: in one place in your above code, it's easy to see where the problem is. (Besides which in your debugger you should be able to see what line it happens on.) You're calling [self.employees objectAtIndex:...]. Obviously you expect self.employees to be an array, but it is a string instead. I don't really know about plist processing, so figuring out why employees got a string and how to make it get an array instead is up to you.
I have this crash. It is similar to other threads, but not the same.
I would like to show a modal view controller first the user goes to a specific view controller. Following the hints, I do that on - (void) viewDidAppear:(BOOL)animated, and apply a delay as I saw it's recommended.
- (void) viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[self performSelector:#selector(presentMyModal) withObject:nil afterDelay:1];
}
- (void) presentModal{
ModalViewController *modal = [[[ModalViewController alloc] init] autorelease];
[self presentModalViewController:modal animated:YES];
}
Afterwards, ramdomly It crashes. I get this message in console:
<Warning>: *** Assertion failure in -[UIWindowController transition:fromViewController:toViewController:target:didEndSelector:], /SourceCache/UIKit/UIKit-1447.6.4/UIWindowController.m:186
Thu Feb 3 10:00:44 unknown MyApp[1830] <Error>: *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Attempting to begin a modal transition from <UINavigationController: 0x454260> to <ModalViewController: 0x47af00> while a transition is already in progress. Wait for viewDidAppear/viewDidDisappear to know the current transition has completed'
*** Call stack at first throw:
(
0 CoreFoundation 0x3759dc7b __exceptionPreprocess + 114
1 libobjc.A.dylib 0x32d9bee8 objc_exception_throw + 40
2 CoreFoundation 0x3759dac3 +[NSException raise:format:arguments:] + 70
3 Foundation 0x351a3e73 -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 62
4 UIKit 0x359e92a8 -[UIWindowController transition:fromViewController:toViewController:target:didEndSelector:] + 208
5 UIKit 0x359e8c98 -[UIViewController presentModalViewController:withTransition:] + 2792
6 UIKit 0x35a7b51c -[UIViewController _tryRecursivelyPresentModalViewController:withTransition:] + 116
7 UIKit 0x359e84c0 -[UIViewController presentModalViewController:withTransition:] + 784
8 UIKit 0x359e8060 -[UIViewController presentModalViewController:animated:] + 96
9 MyApp 0x0005d57f -[MyAppViewController presentMyModal] + 58
10 Foundation 0x351724db __NSFireDelayedPerform + 366
11 CoreFoundation 0x37552305 __CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__ + 16
12 CoreFoundation 0x37551cd9 __CFRunLoopDoTimer + 988
13 CoreFoundation 0x37521a91 __CFRunLoopRun + 1184
14 CoreFoundation 0x3752150b CFRunLoopRunSpecific + 226
15 CoreFoundation 0x37521419 CFRunLoopRunInMode + 60
16 GraphicsServices 0x33e76d24 GSEventRunModal + 196
17 UIKit 0x3591d57c -[UIApplication _run] + 588
18 UIKit 0x3591a558 UIApplicationMain + 972
19 MyApp 0x0000e75f main + 50
20 MyApp 0x0000e6e8 start + 52
As you can see, I wait until view is appeared. Is this maybe an OS bug? It seems like it tries to present recursively other modal view controllers, provoquing crashes.
Thanks a lot.
Apart from me being picky in the comment, I thought I could as well help with this one as well. I think you need to search for the culprit somewhere else. I created a new project and a view controller with this snippet:
#import "VC1.h"
#implementation VC1
- (void) viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[self performSelector:#selector(presentModal) withObject:nil afterDelay:1.0];
}
- (void)presentModal {
static int colorChooser = 0;
VC1 *vc1 = [[[VC1 alloc] init] autorelease];
switch (colorChooser%2) {
case 0:
vc1.view.backgroundColor = [UIColor whiteColor];
break;
default:
vc1.view.backgroundColor = [UIColor blackColor];
break;
}
colorChooser++;
[self presentModalViewController:vc1 animated:YES];
}
#end
And it works flawlessly after being pushed on the navigation controller. It is recursively alternating between black and white views, tested both in the sim and on a 3G device.
Maybe you are doing some other view transitions due to some notifications or other asynchronic means? Either way you would need to share more of your code for anyone to tell where the problem is.
Our preferred solution is to use -[UIViewController presentViewController:animated:completion:] and do whatever the next action is (eg presenting another VC) in the completion block.
For example:
[self presentViewController:yourViewController animated:YES completion:^{
[yourViewController presentMyModal];
}];
This method was introduced in iOS 5.0.
I have a problem here. Or Maybe I'm really tired...
I have a class :
#interface THECLASS : UIViewController <UITableViewDelegate> {
NSMutableArray* param;
}
#property(nonatomic, retain) NSMutableArray* param;
Inside that class I have a method that is called when the user clicks on a UISwitch inside a tableViewCell (I build the params into a IBAction method that is not shwon here) :
#synthesize param;
- (void) changedSelectorValue:(NSIndexPath*)indexPath isOn:(BOOL)isOn {
[self.param addObject:#"eeeee"];
}
This crashes the app with the following log :
2011-02-04 01:31:02.548 Learning Project[3895:207] -[__NSArrayI addObject:]: unrecognized selector sent to instance 0x630c960
2011-02-04 01:31:02.549 Learning Project[3895:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayI addObject:]: unrecognized selector sent to instance 0x630c960'
*** Call stack at first throw:
(
0 CoreFoundation 0x00fc4be9 __exceptionPreprocess + 185
1 libobjc.A.dylib 0x011195c2 objc_exception_throw + 47
2 CoreFoundation 0x00fc66fb -[NSObject(NSObject) doesNotRecognizeSelector:] + 187
3 CoreFoundation 0x00f36366 ___forwarding___ + 966
4 CoreFoundation 0x00f35f22 _CF_forwarding_prep_0 + 50
5 Learning Project 0x00019463 -[ChoixJoursDisponibiliteController changedSelectorValue:isOn:] + 644
6 Learning Project 0x000190db -[ChoixJoursDisponibiliteController clickChangedSelectorValue:] + 307
7 UIKit 0x002f6a6e -[UIApplication sendAction:to:from:forEvent:] + 119
8 UIKit 0x003851b5 -[UIControl sendAction:to:forEvent:] + 67
9 UIKit 0x00387647 -[UIControl(Internal) _sendActionsForEvents:withEvent:] + 527
10 UIKit 0x004c9c6d -[UISwitch _onAnimationDidStop:finished:context:] + 201
11 UIKit 0x00327665 -[UIViewAnimationState sendDelegateAnimationDidStop:finished:] + 294
12 UIKit 0x003274f7 -[UIViewAnimationState animationDidStop:finished:] + 77
13 QuartzCore 0x01eab6cb _ZL23run_animation_callbacksdPv + 278
14 QuartzCore 0x01eab589 _ZN2CAL14timer_callbackEP16__CFRunLoopTimerPv + 157
15 CoreFoundation 0x00fa5fe3 __CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__ + 19
16 CoreFoundation 0x00fa7594 __CFRunLoopDoTimer + 1220
17 CoreFoundation 0x00f03cc9 __CFRunLoopRun + 1817
18 CoreFoundation 0x00f03240 CFRunLoopRunSpecific + 208
19 CoreFoundation 0x00f03161 CFRunLoopRunInMode + 97
20 GraphicsServices 0x018f9268 GSEventRunModal + 217
21 GraphicsServices 0x018f932d GSEventRun + 115
22 UIKit 0x0030542e UIApplicationMain + 1160
23 Learning Project 0x00002580 main + 102
24 Learning Project 0x00002511 start + 53
25 ??? 0x00000001 0x0 + 1
)
terminate called after throwing an instance of 'NSException'
param is filled from a caller view by something like :
NSMutableArray* filledArray = [NSMutableArray arrayWithObjects:#"SOME TEXT", nil]
nextController.param = filledArray;
NSLog(#"%#", self.param); just before the crash gives :
2011-02-04 02:01:17.205 Learning Project[4223:207] (
"JOURDISPO_MARDI"
)
Why does this crash ?
The array is there, filled and not nil... It seems well defined... addObject is a NSMutableArray method... I don't understand
I don't show it here but there is the same log with removeObject.
Answer is buried in the comments to the "accepted" answer. I came here from Google and it fixed my problem, so ... to make it clearer, based on #e.James's comment:
When you implement the "NSCopying" protocol, and implement
"copyWithZone", you MUST NOT use "copy" on your internal mutable arrays - this
DOES NOT copy the array (instead, it creates a non-mutable copy).
e.g. from my own code:
// Class: MyClass
#property(nonatomic, retain) NSMutableArray* mutArray;
-(id)copyWithZone:(NSZone *)zone
{
MyClass* other = [[MyClass alloc] init];
// other.mutArray = [self.mutArray copy]; // WRONG!
other.mutArray = [self.mutArray mutableCopy]; // CORRECT!
return other;
}
For some reason you're calling the addObject: method on an instance of NSArray, as the stack trace indicates. Perhaps the synthesized getter of your param object returns an NSArray? Don't use the getter in your method:
- (void) changedSelectorValue:(NSIndexPath*)indexPath isOn:(BOOL)isOn {
[param addObject:#"eeeee"];
}
Ultimately, the error is correct. You're calling a mutating method on an immutable object. Strange enough, in my testing this doesn't cause a compiler error or warning:
NSMutableArray *array = [[NSArray alloc] init];
Edit: Whether you want to believe it or not, somehow the param ivar is being set to an instance of an NSArray. Override the setter:
- (void)setParam:(NSMutableArray *)p {
if (param != p) {
[param release];
param = [p retain];
}
}
Then add a breakpoint on that method, and check to see when an NSArray is being passed in.
Your property is defined as retain, and you say that param is "a filled array from the caller view". This could be your problem.
For example, if you do the following:
NSArray * filledArray = [NSArray arrayWithObjects:..., nil];
theClassInstance.param = filledArray;
You will have retained a non-mutable array.
Edit: To debug the setting of param, you could do the following in your .m:
#dynamic param;
- (NSMutableArray*)param { return param; }
- (void)setParam:(NSMutableArray*)newArray {
NSLog(#"setting new param from class: %#",
NSStringFromClass([newArray class]));
[newArray retain];
[param release];
param = newArray;
}
What does your init method look like? It should look something like this...
- (id)init
{
self = [super init];
self.param = [NSMutableArray array];
return self;
}
After struggling a lot i found the solution for my problem. In my case the problem was,
I have a mutable dictionary and array
#property(strong,nonatomic)NSMutableDictionary *dictInfo;
#property(retain,nonatomic) NSMutableArray *userAccountsList;
And I am adding server array list to userAccountsList array like this
self.dictInfo = [jsonObject valueForKey:#"customerAccountList"];
I have taken another array and adding server array list to that array
array = [self.dictInfo valueForKey:#"banker"];
and finally adding array to userAccountsListArray like this
userAccountsList = [NSMutableArray arrayWithArray:array];
And here adding additional object to array
[userAccountsList addObject:#"Other Account"];//I was getting error here.
[tableViewObj reloadData];
Hope it helps someone. Please vote up if it helps.