Hi,
I don't understand selectors. I have written this simple code and put breakpoints to look how it behave and "count" should change from 1 to 4 cause breakpoint points 4 times at count++ . Why "count" doesn't change from 1 to 4? there are three 1 and one 2 instead.
main:
and here is output:
2011-04-22 13:31:35.461 Tablice[14110:a0f] Zdzisław Grabowski
2011-04-22 13:31:35.464 Tablice[14110:a0f] Pawel Zet
2011-04-22 13:31:35.464 Tablice[14110:a0f] Gosia Zet
2011-04-22 13:31:35.465 Tablice[14110:a0f] Hania Zet
2011-04-22 13:31:35.466 Tablice[14110:a0f]
2011-04-22 13:31:35.466 Tablice[14110:a0f] the count number: 1
2011-04-22 13:31:35.469 Tablice[14110:a0f] -------------------------
2011-04-22 13:31:35.470 Tablice[14110:a0f] what is here?awel Zet
2011-04-22 13:31:35.471 Tablice[14110:a0f] and here: Zdzisław Grabowski
2011-04-22 13:31:35.472 Tablice[14110:a0f] +++++++++++++++++++++++++
2011-04-22 13:31:35.473 Tablice[14110:a0f]
2011-04-22 13:31:35.473 Tablice[14110:a0f]
2011-04-22 13:31:35.474 Tablice[14110:a0f] the count number: 1
2011-04-22 13:31:35.474 Tablice[14110:a0f] -------------------------
2011-04-22 13:31:35.475 Tablice[14110:a0f] what is here?:Hania Zet
2011-04-22 13:31:35.477 Tablice[14110:a0f] and here: Gosia Zet
2011-04-22 13:31:35.478 Tablice[14110:a0f] +++++++++++++++++++++++++
2011-04-22 13:31:35.479 Tablice[14110:a0f]
2011-04-22 13:31:35.479 Tablice[14110:a0f]
2011-04-22 13:31:35.480 Tablice[14110:a0f] the count number: 1
2011-04-22 13:31:35.480 Tablice[14110:a0f] -------------------------
2011-04-22 13:31:35.482 Tablice[14110:a0f] what is here?:Gosia Zet
2011-04-22 13:31:35.483 Tablice[14110:a0f] and here: Pawel Zet
2011-04-22 13:31:35.483 Tablice[14110:a0f] +++++++++++++++++++++++++
2011-04-22 13:31:35.484 Tablice[14110:a0f]
2011-04-22 13:31:35.485 Tablice[14110:a0f]
2011-04-22 13:31:35.485 Tablice[14110:a0f] the count number: 2
2011-04-22 13:31:35.486 Tablice[14110:a0f] -------------------------
2011-04-22 13:31:35.486 Tablice[14110:a0f] what is here?:Hania Zet
2011-04-22 13:31:35.487 Tablice[14110:a0f] and here: Pawel Zet
2011-04-22 13:31:35.488 Tablice[14110:a0f] +++++++++++++++++++++++++
2011-04-22 13:31:35.488 Tablice[14110:a0f]
2011-04-22 13:31:35.489 Tablice[14110:a0f] Gosia Zet
2011-04-22 13:31:35.489 Tablice[14110:a0f] Hania Zet
2011-04-22 13:31:35.490 Tablice[14110:a0f] Pawel Zet
2011-04-22 13:31:35.490 Tablice[14110:a0f] Zdzisław Grabowski
I don't understand selectors. I have written this simple code and put breakpoints to look how it behave and "count" should change from 1 to 4 cause breakpoint points 4 times at count++ . Why "count" doesn't change from 1 to 4? there are three 1 and one 2 instead.
Code:
#import <Foundation/Foundation.h>
@interface [B]AdressBook[/B] : NSObject {
NSString *bookname;
NSMutableArray *bookArray;
}
@property (nonatomic,copy) NSString *bookname;
@property (nonatomic,copy) NSMutableArray *bookArray;
-(id) initWithName: (NSString*)name;
-(void) addCard: (AdressCard*) kart;
-(void) list;
-(void) sortedList;
@end
#import "AdressCard.h"
#import "AdressBook.h"
@implementation [B]AdressBook[/B]
@synthesize bookname, bookArray;
-(id) initWithName: (NSString*)name
{
self=[super init];
bookname=[[NSString alloc] initWithString:name];
bookArray=[[NSMutableArray alloc] init];
return self;
}
-(void) addCard: (AdressCard*) kart
{
[bookArray addObject:kart];
}
-(void) list
{
for (AdressCard *theKart in bookArray)
NSLog(@"%@",[theKart name]);
}
-(void) sortedList
{
//int count=0;
[bookArray sortUsingSelector:@selector(compareName:)];
}
@end
Code:
#import <Foundation/Foundation.h>
@interface [B]AdressCard[/B] : NSObject {
NSString *name;
NSString *email;
int count;
}
@property (copy) NSString *name,*email;
-(void) setName: (NSString*) theName setEmail: (NSString*) theEmail;
-(NSComparisonResult) compareName: (id) element;
@end
#import "AdressCard.h"
#import "AdressBook.h"
@implementation [B]AdressCard[/B]
@synthesize name,email;
-(void) setName: (NSString*) theName setEmail: (NSString*) theEmail
{
[self setName:theName];
[self setEmail:theEmail];
}
-(NSComparisonResult) compareName: (id) element
{
[COLOR="YellowGreen"]count++[/COLOR];
NSLog(@" ");
NSLog(@"the count number: %d", count);
NSLog(@"-------------------------");
NSLog(@"what is here?:%@", [element name]);
NSLog(@"and here: %@",[self name]);
NSLog(@"+++++++++++++++++++++++++");
NSLog(@" ");
return [name compare:(id) [element name]];
}
@end
main:
Code:
#import <Foundation/Foundation.h>
#import "AdressCard.h"
#import "AdressBook.h"
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
AdressCard *myNewCard=[[AdressCard alloc] init];
AdressCard *mySecNewCard=[[AdressCard alloc] init];
AdressCard *myThreeNewCard=[[AdressCard alloc] init];
AdressCard *myFourNewCard=[[AdressCard alloc] init];
NSString *imie=@"Zdzisław Grabowski";
NSString *email=@"ksiedzulek@wp.pl";
NSString *imieSec=@"Pawel Zet";
NSString *emailSec=@"kbm@onet.pl";
NSString *imieThree=@"Gosia Zet";
NSString *emailThree=@"kbmG@onet.pl";
NSString *imieFour=@"Hania Zet";
NSString *emailFour=@"kbmG@onet.pl";
[myNewCard setName:imie setEmail:email];
[mySecNewCard setName:imieSec setEmail:emailSec];
[myThreeNewCard setName:imieThree setEmail:emailThree];
[myFourNewCard setName:imieFour setEmail:emailFour];
AdressBook *myBook=[[AdressBook alloc] initWithName:@"moj lancuch"];
[myBook addCard:myNewCard];
[myBook addCard:mySecNewCard];
[myBook addCard:myThreeNewCard];
[myBook addCard:myFourNewCard];
[myBook list];
[myBook sortedList];
[myBook list];
[pool drain];
return 0;
}
and here is output:
2011-04-22 13:31:35.461 Tablice[14110:a0f] Zdzisław Grabowski
2011-04-22 13:31:35.464 Tablice[14110:a0f] Pawel Zet
2011-04-22 13:31:35.464 Tablice[14110:a0f] Gosia Zet
2011-04-22 13:31:35.465 Tablice[14110:a0f] Hania Zet
2011-04-22 13:31:35.466 Tablice[14110:a0f]
2011-04-22 13:31:35.466 Tablice[14110:a0f] the count number: 1
2011-04-22 13:31:35.469 Tablice[14110:a0f] -------------------------
2011-04-22 13:31:35.470 Tablice[14110:a0f] what is here?awel Zet
2011-04-22 13:31:35.471 Tablice[14110:a0f] and here: Zdzisław Grabowski
2011-04-22 13:31:35.472 Tablice[14110:a0f] +++++++++++++++++++++++++
2011-04-22 13:31:35.473 Tablice[14110:a0f]
2011-04-22 13:31:35.473 Tablice[14110:a0f]
2011-04-22 13:31:35.474 Tablice[14110:a0f] the count number: 1
2011-04-22 13:31:35.474 Tablice[14110:a0f] -------------------------
2011-04-22 13:31:35.475 Tablice[14110:a0f] what is here?:Hania Zet
2011-04-22 13:31:35.477 Tablice[14110:a0f] and here: Gosia Zet
2011-04-22 13:31:35.478 Tablice[14110:a0f] +++++++++++++++++++++++++
2011-04-22 13:31:35.479 Tablice[14110:a0f]
2011-04-22 13:31:35.479 Tablice[14110:a0f]
2011-04-22 13:31:35.480 Tablice[14110:a0f] the count number: 1
2011-04-22 13:31:35.480 Tablice[14110:a0f] -------------------------
2011-04-22 13:31:35.482 Tablice[14110:a0f] what is here?:Gosia Zet
2011-04-22 13:31:35.483 Tablice[14110:a0f] and here: Pawel Zet
2011-04-22 13:31:35.483 Tablice[14110:a0f] +++++++++++++++++++++++++
2011-04-22 13:31:35.484 Tablice[14110:a0f]
2011-04-22 13:31:35.485 Tablice[14110:a0f]
2011-04-22 13:31:35.485 Tablice[14110:a0f] the count number: 2
2011-04-22 13:31:35.486 Tablice[14110:a0f] -------------------------
2011-04-22 13:31:35.486 Tablice[14110:a0f] what is here?:Hania Zet
2011-04-22 13:31:35.487 Tablice[14110:a0f] and here: Pawel Zet
2011-04-22 13:31:35.488 Tablice[14110:a0f] +++++++++++++++++++++++++
2011-04-22 13:31:35.488 Tablice[14110:a0f]
2011-04-22 13:31:35.489 Tablice[14110:a0f] Gosia Zet
2011-04-22 13:31:35.489 Tablice[14110:a0f] Hania Zet
2011-04-22 13:31:35.490 Tablice[14110:a0f] Pawel Zet
2011-04-22 13:31:35.490 Tablice[14110:a0f] Zdzisław Grabowski