Illegal DataSource, but everything is implemented fine - cocoa

Getting :
*** Illegal NSTableView data source (<NSApplication: 0x101602bc0>). Must implement numberOfRowsInTableView: and tableView:objectValueForTableColumn:row:
Code .h
//
// AppDelegate.h
// MySQL
//
// Created by - on 10/12/12.
// Copyright (c) 2012 - Software. All rights reserved.
//
#import <Cocoa/Cocoa.h>
#interface AppDelegate : NSObject <NSApplicationDelegate> {
NSMutableArray *tabelle_totali;
IBOutlet NSTableView *tabella_tabelle_totali;
IBOutlet NSTableView *tabella_contenitore;
}
#property (assign) IBOutlet NSWindow *window;
//Metodo per scaricare dati
- (void) download_tabelle ;
//Manipolazione tabelle ricevute
- (void)tabelle_ricevute:(NSData *)tabelle;
//Refresh tabella
- (IBAction)refresh_tablelle:(id)sender;
//Refresh tabelle
- (int)numberOfRowsInTableView:(NSTableView *)aTableView;
- (id)tableView:(NSTableView *)aTableView objectValueForTableColumn:(NSTableColumn *)aTableColumn row:(int)rowIndex;
#end
Code .m
//
// AppDelegate.m
// MySQL
//
// Created by - on 10/12/12.
// Copyright (c) 2012 Alberto Bellini Software. All rights reserved.
//
#import "AppDelegate.h"
#implementation AppDelegate
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
[self download_tabelle];
[tabella_tabelle_totali reloadData];
}
- (void) download_tabelle {
NSMutableString *databaseURL = [[NSMutableString alloc] initWithString:#"http://*********************.php"];
//inizializzazione richiesta url
NSURL *url = [NSURL URLWithString:databaseURL];
//Richiesta asincrona per richiedere dati
NSURLRequest *request = [NSURLRequest requestWithURL:url];
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *tabelle, NSError *error)
{
[self tabelle_ricevute:tabelle];
}
];
}
- (void)tabelle_ricevute:(NSData *)tabelle
{
NSString *response = [[NSString alloc] initWithData:tabelle encoding:NSUTF8StringEncoding];
NSArray *tmpResp = [response componentsSeparatedByString:#"####"]; //This array splits the response string
NSLog(#"%#",response);
//Aggiungo le mie tabelle al mio array
[tabelle_totali addObjectsFromArray:tmpResp];
}
- (IBAction)refresh_tablelle:(id)sender {
//Cancello vecchi dati
while([[tabella_tabelle_totali tableColumns] count] > 0) {
[tabella_tabelle_totali removeTableColumn:[[tabella_tabelle_totali tableColumns] lastObject]];
}
NSTableColumn *column = [[NSTableColumn alloc] initWithIdentifier:#"1"];
[column setWidth:143];
[[column headerCell] setStringValue:#"*******"];
[tabella_tabelle_totali addTableColumn:column];
[tabella_tabelle_totali reloadData];
}
-(NSInteger)numberOfRowsInTableView:(NSTableView *)aTableView {
return 5;
}
-(id)tableView:(NSTableView *)aTableView objectValueForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row {
return #"hello world";
}
#end
Sorry lot of code is written in italian, but the issue is "international".. Why do i get this error ? The table dataSource is connected to the File's owner and the outlets as well..
When running the app instead of displaying 5 rows with 5 "hello world", obviously nothing happens.. Help

The problem may be in your xib file containing your table view. Have you set the table view's delegate to the File's Owner (which would be an instance of NSApplication), or have you set its delegate to your application delegate? It needs to be set to your application delegate.
If you haven't set up an object (visible in the margin to the left of your UI layout) representing your application delegate, you should do so, and connect your table view's delgate connection to that.

When using Swift, the issue may be that the data source class has not explicitly implemented the NSTableViewDataSource protocol. In particular, with the API renaming in Swift 3, it appears to be the protocol that causes the mapping of the Swift 3 syntax tableView(_:objectValueFor:row:) to the Objective-C selector tableView:objectValueForTableColumn:row:.
I discovered this when implementing a table view data source; although I had implemented the correct methods in Swift, I got the *** Illegal NSTableView data source message at runtime. I was confused until I realized I had forgotten to include the protocol conformance in my class declaration.

Related

could not dequeue collection view when using UIImage objects in Cell

I am having trouble getting this code to run after having it crash after several iterations. The intended behaviour is to simply display images in a Collection View from an array. If I comment out all the objects in the array, it runs with an empty collection view.
logo.png exists and can be properly loaded in other parts of the app through the dropdown. Delegate and DataSource is properly set to self. The Collection View has a single cell displayed with an ImageView in it (tagged as 100) and nothing else. Having a label instead of an image view causes a crash with any objects in the array as well.
The debugging output is
Could not dequeue a view of kind: UICollectionElementKindCell with identifier Cell - must
register a nib or a class for the identifier or connect a prototype cell in a storyboard
The relevant code is the following:
ViewController.m
#import "demo_frameworkViewController.h"
#interface demo_frameworkViewController ()
#end
#implementation demo_frameworkViewController {
NSMutableArray *imageArray;
}
#synthesize imageArray;
- (void)viewDidLoad
{
imageArray = [[NSMutableArray alloc] init];
[imageArray addObject:[UIImage imageNamed:#"logo.png"]];
[imageArray addObject:[UIImage imageNamed:#"logo.png"]];
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark Collection View Methods
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
return [self.imageArray count];
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:#"Cell" forIndexPath:indexPath];
UIImageView *imageDemo = (UIImageView *)[cell viewWithTag:100];
imageDemo.image =[imageArray objectAtIndex:indexPath.row];
[cell.layer setBorderWidth:2.0f];
[cell.layer setBorderColor:[UIColor whiteColor].CGColor];
return cell;
}
#end
ViewController.h
#import <UIKit/UIKit.h>
#interface demo_frameworkViewController : UIViewController <UINavigationControllerDelegate, UICollectionViewDelegate, UICollectionViewDataSource>
#property (nonatomic, retain) NSArray *imageArray;
#property (weak, nonatomic) IBOutlet UIImageView *imageView;
#end
The debugging output means that it cannot find a cell called 'Cell'. In your storyboard you need to set the Collection Reusable View value of the UICollectionViewCell to 'Cell' so that the UICollectionView knows what cell to use

Page View Controller - from images to another view controller

I'm a bit perplexed about what I'm trying to accomplish. I have a page view controller that has a data source containing an array list of images. It's actually a tutorial that a user can flip through. What I'm trying to do is make the last page a log in screen so the user can enter info and hit a login button. I thought this would be as simple as adding a login view controller to the array but oooh how wrong I was D: When I tried that I got this error:
* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIViewController _isResizable]: unrecognized selector sent to instance 0xa160660'
I do apologise for being such a noob I'm new to all of this just trying to get my head around it. Here's my code (accomplished by using this site actually):
My data source (ModelController.h)
#import <Foundation/Foundation.h>
#class DataViewController;
#interface ModelController : NSObject <UIPageViewControllerDataSource>
- (DataViewController *)viewControllerAtIndex:(NSUInteger)index storyboard:(UIStoryboard *)storyboard;
- (NSUInteger)indexOfViewController:(DataViewController *)viewController;`
#end
ModelController.m
#import "ModelController.h"
#import "DataViewController.h"
#import "LoginViewController.h"
#interface ModelController()
#property (readonly, strong, nonatomic) NSArray *pageData;
#end
#implementation ModelController
- (id)init
{
self = [super init];
if (self)
{
// Create the data model
_pageData = [[NSArray alloc] initWithObjects:
[UIImage imageNamed:#"tutorial1.png"],
[UIImage imageNamed:#"tutorial2.png"],
[UIImage imageNamed:#"lastWishes.png"],
[UIImage imageNamed:#"todo.png"],
[UIImage imageNamed:#"web.png"],
(LoginViewController*)[[UIViewController alloc] init],
nil];
}
return self;
}
- (DataViewController *)viewControllerAtIndex:(NSUInteger)index storyboard:(UIStoryboard *)storyboard
{
// Return the data view controller for the given index.
if (([self.pageData count] == 0) || (index >= [self.pageData count]))
{
return nil;
}
// Create a new view controller and pass suitable data.
DataViewController *dataViewController = [storyboard instantiateViewControllerWithIdentifier:#"DataViewController"];
dataViewController.dataObject = self.pageData[index];
return dataViewController;
}
- (NSUInteger)indexOfViewController:(DataViewController *)viewController
{
// Return the index of the given data view controller.
// For simplicity, this implementation uses a static array of model objects and the view controller stores the model object; you can therefore use the model object to identify the index.
return [self.pageData indexOfObject:viewController.dataObject];
}
#pragma mark - Page View Controller Data Source
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController
{
NSUInteger index = [self indexOfViewController:(DataViewController *)viewController];
if ((index == 0) || (index == NSNotFound)) {
return nil;
}
index--;
return [self viewControllerAtIndex:index storyboard:viewController.storyboard];
}
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController
{
NSUInteger index = [self indexOfViewController:(DataViewController *)viewController];
if (index == NSNotFound) {
return nil;
}
index++;
if (index == [self.pageData count]) {
return nil;
}
return [self viewControllerAtIndex:index storyboard:viewController.storyboard];
}
#end
The Parent (RootViewController.h)
#import <UIKit/UIKit.h>
#interface RootViewController : UIViewController <UIPageViewControllerDelegate>
#property (strong, nonatomic) UIPageViewController *pageViewController;
#end
RootViewController.m
#import "RootViewController.h"
#import "ModelController.h"
#import "DataViewController.h"
#interface RootViewController ()
#property (readonly, strong, nonatomic) ModelController *modelController;
#end
#implementation RootViewController
#synthesize modelController = _modelController;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// Configure the page view controller and add it as a child view controller.
self.pageViewController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStylePageCurl navigationOrientation:UIPageViewControllerNavigationOrientationVertical options:nil];
self.pageViewController.delegate = self;
DataViewController *startingViewController = [self.modelController viewControllerAtIndex:0 storyboard:self.storyboard];
NSArray *viewControllers = #[startingViewController];
[self.pageViewController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:NULL];
self.pageViewController.dataSource = self.modelController;
[self addChildViewController:self.pageViewController];
[self.view addSubview:self.pageViewController.view];
// Set the page view controller's bounds using an inset rect so that self's view is visible around the edges of the pages.
CGRect pageViewRect = self.view.bounds;
self.pageViewController.view.frame = pageViewRect;
[self.pageViewController didMoveToParentViewController:self];
// Add the page view controller's gesture recognizers to the book view controller's view so that the gestures are started more easily.
self.view.gestureRecognizers = self.pageViewController.gestureRecognizers;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (ModelController *)modelController
{
// Return the model controller object, creating it if necessary.
// In more complex implementations, the model controller may be passed to the view controller.
if (!_modelController) {
_modelController = [[ModelController alloc] init];
}
return _modelController;
}
#pragma mark - UIPageViewController delegate methods
/*
- (void)pageViewController:(UIPageViewController *)pageViewController didFinishAnimating: (BOOL)finished previousViewControllers:(NSArray *)previousViewControllers transitionCompleted: (BOOL)completed
{
}
*/
- (UIPageViewControllerSpineLocation)pageViewController:(UIPageViewController *)pageViewController spineLocationForInterfaceOrientation:(UIInterfaceOrientation)orientation
{
// Set the spine position to "min" and the page view controller's view controllers array to contain just one view controller. Setting the spine position to 'UIPageViewControllerSpineLocationMid' in landscape orientation sets the doubleSided property to YES, so set it to NO here.
UIViewController *currentViewController = self.pageViewController.viewControllers[0];
NSArray *viewControllers = #[currentViewController];
[self.pageViewController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:YES completion:NULL];
self.pageViewController.doubleSided = NO;
return UIPageViewControllerSpineLocationMin;
}
#end
The Child (DataViewController.h)
#import <UIKit/UIKit.h>
#interface DataViewController : UIViewController
#property (strong, nonatomic) id dataObject;
#property (weak, nonatomic) IBOutlet UIImageView *imageView;
#end
DataViewController.m
#import "DataViewController.h"
#interface DataViewController ()
#end
#implementation DataViewController
- (id)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.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void) viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
self.imageView.image = _dataObject;
}
#end
Once again, the code in question is here where I'm trying to add a view controller to the data source as the last page:
_pageData = [[NSArray alloc] initWithObjects:
[UIImage imageNamed:#"tutorial1.png"],
[UIImage imageNamed:#"tutorial2.png"],
[UIImage imageNamed:#"lastWishes.png"],
[UIImage imageNamed:#"todo.png"],
[UIImage imageNamed:#"web.png"],
(LoginViewController*)[[UIViewController alloc] init],
nil];
and getting unrecognized selector error when at runtime. I've also tried this as well:
- (id)init
{
self = [super init];
if (self)
{
LoginViewController *viewController = [self.storyboard instantiateViewControllerWithIdentifier:#"LoginViewController"];
// Create the data model
_pageData = [[NSArray alloc] initWithObjects:
[UIImage imageNamed:#"tutorial1.png"],
[UIImage imageNamed:#"tutorial2.png"],
[UIImage imageNamed:#"lastWishes.png"],
[UIImage imageNamed:#"todo.png"],
[UIImage imageNamed:#"web.png"],
viewController,
nil];
}
return self;
}
Any suggestions would be great. THanks!!
Your idea is 100% correct, your implementation is not.
This line:
dataViewController.dataObject = self.pageData[index];
is very suspicious because that will return a UIViewController in the case of your login screen. I would suggest you type-check your page data, if it is already a UIViewController subclass, just return it, if it is (in your case) a UIImage add it as the data object.

EXC_BAD_ACCESS when I close my window, which is also my application's delegate

I wrote a Cocoa Application and I got EXC_BAD_ACCESS error when I'm closing an application window. I read that this error usually means problems with memory, but I have ARC mode on and I don't need care about releasing e.t.c. (xCode forbids me to call this functions and manage memory automatically).
Error is pointing at line return NSApplicationMain(argc, (const char **)argv); in main function.
Here's my application's code:
.h file:
#interface MainDreamer : NSWindow <NSWindowDelegate>
{
NSTextField *dreamField;
NSTableView *dreamTable;
NSImageView *dreamview;
NSMutableArray *dreamlist;
NSMutableArray *dataset;
}
#property (nonatomic, retain) IBOutlet NSTextField *dreamField;
#property (nonatomic, retain) IBOutlet NSTableView *dreamTable;
#property (nonatomic, retain) IBOutlet NSImageView *dreamview;
#property (nonatomic, retain) IBOutlet NSMutableArray *dreamlist;
#property (nonatomic, retain) IBOutlet NSMutableArray *dataset;
#property (assign) IBOutlet NSWindow *window;
#end
.m file:
#implementation MainDreamer
#synthesize window;
#synthesize dataset;
#synthesize dreamField;
#synthesize dreamlist;
#synthesize dreamview;
#synthesize dreamTable;
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification{
NSString *applicationPath = [[NSBundle mainBundle] bundlePath];
NSString *filename = [applicationPath stringByAppendingPathComponent:#"dreams"];
NSLog(self.description);
dreamlist = [[NSMutableArray alloc] init];
dataset = [[NSMutableArray alloc] init];
dataset = [NSKeyedUnarchiver unarchiveObjectWithFile:filename];
if([dataset count] != 0) {
int i = 0;
while (i < [dataset count]) {
Dream *dr = [[Dream alloc] init];
dr = [dataset objectAtIndex:i];
[dreamlist addObject: dr.dreamname];
i++;
}
}
[dreamTable reloadData];
}
-(void)applicationWillTerminate:(NSNotification *)notification{
NSString *applicationPath = [[NSBundle mainBundle] bundlePath];
NSString *filename = [applicationPath stringByAppendingPathComponent:#"dreams"];
[NSKeyedArchiver archiveRootObject:dataset toFile:filename];
NSLog(#"finish");
}
- (void) mouseUp:(NSEvent *)theEvent{
long index = [dreamTable selectedRow];
Dream *dr = [[Dream alloc] init];
dr = [dataset objectAtIndex:index];
dr.dreampicture = dreamview.image;
[dataset replaceObjectAtIndex:index withObject:dr];
NSLog(self.description);
}
- (void) tableViewSelectionDidChange: (NSNotification *) notification{
long row = [dreamTable selectedRow];
Dream *dr = [[Dream alloc] init];
dr = [dataset objectAtIndex: row];
if(dr.dreampicture != NULL)
dreamview.image = dr.dreampicture;
NSLog(#"selected row changed");
}
Class "Dream":
#interface Dream : NSObject <NSCoding>
{
NSString *dreamname;
NSImage *dreampicture;
}
#property (retain) NSString* dreamname;
#property (retain) NSImage* dreampicture;
-(id)initWithCoder:(NSCoder *)aDecoder;
-(void)encodeWithCoder:(NSCoder *)aCoder;
#end
What is wrong, why EXC_BAD_ACCESS occurs?I remind that I have xCode 4 with Automatic Reference Counting (ARC)
Thanks
UPDATE
I used Profile to find zombie event. So I found out this: An Objective-C message was sent to a deallocated object(zombie( at adress 0x108d85230)
Responsible Caller - [NSApplication(NSWindowCache) _checkForTerminateAfterLastWindowClosed: saveWindows:]
I had this function in code:
- (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)sender{
return TRUE;
}
However after I putted it in comments, this zombie event continue to occur.
The crash is caused by the fact that you made the window your application's delegate. When you close the window, that is the last release that kills it off, and if it's the last window you had up, it causes the application to ask its delegate whether it should quit. Since the window you just killed off is the application's delegate, you get that crash.
Longer explanation and suggestion of solution in my answer on your subsequent question.
This is wrong:
dataset = [[NSMutableArray alloc] init]; // WRONG
dataset = [NSKeyedUnarchiver unarchiveObjectWithFile:filename];
Why? You first allocate an empty array, and store that in the instance variable dataset. But in the next line, you replace the empty array with whatever +unarchiveObjectWithFile: returns. Why is this a problem? Well, if you read the docs, you'll see that it returns nil if the file is not found. This means that you now replace the empty array with nil, and all messages you send to dataset will be ignored (messages to nil are silently ignored in Objective-C)
I assume you actually want load the dataset from file, and only if that failed, start with an empty dataset:
dataset = [NSKeyedUnarchiver unarchiveObjectWithFile:filename];
if (dataset==nil) dataset = [[NSMutableArray alloc] init];
You have a similar error later on:
Dream *dr = [[Dream alloc] init]; // WRONG
dr = [dataset objectAtIndex:index];
You create a Dream object, and then replace it immediately with something from the dataset. What you actually want to do is:
Dream *dr;
dr = [dataset objectAtIndex:index];
or shorter:
Dream *dr = [dataset objectAtIndex:index];
Then again, you could replace the while loop with a fast-enumeration-style for loop:
for (Dream *dr in dataset) {
[dreamlist addObject: dr.dreamname];
}
Finally, to get to a point, I don't think the EXC_BAD_ACCESS actually occurs in main.h. I assume you use Xcode 4. Please use the thread/stack navigator in the right sidebar when debugging to find the actual position where the error occurs.
It could be that the error actually occurs in applicationWillTerminate:, because you try to archive dataset, which is probably nil, and it's probably not allowed to archive nil.
With ARC you should use strong and weak instead of retain and assign.

Cannot create NSArray from object

XCode Error:
GNU gdb 6.3.50-20050815 (Apple version gdb-1708) (Mon Aug 8 20:32:45 UTC 2011)
Copyright 2004 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB. Type "show warranty" for details.
This GDB was configured as "x86_64-apple-darwin".tty /dev/ttys000
[Switching to process 2530 thread 0x0]
2011-11-12 18:29:29.857 RaiseMan[2530:707] Cannot create NSArray from object <Document: 0x100539a00> of class Document
2011-11-12 18:29:29.879 RaiseMan[2530:707] Cannot create NSArray from object <Document: 0x10015dda0> of class Document
2011-11-12 18:30:10.362 RaiseMan[2530:707] Cannot create NSArray from object <Document: 0x1001b0a90> of class Document
person.h:
#import <Foundation/Foundation.h>
#interface Person : NSObject
{
NSString *personName;
float expectedRaise;
}
#property (readwrite, copy) NSString *personName;
#property (readwrite) float expectedRaise;
#end
person.m:
#import "Person.h"
#implementation Person
- (id) init
{
self = [super init];
expectedRaise = 5.0;
personName = #"New Person";
return self;
}
- (void)dealloc
{
[personName release];
[super dealloc];
}
#synthesize personName;
#synthesize expectedRaise;
#end
document.h:
#import <Cocoa/Cocoa.h>
#interface Document : NSDocument
{
NSMutableArray *employees;
}
- (void)setEmployees:(NSMutableArray *)a;
#end
document.m
#import "Document.h"
#implementation Document
- (id)init
{
self = [super init];
//if (self) {
employees = [[NSMutableArray alloc] init];
//}
return self;
}
- (void)dealloc
{
[self setEmployees:nil];
[super dealloc];
}
-(void)setEmployees:(NSMutableArray *)a
{
//this is an unusual setter method we are goign to ad a lot of smarts in the next chapter
if (a == employees)
return;
[a retain];
[employees release];
employees = a;
}
- (NSString *)windowNibName
{
// Override returning the nib file name of the document
// If you need to use a subclass of NSWindowController or if your document supports multiple NSWindowControllers, you should remove this method and override -makeWindowControllers instead.
return #"Document";
}
- (void)windowControllerDidLoadNib:(NSWindowController *)aController
{
[super windowControllerDidLoadNib:aController];
// Add any code here that needs to be executed once the windowController has loaded the document's window.
}
- (NSData *)dataOfType:(NSString *)typeName error:(NSError **)outError
{
/*
Insert code here to write your document to data of the specified type. If outError != NULL, ensure that you create and set an appropriate error when returning nil.
You can also choose to override -fileWrapperOfType:error:, -writeToURL:ofType:error:, or -writeToURL:ofType:forSaveOperation:originalContentsURL:error: instead.
*/
NSException *exception = [NSException exceptionWithName:#"UnimplementedMethod" reason:[NSString stringWithFormat:#"%# is unimplemented", NSStringFromSelector(_cmd)] userInfo:nil];
#throw exception;
return nil;
}
- (BOOL)readFromData:(NSData *)data ofType:(NSString *)typeName error:(NSError **)outError
{
/*
Insert code here to read your document from the given data of the specified type. If outError != NULL, ensure that you create and set an appropriate error when returning NO.
You can also choose to override -readFromFileWrapper:ofType:error: or -readFromURL:ofType:error: instead.
If you override either of these, you should also override -isEntireFileLoaded to return NO if the contents are lazily loaded.
*/
NSException *exception = [NSException exceptionWithName:#"UnimplementedMethod" reason:[NSString stringWithFormat:#"%# is unimplemented", NSStringFromSelector(_cmd)] userInfo:nil];
#throw exception;
return YES;
}
+ (BOOL)autosavesInPlace
{
return YES;
}
#end
Full project code: http://sharesend.com/zyca7
I run into the same problem today and I have already solved it.
You should bind the column rather than the cell to the array controller.
Without downloading the zip file from ShareSend (please use a better host, such as Dropbox, or better yet, some form of version control), I can only guess that you have bound an array controller's contentArray to your document but failed to specify an array property of that document.
Telling the array controller to use the document itself as its content array will not work, because a document is not an array. Go into your nib, select the array controller, edit the contentArray binding, and specify the model key path for the array property you want to bind it to.

objectAtIndex - Message sent to deallocated instance

I am having a real problem finding where my problem is in my search controller. This is a table view with search bar and search display controller. It used to work fine, but all the sudden it stopped working. I turned on NSZombieEnabled and it shows that my NSArray called searchDataSource is the zombie.
When you type a search term the "shouldReloadTableForSearchTerm" executes the handleSearchForTerm function. The handleSearchForTerm" function accesses my ProductInfo class that query a SQLite database and returns the query results. Those results are then placed in my searchDataSource Array. Everything appears to work fine there. However, once I get to the "cellForRowAtIndexPath" function and I try to load the cells from the searchDataSource, that is when I run in to the problem of the Array having been deallocated.
Here is my code for the search controller:
//
// SearchViewController.h
// Priority Wire
//
// Created by Keith Yohn on 2/2/11.
// Copyright 2011 Priority Wire & Cable. All rights reserved.
//
#import <UIKit/UIKit.h>
#interface FourthViewController : UIViewController <UITableViewDataSource, UITableViewDelegate, UISearchDisplayDelegate, UISearchBarDelegate> {
UITableView *mainTableView;
NSArray *searchDataSource;
NSMutableArray *contentsList;
NSMutableArray *searchResults;
NSString *savedSearchTerm;
NSString *webURL;
}
#property (nonatomic, retain) IBOutlet UITableView *mainTableView;
#property (nonatomic, retain) IBOutlet NSArray *searchDataSource;
#property (nonatomic, retain) NSMutableArray *contentsList;
#property (nonatomic, retain) NSMutableArray *searchResults;
#property (nonatomic, copy) NSString *savedSearchTerm;
#property (nonatomic, retain) NSString *webURL;
- (void)handleSearchForTerm:(NSString *)searchTerm;
#end
SearchViewController.m
//
// SearchViewController.m
// Priority Wire
//
// Created by Keith Yohn on 2/2/11.
// Copyright 2011 Priority Wire & Cable. All rights reserved.
//
#import "FourthViewController.h"
#import "ProductsDatabase.h"
#import "ProductInfo.h"
#import "WebViewController.h"
#implementation FourthViewController
#synthesize mainTableView;
#synthesize searchDataSource;
#synthesize contentsList;
#synthesize searchResults;
#synthesize savedSearchTerm;
#synthesize webURL;
- (void)viewDidLoad {
[super viewDidLoad];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [self.searchDataSource count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
// Set up the cell...
ProductInfo *info = [searchDataSource objectAtIndex:indexPath.row]; //This is where I get the 'message sent to deallocated instance' message.
[cell.textLabel setText:info.sName];
[cell.detailTextLabel setText:info.sType];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
ProductInfo *info = [searchDataSource objectAtIndex:indexPath.row];
webURL = [NSString stringWithFormat:#"http://www.prioritywire.com/specs/%#", info.sFile];
WebViewController *wvController = [[WebViewController alloc] initWithNibName:#"WebViewController" bundle:[NSBundle mainBundle]];
wvController.URL = webURL;
wvController.navTitle = #"Spec Sheet";
[self.navigationController pushViewController:wvController animated:YES];
[wvController release];
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Relinquish ownership any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
[super viewDidUnload];
// Save the state of the search UI so that it can be restored if the view is re-created.
[self setSavedSearchTerm:[[[self searchDisplayController] searchBar] text]];
[self setSearchResults:nil];
}
- (void)dealloc {
[searchDataSource release], searchDataSource = nil;
[mainTableView release];
[contentsList release];
[searchResults release];
[savedSearchTerm release];
[super dealloc];
}
- (void)handleSearchForTerm:(NSString *)searchTerm
{
[self setSavedSearchTerm:searchTerm];
if ([self searchResults] == nil)
{
NSMutableArray *array = [[NSMutableArray alloc] init];
[self setSearchResults:array];
[array release], array = nil;
} else {
NSArray *productInfo = [[ProductsDatabase database] searchListing:searchTerm];
self.searchDataSource = productInfo;
[self.mainTableView reloadData];
[productInfo release];
}
[[self searchResults] removeAllObjects];
if ([[self savedSearchTerm] length] != 0)
{
for (NSString *currentString in [self contentsList])
{
if ([currentString rangeOfString:searchTerm options:NSCaseInsensitiveSearch].location != NSNotFound)
{
[[self searchResults] addObject:currentString];
}
}
}
}
#pragma mark -
#pragma mark UISearchDisplayController Delegate Methods
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
[self handleSearchForTerm:searchString];
// Return YES to cause the search result table view to be reloaded.
return YES;
}
- (void)searchDisplayControllerWillEndSearch:(UISearchDisplayController *)controller
{
[self setSavedSearchTerm:nil];
self.searchDataSource = nil;
[self.mainTableView reloadData];
}
#end
I am quite new to objective-C and can't understand what I did wrong. I have spent days trying to figure this out and have had no luck. I would appreciate any help anyone can offer.
Keith
This bit of code seems to be the only place searchDataSource gets set:
NSArray *productInfo = [[ProductsDatabase database] searchListing:searchTerm];
self.searchDataSource = productInfo;
[self.mainTableView reloadData];
[productInfo release];
If ProductsDatabase follows the rules, you don't own the returned array (i.e. it is already autoreleased) so the release on the fourth line is incorrect.
Don't you mean to use your searchResults-array instead of your searchDataSource, because in handleSearchForTerm: you are adding the results to it. Why do you even have the searchResult ivar? It's only used in handleSearchForTerm:, maybe some mixup there?

Resources