Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 04-01-2011, 12:21 PM   #1 (permalink)
Is this a peanut, doctor?
 
Join Date: Apr 2011
Location: Copenhagen
Posts: 254
teCma is on a distinguished road
Default Invalid argument type to unary expression - HELP!

Hey forum, noob here.

I have this program with a tableView as my first view. I have also implemented (or at least tried to) a search bar on top of the view. Have used several hours to search for a solution, but without positive results.

Code:
 #import "FirstViewController.h"
    #import "NSDictionary-MutableDeepCopy.h"


    @implementation FirstViewController

    @synthesize listData, table, search, allNames;

    #pragma mark -
    #pragma mark Custom Methods 
    -(void)resetSearch {
        NSMutableDictionary *allNamesCopy = [self.allNames mutableDeepCopy];
        self.names = allNamesCopy;
        [allNamesCopy release];
        NSMutableArray *keyArray = [[NSMutableArray alloc] init];
        [keyArray addObjectsFromArray:[[self.allNames allKeys]
                                       sortedArrayUsingSelector:@selector(compare:)]];
        self.keys = keyArray;
        [keyArray release];
    }

    -(void)handleSearchForTerm:(NSString *)searchTerm {
        NSMutableArray *sectionsToRemove = [[NSMutableArray alloc] init];
        [self resetSearch];

        for (NSString *key in self.keys) {
            NSMutableArray *array = [names valueForKey:key];
            NSMutableArray *toRemove = [[NSMutableArray alloc] init];
            for (NSString *name in array) {
                if ([name rangeOfString:searchTerm
                    options:NSCaseInsensitiveSearch].location == NSNotFound)
                    [toRemove addObject:name];
            }

            if ([array count] == [toRemove count])
                [sectionsToRemove addObject:key];

            [array removeObjectsInArray:toRemove];

            [toRemove release];

        }

        [self.keys removeObjectsInArray:sectionsToRemove];
        [sectionsToRemove release];
        [table reloadData];

    }


    - (void)viewDidLoad {    

        NSString *path = [[NSBundle mainBundle] pathForResource:@"sortednames" ofType:@"plist"];
        NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:path];

        self.allNames = dict;

        [dict release];

        [self resetSearch];
        [table reloadData];
        [table setContentOffset:CGPointMake(0.0, 44.0)animated:NO];


        self.parentViewController.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"background.png"]];

        NSArray *array = [[NSArray alloc] initWithObjects: ALOT OF STRINGS, nil];

        self.listData = array;

        [array release];
        [super viewDidLoad];

    }

    /*
     // The designated initializer. Override to perform setup that is required before the view is loaded.
     - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
     if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
     // Custom initialization
     }
     return self;
     }
     */

    /*
     // Implement loadView to create a view hierarchy programmatically, without using a nib.
     - (void)loadView {
     }
     */

    /*
     // Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
     - (void)viewDidLoad {
     [super viewDidLoad];
     }
     */

    /*
     // Override to allow orientations other than the default portrait orientation.
     - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
     // Return YES for supported orientations
     return (interfaceOrientation == UIInterfaceOrientationPortrait);
     }
     */

    - (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;

        self.listData =  nil;
        self.table = nil;
        self.search = nil;
        self.allNames = nil;
        self.names = nil;
        self.keys = nil;

        [super viewDidUnload];
    }


    - (void)dealloc {[listData release];
        [search release];
        [table release];
        [allNames release];
        [keys release];
        [names release];
        [super dealloc];
    }

    #pragma mark -
    #pragma mark Table View Data Source Methods

    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
        return ([keys count] > 0) ? [keys count] : 1;

    }

    - (NSInteger)tableView:(UITableView *)aTableView
     numberOfRowsInSection: (NSInteger)section {
        return [self.listData count];
        if ([keys count] == 0)
            return 0;
        NSString *key = [keys objectAtIndex:section];
        NSArray *nameSection = [names objectForKey:key];
        return [nameSection count];
    }

    - (UITableViewCell *)tableView:(UITableView *)aTableView

    cellForRowAtIndexPath:(NSIndexPath *)indexPath {

        NSUInteger section = [indexPath section];
        NSUInteger row = [indexPath row];

        NSString *key = [keys objectAtIndex:section];
        NSArray *nameSection = [names objectForKey:key];

        static NSString *sectionsTableIdentifier = @"sectionsTableIdentifier";



        UITableViewCell *cell = [aTableView dequeueReusableCellWithIdentifier:
                                 sectionsTableIdentifier];
        if (cell == nil) {
            cell = [[[UITableViewCell alloc]initWithFrame:CGRectZero
                                          reuseIdentifier: sectionsTableIdentifier] autorelease];
        } 

        cell.backgroundColor = [UIColor clearColor];
        cell.textColor = [UIColor blueColor]; 
        cell.detailTextLabel.textColor = [UIColor blueColor];

        cell.text = [nameSection objectAtIndex:row];
        return cell;



    }

        - (NSString *)tableView:(UITableView *)tableView 
        titleForHeaderInSection:(NSInteger)section {
        if ([keys count] == 0)
            return nil;


        NSString *key = [keys objectAtIndex:section];
        return key;    

        }

        -(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
            return keys;


#pragma mark -
#pragma mark Table View Delegate Methods
        - (NSIndexPath *)tableView:(UITableView *)tableView
    willSelectRowAtIndexPath:(NSIndexPath *)indexPath {
            [search resignFirstResponder];
            return indexPath;
        }


#pragma mark -
#pragma mark Serch Bar Delegate Methods

    - (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
        NSString *searchTerm = [searchBar text];
        [self handleSearchForTerm:searchTerm];

    }

-(void)searchBar(UISearchBar *)searchBar
textDidChange:(NSString *)searchTerm {
    if ([searchTerm length] == 0) {
        self [resetSearch];
        [table reloadData];
        return;
    }

    [self handleSearchForTerm:searchTerm];

}

    - (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
        search.text = @"":
        [self resetSearch];
        [table reloadData];
        [searchBar resignFirstResponder];

}
    (some if statements)
    return cell;
    }

@end
My errors occur in the following:

#pragma mark -
#pragma mark Table View Delegate Methods
- (NSIndexPath *)tableViewUITableView *)tableView
willSelectRowAtIndexPathNSIndexPath *)indexPath {
[search resignFirstResponder];
return indexPath;

It's the third line. Im aware that it is the missing } just before this method. But when I insert that, the rest of the document becomes error'ed..

1. Invalid argument type 'NSIndexPath *' to unary expression
2. Wrong type argument to unary minus
3. Expected ';' before ':' token

I don't know if I should post more of my code, if needed - let me know. There's alot.

Thanks alot in advance...

Btw. sorry for bad english and grammar
teCma is offline   Reply With Quote
Old 04-01-2011, 12:40 PM   #2 (permalink)
Registered Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,858
smasher will become famous soon enough
Default

As you said, you are missing the } at the end of your sectionIndexTitlesForTableView: method. You must fix that error.

After that you're missing a colon in this line:
Code:
-(void)searchBar(UISearchBar *)searchBar

should be

-(void)searchBar: (UISearchBar *)searchBar
In that same method you have self [resetSearch]; which is incorrect too.
__________________

Free Games!
smasher is offline   Reply With Quote
Old 04-03-2011, 01:57 PM   #3 (permalink)
Is this a peanut, doctor?
 
Join Date: Apr 2011
Location: Copenhagen
Posts: 254
teCma is on a distinguished road
Default

Ehm. Thanks alot for your help - I do appreciate it.

I can launch my app, type in charaters, press cancel button - BUT: when i launch the app it highlights these line:

Code:
NSString *key = [keys objectAtIndex:section];
NSArray *nameSection = [names objectForKey:key];
it comes with an error that says Thread 1: Program recieved signal :"SIGABRT"... this it at the "self.names = allNamesCopy" line. O.o

If anybody has any suggestions for what i should do to make my array: listData searchable PLZZ LET ME KNOW. Also how to fix that error message which is actully green :O

Sorry that I'm so stupid xD - I guess I am.

Last edited by teCma; 04-03-2011 at 03:27 PM.
teCma is offline   Reply With Quote
Old 04-03-2011, 05:26 PM   #4 (permalink)
Is this a peanut, doctor?
 
Join Date: Apr 2011
Location: Copenhagen
Posts: 254
teCma is on a distinguished road
Default

Error from log:

2011-04-04 00:22:05.423 Cantinos[437:207] Unknown class tableView in Interface Builder file.
2011-04-04 00:22:05.432 Cantinos[437:207] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[NSMutableArray objectAtIndex:]: index 0 beyond bounds for empty array'
*** Call stack at first throw:
(
0 CoreFoundation 0x00dc85a9 __exceptionPreprocess + 185
1 libobjc.A.dylib 0x00f1c313 objc_exception_throw + 44
2 CoreFoundation 0x00dbe0a5 -[__NSArrayM objectAtIndex:] + 261
3 Cantinos 0x000038c1 -[FirstViewController tableView:cellForRowAtIndexPath:] + 113
4 UIKit 0x0032db98 -[UITableView(UITableViewInternal) _createPreparedCellForGlobalRow:withIndexPath:] + 634
5 UIKit 0x003234cc -[UITableView(UITableViewInternal) _createPreparedCellForGlobalRow:] + 75
6 UIKit 0x003388cc -[UITableView(_UITableViewPrivate) _updateVisibleCellsNow:] + 1561
7 UIKit 0x0033090c -[UITableView layoutSubviews] + 242
8 QuartzCore 0x01d68a5a -[CALayer layoutSublayers] + 181
9 QuartzCore 0x01d6addc CALayerLayoutIfNeeded + 220
10 QuartzCore 0x01d100b4 _ZN2CA7Context18commit_transactionEPNS_11Transacti onE + 310
11 QuartzCore 0x01d11294 _ZN2CA11Transaction6commitEv + 292
12 UIKit 0x002ba9c9 -[UIApplication _reportAppLaunchFinished] + 39
13 UIKit 0x002bae83 -[UIApplication _runWithURLayload:launchOrientation:statusBarSty le:statusBarHidden:] + 690
14 UIKit 0x002c5617 -[UIApplication handleEvent:withNewEvent:] + 1533
15 UIKit 0x002bdabf -[UIApplication sendEvent:] + 71
16 UIKit 0x002c2f2e _UIApplicationHandleEvent + 7576
17 GraphicsServices 0x01720992 PurpleEventCallback + 1550
18 CoreFoundation 0x00da9944 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FU NCTION__ + 52
19 CoreFoundation 0x00d09cf7 __CFRunLoopDoSource1 + 215
20 CoreFoundation 0x00d06f83 __CFRunLoopRun + 979
21 CoreFoundation 0x00d06840 CFRunLoopRunSpecific + 208
22 CoreFoundation 0x00d06761 CFRunLoopRunInMode + 97
23 UIKit 0x002ba7d2 -[UIApplication _run] + 623
24 UIKit 0x002c6c93 UIApplicationMain + 1160
25 Cantinos 0x00001bf4 main + 102
26 Cantinos 0x00001b85 start + 53
)
terminate called after throwing an instance of 'NSException'

hope you can help...
teCma is offline   Reply With Quote
Old 04-03-2011, 05:36 PM   #5 (permalink)
Reading the Documentation
 
baja_yu's Avatar
 
Join Date: Sep 2010
Location: 45.255019,19.844908
Posts: 5,414
baja_yu has a spectacular aura about
Default

I think the error is pretty clear

Quote:
[NSMutableArray objectAtIndex:]: index 0 beyond bounds for empty array'
baja_yu is offline   Reply With Quote
Old 04-03-2011, 05:56 PM   #6 (permalink)
Is this a peanut, doctor?
 
Join Date: Apr 2011
Location: Copenhagen
Posts: 254
teCma is on a distinguished road
Default

right, ty

but actully I dont really get my code :O

What is supposed to be stored in keys?...
teCma is offline   Reply With Quote
Old 04-03-2011, 06:09 PM   #7 (permalink)
Just helping out.
 
Domele's Avatar
 
Join Date: Feb 2011
Posts: 2,565
Domele is on a distinguished road
Default

The answer to that is to stop copying and pasting code you don't understand.
Domele is offline   Reply With Quote
Old 04-03-2011, 06:13 PM   #8 (permalink)
Is this a peanut, doctor?
 
Join Date: Apr 2011
Location: Copenhagen
Posts: 254
teCma is on a distinguished road
Default

Quote:
Originally Posted by Domele View Post
The answer to that is to stop copying and pasting code you don't understand.
yes, I know that is very, very dumb. I just really want to get that searchbar function implemented in my program... I'm really reading and learning alot atm.
teCma is offline   Reply With Quote
Old 04-03-2011, 06:14 PM   #9 (permalink)
Is this a peanut, doctor?
 
Join Date: Apr 2011
Location: Copenhagen
Posts: 254
teCma is on a distinguished road
Default

Quote:
Originally Posted by Domele View Post
The answer to that is to stop copying and pasting code you don't understand.
Even though I wrote it all my self, didnt paste - not that it matter's :/
teCma is offline   Reply With Quote
Old 04-03-2011, 06:18 PM   #10 (permalink)
Just helping out.
 
Domele's Avatar
 
Join Date: Feb 2011
Posts: 2,565
Domele is on a distinguished road
Default

Okay then. Pretty sure your keys arrays is supposed to be your datasource for your tableview.
Domele is offline   Reply With Quote
Old 04-03-2011, 06:28 PM   #11 (permalink)
Is this a peanut, doctor?
 
Join Date: Apr 2011
Location: Copenhagen
Posts: 254
teCma is on a distinguished road
Default

Quote:
Originally Posted by Domele View Post
Okay then. Pretty sure your keys arrays is supposed to be your datasource for your tableview.
Then I guess it's pretty fail that my array with all the data that is for the tableView is in an array called listData. O.o
teCma is offline   Reply With Quote
Reply

Bookmarks

Tags
invalid argument type, uisearchbar, uitableview, unary expression

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 373
12 members and 361 guests
condor304, dansparrow, dre, ilmman, LezB44, michelle, Objective Zero, samdanielblr, Sami Gh, shagor012, thephotographer, tinamm64
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,663
Threads: 94,119
Posts: 402,896
Top Poster: BrianSlick (7,990)
Welcome to our newest member, LezB44
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 01:39 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0