Typing in UISearchBar crashes my app - objective-c

UPDATE 2
My app is crashing when presenting it modally after a user taps a button in ViewController1. In my storyboard, I have a standard present modally segue set to pop up the UINavController/UITableViewController containing the UISearchBar. That's failing every time.
However, in my AppDelegate, if I set the window's rootViewController to the same UINavController/UITableViewController, everything works as expected.
For some reason transitioning via segue and then acting the UISearchBar is causing the crash.
UPDATE 1
I'm now receiving the following error when tapping one letter:
2015-02-03 12:23:35.262 Afar D[28348:2740681] -[NSNull length]: unrecognized selector sent to instance 0x10e352ce0
2015-02-03 12:23:40.313 Afar D[28348:2740681] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSNull length]: unrecognized selector sent to instance 0x10e352ce0'
*** First throw call stack:
(
0 CoreFoundation 0x000000010e0a9f35 __exceptionPreprocess + 165
1 libobjc.A.dylib 0x000000010dd42bb7 objc_exception_throw + 45
2 CoreFoundation 0x000000010e0b104d -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
3 CoreFoundation 0x000000010e00927c ___forwarding___ + 988
4 CoreFoundation 0x000000010e008e18 _CF_forwarding_prep_0 + 120
5 CoreFoundation 0x000000010df89a7b CFStringCompareWithOptionsAndLocale + 219
6 Foundation 0x000000010d6822b7 -[NSString compare:options:range:] + 29
7 UIKit 0x000000010c5f53b4 -[UIPhysicalKeyboardEvent _matchesKeyCommand:] + 224
8 UIKit 0x000000010c53c12e -[UIResponder(Internal) _keyCommandForEvent:] + 285
9 UIKit 0x000000010c53c197 -[UIResponder(Internal) _keyCommandForEvent:] + 390
10 UIKit 0x000000010c53c197 -[UIResponder(Internal) _keyCommandForEvent:] + 390
11 UIKit 0x000000010c53c197 -[UIResponder(Internal) _keyCommandForEvent:] + 390
12 UIKit 0x000000010c53c197 -[UIResponder(Internal) _keyCommandForEvent:] + 390
13 UIKit 0x000000010c53c197 -[UIResponder(Internal) _keyCommandForEvent:] + 390
14 UIKit 0x000000010c53c197 -[UIResponder(Internal) _keyCommandForEvent:] + 390
15 UIKit 0x000000010c53c197 -[UIResponder(Internal) _keyCommandForEvent:] + 390
16 UIKit 0x000000010c3d8f0a -[UIApplication _handleKeyUIEvent:] + 126
17 UIKit 0x000000010c5c7fcc -[UIKeyboardImpl _handleKeyEvent:executionContext:] + 66
18 UIKit 0x000000010c75bbb7 -[UIKeyboardLayoutStar completeRetestForTouchUp:timestamp:interval:executionContext:] + 3611
19 UIKit 0x000000010c75a8e5 -[UIKeyboardLayoutStar touchUp:executionContext:] + 1374
20 UIKit 0x000000010c5d531b __28-[UIKeyboardLayout touchUp:]_block_invoke + 242
21 UIKit 0x000000010cb23914 -[UIKeyboardTaskQueue continueExecutionOnMainThread] + 332
22 UIKit 0x000000010c5d521c -[UIKeyboardLayout touchUp:] + 252
23 UIKit 0x000000010c5d5cc6 -[UIKeyboardLayout touchesEnded:withEvent:] + 319
24 UIKit 0x000000010c40b308 -[UIWindow _sendTouchesForEvent:] + 735
25 UIKit 0x000000010c40bc33 -[UIWindow sendEvent:] + 683
26 UIKit 0x000000010c3d89b1 -[UIApplication sendEvent:] + 246
27 UIKit 0x000000010c3e5a7d _UIApplicationHandleEventFromQueueEvent + 17370
28 UIKit 0x000000010c3c1103 _UIApplicationHandleEventQueue + 1961
29 CoreFoundation 0x000000010dfdf551 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
30 CoreFoundation 0x000000010dfd541d __CFRunLoopDoSources0 + 269
31 CoreFoundation 0x000000010dfd4a54 __CFRunLoopRun + 868
32 CoreFoundation 0x000000010dfd4486 CFRunLoopRunSpecific + 470
33 GraphicsServices 0x000000010fc109f0 GSEventRunModal + 161
34 UIKit 0x000000010c3c4420 UIApplicationMain + 1282
35 My App 0x000000010a5e1c63 main + 115
36 libdyld.dylib 0x000000010e75a145 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
I'm using iOS8's new UISearchController to manage a UISearchBar for filtering data in a table. If I activate the UISearchBar, the app crashes as soon as I type the first letter (Interestingly, the on-screen keyboard doesn't appear in the Simulator, but I don't think that's related).
In order to narrow things down, I've commented almost everything out of my UIViewController so that the UITableView renders nothing. I've implemented UISearchControllerDelegate and UISearchBarDelegate just so I can log when each method gets called. However, after typing the first letter, I receive no console messages. I don't even know where I could set a breakpoint.
Here's the crash I get:
-[NSNull length]: unrecognized selector sent to instance 0x107602ce0
Note that nowhere in my file am I calling length on anything. Something behind the scenes is, but I can't figure out what.
Here's my code to set up the UISearchController:
- (void)viewDidLoad
{
[super viewDidLoad];
self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
self.searchController.searchResultsUpdater = self;
self.searchController.definesPresentationContext = true;
self.searchController.hidesNavigationBarDuringPresentation = false;
[self.searchController.searchBar sizeToFit];
self.tableView.tableHeaderView = self.searchController.searchBar;
self.searchController.searchBar.delegate = self;
self.searchController.delegate = self;
}
By request, here are my delegate methods (implemented just to see if anything gets called after typing the first letter - they don't):
#pragma mark - UISearchResultsUpdating
- (void)updateSearchResultsForSearchController:(UISearchController *)searchController
{
NSLog(#"%s", __PRETTY_FUNCTION__);
}
#pragma mark - UISearchControllerDelegate
- (void)didDismissSearchController:(UISearchController *)searchController
{
NSLog(#"%s", __PRETTY_FUNCTION__);
}
- (void)didPresentSearchController:(UISearchController *)searchController
{
NSLog(#"%s", __PRETTY_FUNCTION__);
}
- (void)presentSearchController:(UISearchController *)searchController
{
NSLog(#"%s", __PRETTY_FUNCTION__);
}
- (void)willDismissSearchController:(UISearchController *)searchController
{
NSLog(#"%s", __PRETTY_FUNCTION__);
}
- (void)willPresentSearchController:(UISearchController *)searchController
{
NSLog(#"%s", __PRETTY_FUNCTION__);
}
#pragma mark - UISearchBarDelegate
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
NSLog(#"%s", __PRETTY_FUNCTION__);
}
- (BOOL)searchBar:(UISearchBar *)searchBar shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
NSLog(#"%s", __PRETTY_FUNCTION__);
return true;
}
- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar
{
NSLog(#"%s", __PRETTY_FUNCTION__);
return true;
}
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{
NSLog(#"%s", __PRETTY_FUNCTION__);
}
- (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar
{
NSLog(#"%s", __PRETTY_FUNCTION__);
return true;
}
- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar
{
NSLog(#"%s", __PRETTY_FUNCTION__);
}

NSNull is just a singleton object used to represent null. It is traditionally used to wrap around nil values in containers(arrays and dictionaries) that cannot handle nil entries.
This error often comes up when parsing json and trying to add the result in a collection.
Are you doing something like this in your delegate method(s)?
Edit: Judging by the message you are sending to that NSNull (length), you probably get an "empty" result for a string you asked for and expected not be nil.

After burning an entire day struggling with this, it came down to a totally unrelated problem: a corrupted storyboard.
This SO question recommended finding which scene was corrupt and recreating it. Unfortunately there was no way for me to determine that, so after some trial and error, it ended up being the initial view controller in my storyboard, which happened to be a UINavigationController. I deleted that, added it back in, and everything is golden.

Related

UIWebDocumentView _updateSubviewCaches crash in iOS10

I am getting the following crash in HockeyApp more seriously in iOS10. Please find the crash log as given below.
Thread 4 Crashed:
0 libobjc.A.dylib 0x0000000187242f30 objc_msgSend + 16
1 UIKit 0x000000018e86e914 -[UIWebDocumentView _updateSubviewCaches] + 36
2 UIKit 0x000000018e69093c -[UIWebDocumentView subviews] + 88
3 UIKit 0x000000018e941bd4 -[UIView(CALayerDelegate) _wantsReapplicationOfAutoLayoutWithLayoutDirtyOnEntry:] + 68
4 UIKit 0x000000018e63d770 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 1248
5 QuartzCore 0x000000018bb0640c -[CALayer layoutSublayers] + 144
6 QuartzCore 0x000000018bafb0e8 CA::Layer::layout_if_needed(CA::Transaction*) + 288
7 QuartzCore 0x000000018bafafa8 CA::Layer::layout_and_display_if_needed(CA::Transaction*) + 28
8 QuartzCore 0x000000018ba77c64 CA::Context::commit_transaction(CA::Transaction*) + 248
9 QuartzCore 0x000000018ba9f0d0 CA::Transaction::commit() + 508
10 QuartzCore 0x000000018ba9faf0 CA::Transaction::observer_callback(__CFRunLoopObserver*, unsigned long, void*) + 116
11 CoreFoundation 0x00000001887a57dc __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 28
12 CoreFoundation 0x00000001887a340c __CFRunLoopDoObservers + 368
13 CoreFoundation 0x00000001886d2068 CFRunLoopRunSpecific + 472
14 WebCore 0x000000018d273a2c RunWebThread(void*) + 452
15 libsystem_pthread.dylib 0x000000018788b860 _pthread_body + 236
16 libsystem_pthread.dylib 0x000000018788b770 _pthread_start + 280
17 libsystem_pthread.dylib 0x0000000187888dbc thread_start + 0
Any idea what is going on here ?
Seems like the following crash groups are also related.
Crash Group 1
[UIWebBrowserView _collectAdditionalSubviews]
objc_msgSend() selector name: autorelease
Crash Group 2
[UIWebDocumentView subviews]
objc_msgSend() selector name: arrayByAddingObjectsFromArray:
I am answering to my own question, since I am able to replicate this issue and found the root cause.
Please note the following.
1. I am using a UIWebView. First of all, this is not recommended.
2. The following way of implementation causes this issue. I am writing pseudo-code here to demonstrate the problem.
#implementation MyViewController
- (void)loadView {
//creating UIWebView Here
self.webView = [[UIWebView alloc] initWithFrame:_some_frame];
//setting the web view properties here.
//Adding to the view
[self.view addSubView: self.webView];
[self populateWebView];
}
- (void) populateWebView {
[self.webView loadHTMLString:_some_html
baseURL:[NSURL fileURLWithPath:_some_path]];
}
- (void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
if (self.webView) {
//The following two lines causing this issue.
self.webView.scrollView.contentInset = _some_insets;
self.webView.scrollView.scrollIndicatorInsets = _some_insets;
}
}
#end
3. The solution is given below.
#implementation MyViewController
- (void)loadView {
//creating UIWebView Here
self.webView = [[UIWebView alloc] initWithFrame:_some_frame];
//setting the web view properties here.
//Adding to the view
[self.view addSubView: self.webView];
[self populateWebView];
}
- (void) populateWebView {
[self.webView loadHTMLString:_some_html
baseURL:[NSURL fileURLWithPath:_some_path]];
}
- (void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView {
webView.scrollView.contentInset = _some_insets;
webView.scrollView.scrollIndicatorInsets = _some_insets;
}
#end
In my actual code, I was using a custom WebView class here by subclassing the UIWebView. Not think that will create some issue. The root cause is setting the "contentInset" and "scrollIndicatorInsets" in viewDidLayoutSubviews which is not a good idea. When I put the break points, "viewDidLayoutSubviews" called several times and eventually App crashes.
As a solution I moved the following part of the code into "webViewDidFinishLoad" which will call only when the WebView is ready after finished loading. So it makes sense to add this code under this delegate method.
webView.scrollView.contentInset = _some_insets;
webView.scrollView.scrollIndicatorInsets = _some_insets;

Error using AVAudioRecorder

I'm making this voice recording app, but for some reason it won't let me use this delete method without crashing:
-(void)deleteCurrentFiles {
if(recorder != nil) {
if([recorder isRecording]) {
[recorder stop];
}
[recorder release];
recorder = nil;
[self performSelector:#selector(resetTotalTimeLabel) withObject:nil afterDelay:1.0];
}
if(player != nil) {
if([player isPlaying]) {
[player stop];
}
[player release];
player = nil;
currentTime = 0;
timeSlider.value = 0;
[self performSelector:#selector(resetCurrentTimeLabel) withObject:nil afterDelay:0.1];
}
if(commentRecorder != nil) {
if([commentRecorder isRecording]) {
[commentRecorder stop];
}
}
}
The declarations of these instances:
AVAudioRecorder *recorder;
AVAudioRecorder *commentRecorder;
AVAudioPlayer *player;
In resetTotalTimeLabel and resetCurrentTimeLabel there is no referance to / usage of the recorders/player.
The error I get is:
-[__NSArrayI finishedRecording]: unrecognized selector sent to instance 0x1b3ba0
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayI finishedRecording]: unrecognized selector sent to instance 0x1b3ba0'
*** Call stack at first throw:
(
0 CoreFoundation 0x35f08c7b __exceptionPreprocess + 114
1 libobjc.A.dylib 0x30186ee8 objc_exception_throw + 40
2 CoreFoundation 0x35f0a3e3 -[NSObject(NSObject) doesNotRecognizeSelector:] + 98
3 CoreFoundation 0x35eaf467 ___forwarding___ + 506
4 CoreFoundation 0x35eaf220 _CF_forwarding_prep_0 + 48
5 CoreFoundation 0x35ea3f79 -[NSObject(NSObject) performSelector:withObject:] + 24
6 Foundation 0x33fd3e6d __NSThreadPerformPerform + 272
7 CoreFoundation 0x35ebc8d1 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 14
8 CoreFoundation 0x35e8cecd __CFRunLoopDoSources0 + 384
9 CoreFoundation 0x35e8c6f9 __CFRunLoopRun + 264
10 CoreFoundation 0x35e8c50b CFRunLoopRunSpecific + 226
11 CoreFoundation 0x35e8c419 CFRunLoopRunInMode + 60
12 GraphicsServices 0x35261d24 GSEventRunModal + 196
13 UIKit 0x3386557c -[UIApplication _run] + 588
14 UIKit 0x33862558 UIApplicationMain + 972
15 App Name 0x00002959 main + 80
16 App Name 0x00002904 start + 40
)
terminate called after throwing an instance of 'NSException'
It actually seems to happen to the recorder after this method is called.. So is there anything I should add to this method to make it work?
Any thoughts are greatly appriciated!
sending a message to an object which does not respond to it at a strange point in time is a good indication of a reference count imbalance. run your app with zombies enabled in Instruments. reproduce the crash and see if it is a zombie.

Simple document app, throwing an exception on "revert to saved" in Mac OS X 10.7 Lion

I have a simple document app, based on the document template, which is throwing an exception when the "Revert to Saved -> Last Opened Version" menu item is selected on Mac OS X 10.7.
The exception isn't in my code, it's inside Cocoa. It also doesn't appear to be related to anything I'm doing. My app is very simple (at this stage), almost a vanilla cocoa document based app, based on the template included with the latest stable version of Xcode.
I realise this is probably a bug in Lion, but I need to find a workaround.
See below for the exception, and the entire contents (very small) of my NSDocument subclass.
Steps to reproduce
open any document
type a single character into the text view
select File -> Revert to Saved
click Last Opened Version
Exception
2011-09-04 07:10:29.182 myapp[15433:707] *** -[NSPathStore2 stringByAppendingPathExtension:]: nil argument
2011-09-04 07:10:29.191 myapp[15433:707] (
0 CoreFoundation 0x00007fff89c2b986 __exceptionPreprocess + 198
1 libobjc.A.dylib 0x00007fff90c6ed5e objc_exception_throw + 43
2 CoreFoundation 0x00007fff89c2b7ba +[NSException raise:format:arguments:] + 106
3 CoreFoundation 0x00007fff89c2b744 +[NSException raise:format:] + 116
4 Foundation 0x00007fff86d2b172 -[NSPathStore2 stringByAppendingPathExtension:] + 112
5 AppKit 0x00007fff9148f8c3 -[NSDocument _preserveCurrentVersionForReason:error:] + 579
6 AppKit 0x00007fff9147655b __-[NSDocument _revertToVersion:preservingFirst:error:]_block_invoke_3 + 99
7 Foundation 0x00007fff86ef28c3 __-[NSFileCoordinator coordinateReadingItemAtURL:options:error:byAccessor:]_block_invoke_1 + 113
8 Foundation 0x00007fff86ef2f34 -[NSFileCoordinator(NSPrivate) _invokeAccessor:orDont:thenRelinquishAccessClaimForID:] + 202
9 Foundation 0x00007fff86d98a28 -[NSFileCoordinator(NSPrivate) _coordinateReadingItemAtURL:options:error:byAccessor:] + 663
10 Foundation 0x00007fff86ef284c -[NSFileCoordinator coordinateReadingItemAtURL:options:error:byAccessor:] + 79
11 AppKit 0x00007fff91484b41 __-[NSDocument _revertToVersion:preservingFirst:error:]_block_invoke_1 + 347
12 AppKit 0x00007fff914901e3 -[NSDocument performSynchronousFileAccessUsingBlock:] + 42
13 AppKit 0x00007fff9148fc90 -[NSDocument _revertToVersion:preservingFirst:error:] + 125
14 AppKit 0x00007fff91476cf9 -[NSDocument _revertToDiscardRecentChangesPreservingFirst:error:] + 43
15 AppKit 0x00007fff91477094 __-[NSDocument _revertToDiscardRecentChangesThenContinue:]_block_invoke_3 + 164
16 AppKit 0x00007fff91474851 __-[NSDocument performSynchronousFileAccessUsingBlock:]_block_invoke_1 + 19
17 AppKit 0x00007fff91475bda -[NSDocument continueFileAccessUsingBlock:] + 227
18 AppKit 0x00007fff91490413 -[NSDocument _performFileAccessOnMainThread:usingBlock:] + 466
19 AppKit 0x00007fff9149023f -[NSDocument performSynchronousFileAccessUsingBlock:] + 134
20 AppKit 0x00007fff91495891 __-[NSDocument _revertToDiscardRecentChangesThenContinue:]_block_invoke_2 + 301
21 AppKit 0x00007fff914909a9 -[NSDocument _something:wasPresentedWithResult:soContinue:] + 21
22 AppKit 0x00007fff91381bee -[NSAlert didEndAlert:returnCode:contextInfo:] + 93
23 AppKit 0x00007fff9138e356 -[NSApplication endSheet:returnCode:] + 275
24 AppKit 0x00007fff91381ab4 -[NSAlert buttonPressed:] + 265
25 CoreFoundation 0x00007fff89c1b11d -[NSObject performSelector:withObject:] + 61
26 AppKit 0x00007fff911dd852 -[NSApplication sendAction:to:from:] + 139
27 AppKit 0x00007fff911dd784 -[NSControl sendAction:to:] + 88
28 AppKit 0x00007fff911dd6af -[NSCell _sendActionFrom:] + 137
29 AppKit 0x00007fff911dcb7a -[NSCell trackMouse:inRect:ofView:untilMouseUp:] + 2014
30 AppKit 0x00007fff9125c57c -[NSButtonCell trackMouse:inRect:ofView:untilMouseUp:] + 489
31 AppKit 0x00007fff911db786 -[NSControl mouseDown:] + 786
32 AppKit 0x00007fff911a666e -[NSWindow sendEvent:] + 6280
33 AppKit 0x00007fff9113ef19 -[NSApplication sendEvent:] + 5665
34 AppKit 0x00007fff910d542b -[NSApplication run] + 548
35 AppKit 0x00007fff9135352a NSApplicationMain + 867
36 myapp 0x00000001000018d2 main + 34
37 myapp 0x00000001000018a4 start + 52
)
NSDocument Subclass
#implementation MyTextDocument
#synthesize textStorage;
#synthesize textView;
+ (BOOL)autosavesInPlace
{
return YES;
}
- (id)init
{
self = [super init];
if (self) {
self.textStorage = nil;
self.textView = nil;
textContentToLoad = [#"" retain];
}
return self;
}
- (NSString *)windowNibName
{
return #"MyTextDocument";
}
- (void)windowControllerDidLoadNib:(NSWindowController *)aController
{
[super windowControllerDidLoadNib:aController];
self.textStorage = self.textView.textStorage;
[self loadTextContentIntoStorage];
}
- (NSData *)dataOfType:(NSString *)typeName error:(NSError **)outError
{
return [self.textStorage.string dataUsingEncoding:NSUTF8StringEncoding];
}
- (BOOL)readFromData:(NSData *)data ofType:(NSString *)typeName error:(NSError **)outError
{
textContentToLoad = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
if (!textContentToLoad) {
*outError = [NSError errorWithDomain:NSCocoaErrorDomain code:NSFileReadUnknownError userInfo:nil];
return NO;
}
[self loadTextContentIntoStorage];
return YES;
}
- (void)loadTextContentIntoStorage
{
if (!self.textStorage || !textContentToLoad)
return;
[self.textStorage beginEditing];
[self.textStorage replaceCharactersInRange:NSMakeRange(0, self.textStorage.length) withString:textContentToLoad];
[textContentToLoad release], textContentToLoad = nil;
}
#end
Is your plist set up correctly when it comes to types?
It appears that NSDocument passes the result of [self fileNameExtensionForType:[self autosavingFileType] saveOperation:NSAutosaveElsewhereOperation] to stringByAppendingPathExtension: (the method that's throwing the exception here). If your app returns nil for this expression, then this exception may result.
You should probably file a bug at Apple for this, but in the meantime, make sure your app returns something non-nil.
Paste this into you document class (e.g. Document.m):
-(NSString*)fileNameExtensionForType:(NSString *)typeName saveOperation: (NSSaveOperationType)saveOperation
{
return #"yourAutosaveExtension";
}
Before, like kperryua said, this method was not implemented and returned nil.

xcode 'NSInvalidArgumentException', reason: '-[cure superview]: unrecognized selector sent to instance 0x4e3b7b0'

i am not sure why i am getting this error
i created a regular tab bar application
made 5 tabs (compiles and runs)
then put a tableview in one of the tabs , put the code below and i get this error
cure.h
#import <UIKit/UIKit.h>
#interface cure : UIViewController
<UITableViewDataSource, UITableViewDelegate>
{
NSArray *exercises;
}
#end
cure.m
#import "cure.h"
#implementation cure
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"cell"];
// create a cell
if ( cell == nil) {
cell = [[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:#"cell"];
}
// set the main text
cell.textLabel.text = [exercises objectAtIndex:indexPath.row];
// return the cell.
return cell;
}
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
exercises = [[NSArray alloc]
initWithObjects:#"An Interesting Title",
#"A Not That Interesting Title",
#"And Still Another Title", nil];
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[super dealloc];
}
#end
in mainwindows.xib
i have the right tab class name "cure"
error
2011-04-29 22:22:06.446 week 3 week clear [4950:40b] -[cure superview]: unrecognized selector sent to instance 0x4e3b7b0
2011-04-29 22:22:06.451 peek 3 week clear [4950:40b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[cure superview]: unrecognized selector sent to instance 0x4e3b7b0'
*** Call stack at first throw:
(
0 CoreFoundation 0x00dc15a9 __exceptionPreprocess + 185
1 libobjc.A.dylib 0x00f15313 objc_exception_throw + 44
2 CoreFoundation 0x00dc30bb -[NSObject(NSObject) doesNotRecognizeSelector:] + 187
3 CoreFoundation 0x00d32966 ___forwarding___ + 966
4 CoreFoundation 0x00d32522 _CF_forwarding_prep_0 + 50
5 UIKit 0x002e2365 -[UIView(Internal) _addSubview:positioned:relativeTo:] + 77
6 UIKit 0x002e0aa3 -[UIView(Hierarchy) addSubview:] + 57
7 UIKit 0x002e87c1 -[UIView initWithCoder:] + 840
8 Foundation 0x00017c24 _decodeObjectBinary + 3296
9 Foundation 0x00016d91 _decodeObject + 224
10 UIKit 0x004ac979 -[UIRuntimeConnection initWithCoder:] + 212
11 Foundation 0x00017c24 _decodeObjectBinary + 3296
12 Foundation 0x000189f5 -[NSKeyedUnarchiver _decodeArrayOfObjectsForKey:] + 1354
13 Foundation 0x00019024 -[NSArray(NSArray) initWithCoder:] + 596
14 Foundation 0x00017c24 _decodeObjectBinary + 3296
15 Foundation 0x00016d91 _decodeObject + 224
16 UIKit 0x004abc36 -[UINib instantiateWithOwner:options:] + 804
17 UIKit 0x004adab7 -[NSBundle(UINSBundleAdditions) loadNibNamed:owner:options:] + 168
18 UIKit 0x00363628 -[UIViewController _loadViewFromNibNamed:bundle:] + 70
19 UIKit 0x00361134 -[UIViewController loadView] + 120
20 UIKit 0x0036100e -[UIViewController view] + 56
21 UIKit 0x00373f54 -[UITabBarController transitionFromViewController:toViewController:transition:shouldSetSelected:] + 120
22 UIKit 0x00372aaa -[UITabBarController transitionFromViewController:toViewController:] + 64
23 UIKit 0x003748a2 -[UITabBarController _setSelectedViewController:] + 263
24 UIKit 0x00374711 -[UITabBarController _tabBarItemClicked:] + 352
25 UIKit 0x002b14fd -[UIApplication sendAction:to:from:forEvent:] + 119
26 UIKit 0x004b3ce6 -[UITabBar _sendAction:withEvent:] + 422
27 UIKit 0x002b14fd -[UIApplication sendAction:to:from:forEvent:] + 119
28 UIKit 0x00341799 -[UIControl sendAction:to:forEvent:] + 67
29 UIKit 0x00343c2b -[UIControl(Internal) _sendActionsForEvents:withEvent:] + 527
30 UIKit 0x00341750 -[UIControl sendActionsForControlEvents:] + 49
31 UIKit 0x002b14fd -[UIApplication sendAction:to:from:forEvent:] + 119
32 UIKit 0x00341799 -[UIControl sendAction:to:forEvent:] + 67
33 UIKit 0x00343c2b -[UIControl(Internal) _sendActionsForEvents:withEvent:] + 527
34 UIKit 0x003427d8 -[UIControl touchesEnded:withEvent:] + 458
35 UIKit 0x002d5ded -[UIWindow _sendTouchesForEvent:] + 567
36 UIKit 0x002b6c37 -[UIApplication sendEvent:] + 447
37 UIKit 0x002bbf2e _UIApplicationHandleEvent + 7576
38 GraphicsServices 0x01719992 PurpleEventCallback + 1550
39 CoreFoundation 0x00da2944 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 52
40 CoreFoundation 0x00d02cf7 __CFRunLoopDoSource1 + 215
41 CoreFoundation 0x00cfff83 __CFRunLoopRun + 979
42 CoreFoundation 0x00cff840 CFRunLoopRunSpecific + 208
43 CoreFoundation 0x00cff761 CFRunLoopRunInMode + 97
44 GraphicsServices 0x017181c4 GSEventRunModal + 217
45 GraphicsServices 0x01718289 GSEventRun + 115
46 UIKit 0x002bfc93 UIApplicationMain + 1160
47 peek 3 week clear 0x00002658 main + 102
48 peek 3 week clear 0x000025e9 start + 53
)
terminate called after throwing an instance of 'NSException'
Program received signal: “SIGABRT”.
what am i doing wrong?
thank you
You have probably assigned the "cure" class to a UIView in the "Identity" tab in Interface Builder. "cure" is a view controller, so it doesn't implement any UIView methods (such as -superview), and thus throws an exception when trying to load the nib.
Btw, you have a whole bunch of leaks in your code, please read something about memory management in Objective-C (perhaps also about Cocoa naming conventions).

NSInvalidArgumentException when calling selector with arguments

I'm trying to execute a method using selector with one NSString as parameter and fails. I have seen many questions (and answers) like this and tried most solutions but had no success. Please see code and errors below:
My MainView.m is my view controller and it has a method:
-(void)displayMessageOnTextView:(NSString *)message
{
[tv1 setText:message];
}
And in MainView.h:
#interface MainView : UIViewController {
UITextView *tv1;
}
-(void)displayMessageOnTextView:(NSString *)message;
....
I have another thread which is fired by MainView (this is from MainView.m):
- (void) runTest
{
MyThread *t = [[MyThread alloc] initWithInt:13253];
[t setMainView:self];
[t run];
[t release];
}
and MyThread.m:
#import "MyThread.h"
#import "MainView.h"
#implementation MyThread
MainView *_view;
-(id)initWithInt:(int)someInt{
_view = NULL;
return self;
}
-(void)setMainView:(MainView*)view
{
_view = view;
}
-(int)run{
NSString *s = [NSString stringWithFormat:#"Display on Gui:%d", 1];
[_view performSelectorOnMainThread:#selector(displayMessageOnTextView:message:) withObject:s waitUntilDone:YES];
}
#end
Error:
2011-04-20 02:08:11.553 myApp[22627:207] -[MainView displayMessageOnTextView:message:]: unrecognized selector sent to instance 0x4e383a0
2011-04-20 02:08:11.555 myApp[22627:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[MainView displayMessageOnTextView:message:]: unrecognized selector sent to instance 0x4e383a0'
*** Call stack at first throw:
(
0 CoreFoundation 0x00dc55a9 __exceptionPreprocess + 185
1 libobjc.A.dylib 0x00f19313 objc_exception_throw + 44
2 CoreFoundation 0x00dc70bb -[NSObject(NSObject) doesNotRecognizeSelector:] + 187
3 CoreFoundation 0x00d36966 ___forwarding___ + 966
4 CoreFoundation 0x00d36522 _CF_forwarding_prep_0 + 50
5 Foundation 0x0079e94e __NSThreadPerformPerform + 251
6 CoreFoundation 0x00da68ff __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 15
7 CoreFoundation 0x00d0488b __CFRunLoopDoSources0 + 571
8 CoreFoundation 0x00d03d86 __CFRunLoopRun + 470
9 CoreFoundation 0x00d03840 CFRunLoopRunSpecific + 208
10 CoreFoundation 0x00d03761 CFRunLoopRunInMode + 97
11 GraphicsServices 0x00ffd1c4 GSEventRunModal + 217
12 GraphicsServices 0x00ffd289 GSEventRun + 115
13 UIKit 0x00025c93 UIApplicationMain + 1160
14 myApp 0x000024c9 main + 121
15 myApp 0x00002445 start + 53
)
terminate called after throwing an instance of 'NSException'
NOTE:
I'm new to Objective-C (and generally iOS) programming, so any comment regarding coding conventions and best practice will be appreciated (even if not related to my question, but related to my code)
Your method has been declared as
-(void)displayMessageOnTextView:(NSString *)message;
This means that the method name is displayMessageOnTextView: and the corresponding selector is #selector(displayMessageOnTextView:). Note that a method name does not include parameter (variable) names.
In your -run method, change
[_view performSelectorOnMainThread:#selector(displayMessageOnTextView:message:)
withObject:s
waitUntilDone:YES];
to
[_view performSelectorOnMainThread:#selector(displayMessageOnTextView:)
withObject:s
waitUntilDone:YES];
and that runtime error shouldn’t happen again.
For the record, a method declaration corresponding to displayMessageOnTextView:message: would be:
- (void)displayMessageOnTextView:(type1)parameter1 message:(type2)parameter2;
This method:
-(void)displayMessageOnTextView:(NSString *)message
has the selector displayMessageOnTextView:. The name of the parameter itself (message in your case) does not appear in the selector. (This should be sort of intuitive: the name doesn't necessarily appear when you call the method, so it doesn't appear in the selector.) So you need to be using #selector(displayMessageOnTextView:).