How to reverse single link list , using Objective C - objective-c

I write a single link list , but don't know how to reverse , this is my some code.
node class MMNode
#import <Foundation/Foundation.h>
#interface MMNode : NSObject
#property (nonatomic, assign) int data;//data
#property (nonatomic, strong) MMNode *next;//next node
#end
Single link list class MMList
#import <Foundation/Foundation.h>
#import "MMNode.h"
#interface MMList : NSObject
#property (nonatomic, strong) MMNode *head;//first node
#property (nonatomic, strong) MMNode *hail;//last node
//init
- (instancetype)initWithData:(int)data;
//append
- (void)append:(int)data;
//print
- (void)printList;
//
- (void)reverse;
#end
How to reverse single link list?
- (void)reverse {
//Code...?
}

I am posting my code, as I don't know what exactly you have implemented.
LinkNode.h
#import <Foundation/Foundation.h>
#interface LinkNode : NSObject
#property (nonatomic) int data;
#property (nonatomic) LinkNode *next;
-(id)initWithData:(int) data;
#end
LinkNode.m
#import "LinkNode.h"
#implementation LinkNode
-(id)initWithData:(int) data{
if (self = [super init]){
self.data = data;
self.next = nil;
}
return self;
}
#end
SinglyLinkedList.h
#import <Foundation/Foundation.h>
#import "LinkNode.h"
#interface SinglyLinkedList : NSObject
#property(nonatomic) LinkNode *head;
#property(nonatomic) LinkNode *current;
-(id) initWithCapacity: (int)capacity;
-(void) appendData:(int)data;
-(void) prependData : (int)data;
-(void) printList:(LinkNode*)node;
-(void) reverseList;
-(void) deleteData:(int)data;
#end
SinglyLinkedList.m
#import "SinglyLinkedList.h"
#import "LinkNode.h"
#interface SinglyLinkedList()
#end
#implementation SinglyLinkedList
-(id)initWithCapacity:(int)capacity{
if (self == [super init]) {
for (int i=1; i<=capacity;i++) {
[self appendData:i];
}
}
return self;
}
-(void) appendData:(int)data{
if (self.head == nil){
self.current = [[LinkNode alloc] initWithData:data];
self.head = self.current;
return;
}
while (self.current.next != nil) {
self.current = self.current.next;
}
self.current.next = [[LinkNode alloc] initWithData:data];
}
-(void)prependData:(int)data{
if (self.head == nil){
self.current = [[LinkNode alloc] initWithData:data];
self.head = self.current;
return;
}
self.current = [[LinkNode alloc] initWithData:data];
self.current.next = self.head;
self.head = self.current;
}
-(void) printList:(LinkNode *)node {
while (node != nil) {
NSLog(#" %d \t", node.data);
node = node.next;
}
}
-(void) reverseList {
LinkNode *prev = nil;
LinkNode *current = self.head;
LinkNode *next = nil;
while (current != nil) {
next = current.next;
current.next = prev;
prev = current;
current = next;
}
self.head = prev;
}
-(void)deleteData:(int)data{
if (self.head == nil) { return; }
if (self.head.data == data) {
self.head = self.head.next;
return;
}
LinkNode *currentNode = self.head;
while (currentNode.next != nil) {
if (currentNode.next.data == data){
currentNode.next = currentNode.next.next;
return;
}
currentNode = currentNode.next;
}
}
#end
Call from main.m
int main(int argc, const char * argv[]) {
SinglyLinkedList *list = [[SinglyLinkedList alloc] initWithCapacity:5];
[list appendData:15];
NSLog(#"After Append");
[list printList:list.head];
[list prependData:0];
NSLog(#"After Prepend");
[list printList:list.head];
[list reverseList];
NSLog(#"After Reverse");
[list printList:list.head];
[list deleteData:15];
[list reverseList];
NSLog(#"After Delete");
[list printList:list.head];
return NSApplicationMain(argc, argv);
}
For more understanding you can refer this link from geeksforgeeks

Related

NSLOG does not work

I try to get info for the array in the console but the NSLOg does not show anything. This is a class where i store data for the app. Here is the code.
#import <Foundation/Foundation.h>
#import "DAObject.h"
#interface DataModel : NSObject
#property (strong, nonatomic) NSArray *array;
#property (strong, nonatomic) NSArray *array2;
#end
and in the .m file
#import "DataModel.h"
#implementation DataModel
- (id)initWithCoder:(NSCoder *)decoder {
if (self = [super init]) {
}
return self;
}
- (NSArray *) _array
{
DAObject *obj1 = [[DACityObject alloc] init];
obj1.name = #"Obj1";
DACityObject *obj2 = [[DACityObject alloc] init];
obj2.name = #"Obj2";
_array = #[ obj1, obj2];
for (int i = 0; i < [_array count]; i++)
{
NSLog(#"Item %d = %#", i, [_array objectAtIndex:i]);
}
return _array;
}
- (NSArray *) array2 {
_array2 = [[NSArray alloc] initWithObjects:#"icon1.png", #"icon2.png",#"icon3.png", nil];
for (int i = 0; i < [_array2 count]; i++) {
NSLog(#"Item %d = %#", i, [_array2 objectAtIndex:i]);
}
return _array2;
}
#end
Where is the problem? I can't figure it out.
You need to access from anywhere to that getters, after that you will see logs.
For example try to do that in your AppDelegate.m
#import "DataModel.h"
#implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
DataModel *data = [DataModel new];
data.array;
data.array2;
}

No visible #interface for 'BNRItemStore' declares the selector 'deleteImageForKey;'

I'm new to objective C and have been learning with the book "IOS Programming Big Nerd Ranch Guide 4th edition". I keep getting a recurring error and I have been working to resolve it for the last few hours. After researching for a bit, I came here. Any help would be greatly appreciated.
"No visible #interface for 'BNRItemStore' declares the selector 'deleteImageForKey;'"
BNRItemStore.h
#import <Foundation/Foundation.h>
#class BNRItem;
#interface BNRItemStore : NSObject
#property (nonatomic, readonly) NSArray *allItems;
// Notice that this is a class method and prefixed with a + instead of a -
+ (instancetype)sharedStore;
- (BNRItem *)createItem;
- (void)removeItem:(BNRItem *)item;
- (void)moveItemAtIndex:(NSInteger)fromIndex
toIndex:(NSInteger)toIndex;
#end
BNRItemStore.m
#import "BNRItemStore.h"
#import "BNRItem.h"
#interface BNRItemStore ()
#property (nonatomic) NSMutableArray *privateItems;
#end
#implementation BNRItemStore
+ (instancetype)sharedStore
{
static BNRItemStore *sharedStore = nil;
// Do I need to create a sharedStore?
if (!sharedStore) {
sharedStore = [[super allocWithZone:nil] init];
}
return sharedStore;
}
// If a programmer calls [[BNRItemStore alloc] init], let him
// know the error of his ways
- (instancetype)init
{
#throw [NSException exceptionWithName:#"Singleton"
reason:#"Use +[BNRItemStore sharedStore]"
userInfo:nil];
return nil;
}
// Here is the real (secret) initializer
- (instancetype)initPrivate
{
self = [super init];
if (self) {
_privateItems = [[NSMutableArray alloc] init];
}
return self;
}
- (NSArray *)allItems
{
return [self.privateItems copy];
}
- (BNRItem *)createItem
{
BNRItem *item = [BNRItem randomItem];
[self.privateItems addObject:item];
return item;
}
- (void)removeItem:(BNRItem *)item
{
NSString *key = item.itemKey;
if (key) {
[[BNRItemStore sharedStore] deleteImageForKey:key];
}
[self.privateItems removeObjectIdenticalTo:item];
}
- (void)moveItemAtIndex:(NSInteger)fromIndex
toIndex:(NSInteger)toIndex
{
if (fromIndex == toIndex) {
return;
}
// Get pointer to object being moved so you can re-insert it
BNRItem *item = self.privateItems[fromIndex];
// Remove item from array
[self.privateItems removeObjectAtIndex:fromIndex];
// Insert item in array at new location
[self.privateItems insertObject:item atIndex:toIndex];
}
#end
BNRImageStore.h
#import <Foundation/Foundation.h>
#interface BNRImageStore : NSObject
+ (instancetype)sharedStore;
- (void)setImage:(UIImage *)image forKey:(NSString *)key;
- (UIImage *)imageForKey:(NSString *)key;
- (void)deleteImageForKey:(NSString *)key;
#end
BNRImageStore.m
#import "BNRImageStore.h"
#interface BNRImageStore ()
#property (nonatomic, strong) NSMutableDictionary *dictionary;
#end
#implementation BNRImageStore
+ (instancetype)sharedStore
{
static BNRImageStore *sharedStore = nil;
if (!sharedStore) {
sharedStore = [[self alloc] initPrivate];
}
return sharedStore;
}
// No one should call init
- (instancetype)init
{
#throw [NSException exceptionWithName:#"Singleton"
reason:#"Use +[BNRImageStore sharedStore]"
userInfo:nil];
return nil;
}
// Secret designated initializer
- (instancetype)initPrivate
{
self = [super init];
if (self) {
_dictionary = [[NSMutableDictionary alloc] init];
}
return self;
}
- (void)setImage:(UIImage *)image forKey:(NSString *)key
{
self.dictionary[key] = image;
}
- (UIImage *)imageForKey:(NSString *)key
{
return self.dictionary[key];
}
- (void)deleteImageForKey:(NSString *)key
{
if (!key) {
return;
}
[self.dictionary removeObjectForKey:key];
}
#end
You are declaring the method deleteImageForKey in your BNRImageStore class, not in your BNRItemStore.
Check your implementation of removeItem: in BNRItemStore.m
- (void)removeItem:(BNRItem *)item
{
NSString *key = item.itemKey;
if (key) {
[[BNRItemStore sharedStore] deleteImageForKey:key];
}
[self.privateItems removeObjectIdenticalTo:item];
}
I assume you meant to refer to "BNRImageStore" and not BNRItemStore.
Apart from the typo, you should understand that Objective-C objects respond to selectors. Every Objective-C object has an array of selectors that it responds to. When you see the error: "No visible #interface for 'BNRItemStore' declares the selector 'deleteImageForKey;'" you should understand that the compiler does not see the selector you specified as being understood by the class in the error.

objective c delegates

My program displays images with a timer. I want the user to be able to adjust the timer by using a UISlider.
I have a Deck class, which delegates and listens to another delegate 'SpeedSliderDelegate'. I want my Deck class to listen for changes from the SpeedSliderDelegate and update a value.
How do I set my Deck.m model to listen to SpeedSliderDelegate...(just after if(self = [super init] in Deck.m)
SpeedSliderDelegate.h
#protocol SpeedSliderDelegate <NSObject>
-(void)setSpeed:(float)currentSpeed;
#end
Deck.h
#import "SpeedSliderDelegate.h"
#import <Foundation/Foundation.h>
#import "Card.h"
#protocol DeckLooperDelegate <NSObject>
-(void)cardHasChangedTo:card1 aCard2:(Card *)card2 totalCount:(NSInteger)totalCount stopTimer:(NSTimer*)stopTimer cards:(NSArray *)cards;
#end
#interface Deck : NSObject <SpeedSliderDelegate>
#property (nonatomic, retain)NSMutableArray *cards;
#property (nonatomic, retain)NSTimer *timer;
#property (nonatomic, retain)id <DeckLooperDelegate> delegate;
#property (nonatomic)NSInteger total;
#property (nonatomic) float testSpeed;
- (NSInteger) cardsRemaining;
- (void) startTimerLoop;
#end
Deck.m
#import "Deck.h"
#import "Card.h"
#implementation Deck
#synthesize cards;
#synthesize timer;
#synthesize delegate;
#synthesize total, testSpeed;
- (id) init
{
if(self = [super init])
{
//self.delegate = self something like this...
cards = [[NSMutableArray alloc] init];
NSInteger aCount, picNum = 0;
for(int suit = 0; suit < 4; suit++)
{
for(int face = 1; face < 14; face++, picNum++)
{
if (face > 1 && face < 7)
aCount = 1;
else if (face > 6 && face < 10)
aCount = 0;
else
aCount = -1;
NSString* imagePath = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:#"card_%d", picNum] ofType:#"png"];
UIImage* theImage = [[UIImage alloc] initWithContentsOfFile:imagePath];
Card *card = [[Card alloc] initWithFaceValue:(NSInteger)face
countValue:(NSInteger)aCount
suit:(Suit)suit
cardImage:(UIImage *)theImage];
[cards addObject:card];
}
}
}
return self;
}
-(void)setSpeed:(float)currentSpeed
{
testSpeed = currentSpeed;
}
-(void)startTimerLoop
{
if (!timer)
{
timer = [NSTimer scheduledTimerWithTimeInterval:testSpeed target:self
selector:#selector(timerEvent:) userInfo:nil repeats:YES ];
NSLog(#"Timer started!!!");
}
}
-(void)timerEvent:(NSTimer*)timer
{
srand(time(nil));
int index = rand()%[cards count];
Card *randomCard =[cards objectAtIndex:index];
[cards removeObjectAtIndex:index];
NSLog(#"%#" ,randomCard);
int index1 = rand()%[cards count];
Card *randomCard1 =[cards objectAtIndex:index1];
[cards removeObjectAtIndex:index1];
NSLog(#"%#" ,randomCard1);
total += (randomCard.countValue + randomCard1.countValue);
[self.delegate cardHasChangedTo:randomCard aCard2:randomCard1 totalCount:total stopTimer:self.timer cards:cards];
if ([cards count] == 0)
{
[self.timer invalidate];
self.timer = nil;
}
}
- (NSInteger) cardsRemaining
{
return [cards count];
}
- (NSString *) description
{
NSString *desc = [NSString stringWithFormat:#"Deck with %d cards\n",
[self cardsRemaining]];
return desc;
}
#end
GameViewController.h
#import "Deck.h"
#import "SpeedSliderDelegate.h"
#interface GameViewController : UIViewController <DeckLooperDelegate>
#property (nonatomic, retain) IBOutlet UIImageView *cardDisplay, *cardDisplay1;
#property (weak, nonatomic) IBOutlet UILabel *cardName, *cardName1;
#property (weak, nonatomic) IBOutlet UILabel *cardCount, *cardCount1;
#property (weak, nonatomic) IBOutlet UILabel *totalCount;
#property (nonatomic)int stop1;
#property (weak, nonatomic) IBOutlet UIButton *stopButton;
#property (weak, nonatomic) IBOutlet UIButton *restartButton;
#property (weak, nonatomic) IBOutlet UIButton *homeButton;
#property (weak, nonatomic) IBOutlet UISlider *speed;
#property (nonatomic, retain) id <SpeedSliderDelegate> delegate;
- (IBAction)start:(id)sender;
- (IBAction)stop:(id)sender;
- (IBAction)hideShow:(id)sender;
- (IBAction)homeAction:(id)sender;
#end
GameViewController.m
#import <QuartzCore/QuartzCore.h>
#import "GameViewController.h"
#import "Deck.h"
#implementation GameViewController
#synthesize cardDisplay, cardDisplay1, cardName, cardName1, cardCount, cardCount1, totalCount, stop1, stopButton, restartButton, homeButton, speed, delegate;
- (void)viewDidLoad
{
[super viewDidLoad];
Deck *deck = [[Deck alloc]init];
NSLog(#"%#", deck);
for (id cards in deck.cards)
{
NSLog(#"%#", cards);
}
deck.delegate = self;
[deck startTimerLoop];
cardDisplay.layer.cornerRadius = 7;
cardDisplay.clipsToBounds = YES;
cardDisplay1.layer.cornerRadius = 7;
cardDisplay1.clipsToBounds = YES;
[restartButton setHidden:YES];
[delegate setSpeed:speed.value];
}
//#pragma mark - DeckDelegate
-(void)cardHasChangedTo:(Card *)card1 aCard2:(Card *)card2 totalCount:(NSInteger)totalC stopTimer:(NSTimer *)stopTimer cards:(NSArray *)cards
{
[self.cardDisplay setImage:card1.cardImage];
[self.cardDisplay1 setImage:card2.cardImage];
self.cardName.text = card1.description;
self.cardName1.text = card2.description;
self.cardCount.text = [NSString stringWithFormat:#"%d", card1.countValue];
self.cardCount1.text = [NSString stringWithFormat:#"%d", card2.countValue];
self.totalCount.text = [NSString stringWithFormat:#"%d", totalC];
if (stop1 == 86)
{
[stopTimer invalidate];
stopTimer = nil;
}
if ([cards count] == 0)
{
[restartButton setHidden:NO];
}
}
- (IBAction)start:(id)sender
{
stop1 = 85;
[cardDisplay setHidden:YES];
[cardDisplay1 setHidden:YES];
self.totalCount.text = #"0";
self.cardCount.text = #"0";
self.cardCount1.text = #"0";
self.cardName.text = #"";
self.cardName1.text = #"";
Deck *deck = [[Deck alloc]init];
[NSTimer scheduledTimerWithTimeInterval:2 target:self
selector:#selector(cardDisplayDelay:) userInfo:nil repeats:NO];
NSLog(#"%#", deck);
deck.delegate = self;
[deck startTimerLoop];
cardDisplay.layer.cornerRadius = 7;
cardDisplay.clipsToBounds = YES;
cardDisplay1.layer.cornerRadius = 7;
cardDisplay1.clipsToBounds = YES;
[restartButton setHidden:YES];
}
- (IBAction)stop:(id)sender
{
stop1 = 86; //cancelled
[NSTimer scheduledTimerWithTimeInterval:2 target:self
selector:#selector(restartButtonDelay:) userInfo:nil repeats:NO];
}
-(void)restartButtonDelay:(NSTimer*)timer
{
[restartButton setHidden:NO];
}
-(void)cardDisplayDelay:(NSTimer*)timer
{
[cardDisplay setHidden:NO];
[cardDisplay1 setHidden:NO];
}
- (IBAction)hideShow:(id)sender
{
if ([cardCount isHidden])
{
[cardCount setHidden:NO];
[cardCount1 setHidden:NO];
[totalCount setHidden:NO];
}
else
{
[cardCount setHidden:YES];
[cardCount1 setHidden:YES];
[totalCount setHidden:YES];
}
}
- (IBAction)homeAction:(id)sender
{
stop1 = 86; //cancelled
}
#end
In the updating class .h
#protocol DoSomethingDelegate <NSObject>
-(void)doSomething;
#end
#property (assign, nonatomic) id <DoSomethingDelegate> delegate;
In the updating class .m
-(void)doSomethingSomewhereElse {
if (self.delegate != nil && [self.delegate respondsToSelector:#selector(doSomething)]) {
[self.delegate performSelector:#selector(doSomething)];
}
} else {
NSLog(#"Delgate doesn't implement doSomething");
}
}
In the receiving class' .h:
Conform to protocol <DoSomethingDelegate>
#interface myDoSomethingClass : NSObject <DoSomethingDelegate>
...
Implement the delegate method in the receiving class' .m:
-(void)someInit{
//set the delegate to self
}
In the view controller .m
-(void)doSomething{
NSLog(#"The delegate told me to do this!");
}
The deck is not its own delegate. Right now your code says that a Deck is a SpeedSliderDelegate, and that a Deck's delegate is a DeckLooperDelegate.
If you always need a delegate for Deck, you can do it this way:
- (id) initWithDelegate:(id<DeckLooperDelegate>)delegate {
if (self = [super init]) {
self.delegate = delegate;
cards = [[NSMutableArray alloc] init];
NSInteger aCount, picNum = 0;
// etc etc etc
}
}
Then, in the DeckLooperDelegate that creates the deck, you call it like this:
Deck *deck = [[Deck alloc] initWithDelegate:self];
Then you want your speed slider to set the deck as its delegate.
mySpeedSlider.delegate = deck;
Now mySpeedSlider can call [delegate setSpeed:] to change the deck's speed.

Using a property as a count in Objective C

I am new to Objective C. I was following Stanford lectures 2011-12 fall on iOS development and in assignment 1 it asks to implement a decimal point in a calculator. This is what my implementation looks like:
#import "CalculatorViewController.h"
#import "CalculatorBrain.h"
#interface CalculatorViewController()
#property (nonatomic) BOOL userIsInTheMiddleOfEnteringNumber;
#property (nonatomic,strong) CalculatorBrain *brain;
#property (nonatomic) int userPressedDecimalPoint;
#end
#implementation CalculatorViewController
#synthesize display = _display;
#synthesize userIsInTheMiddleOfEnteringNumber = _userIsInTheMiddleOfEnteringNumber;
#synthesize userPressedDecimalPoint = _userPressedDecimalPoint;
#synthesize brain = _brain;
- (CalculatorBrain *) brain{
if (!_brain) _brain = [[CalculatorBrain alloc] init];
return _brain;
}
- (IBAction)digitpressed:(UIButton *)sender {
NSString *digit = sender.currentTitle;
if (digit == #"."){
self.userIsInTheMiddleOfEnteringNumber = YES;
self.userPressedDecimalPoint++;
}
if (self.userIsInTheMiddleOfEnteringNumber && self.userPressedDecimalPoint< 2){
self.display.text = [self.display.text stringByAppendingString:digit];
NSLog(#"decimal pressed: %d times",self.userPressedDecimalPoint);
}
else{
self.display.text = digit;
self.userIsInTheMiddleOfEnteringNumber = YES;
self.userPressedDecimalPoint = 1;
}
}
So basically I have setup a property userPressedDecimalPoint and i try to use this property as a counter every time the decimal point is pressed. However from the NSLog i see that no matter how many times i press the decimal point it only shows 1 time pressed. Consequently, the output shows multiple decimal points if entered. Any suggestions would be appreciated. Thanks!
Full version of Corrected Code:
#import "CalculatorViewController.h"
#import "CalculatorBrain.h"
#interface CalculatorViewController()
#property (nonatomic) BOOL userIsInTheMiddleOfEnteringNumber;
#property (nonatomic,strong) CalculatorBrain *brain;
#property (nonatomic) int userPressedDecimalPoint;
#end
#implementation CalculatorViewController
#synthesize display = _display;
#synthesize userIsInTheMiddleOfEnteringNumber = _userIsInTheMiddleOfEnteringNumber;
#synthesize userPressedDecimalPoint = _userPressedDecimalPoint;
#synthesize brain = _brain;
- (CalculatorBrain *) brain{
if (!_brain) _brain = [[CalculatorBrain alloc] init];
return _brain;
}
- (IBAction)digitpressed:(UIButton *)sender {
NSString *digit = sender.currentTitle;
if ([digit isEqualToString:#"."]){
self.userIsInTheMiddleOfEnteringNumber = YES;
self.userPressedDecimalPoint++;
}
if (self.userIsInTheMiddleOfEnteringNumber && self.userPressedDecimalPoint< 2){
self.display.text = [self.display.text stringByAppendingString:digit];
// NSLog(#"decimal pressed: %d times",self.userPressedDecimalPoint);
}
else if (![digit isEqualToString:#"."]){
self.display.text = digit;
self.userIsInTheMiddleOfEnteringNumber = YES;
}
}
- (IBAction)operationPressed:(UIButton *)sender {
if (self.userIsInTheMiddleOfEnteringNumber) [self enterPressed];
double result = [self.brain performOperation:sender.currentTitle];
NSString *resultString = [NSString stringWithFormat:#"%g", result];
self.display.text = resultString;
}
- (IBAction)enterPressed {
self.userPressedDecimalPoint = 0;
[self.brain pushOperand:[self.display.text doubleValue]];
self.userIsInTheMiddleOfEnteringNumber = NO;
}
#end
Unless there is something that sets userIsInTheMiddleOfEnteringNumber to NO, any time the number is greater than one, your code will execute self.userPressedDecimalPoint = 1; and set it back to one.

Problems with array of type class

Hey guys I am trying to build a database that maps NSStrings to int. I have on class called Movie.h where each object has a name and a number assigned:
//Movie.h
#interface Movie : NSObject
{
int m_num;
NSString *m_name;
}
#property int m_num;
#property(nonatomic, retain) NSString *m_name;
#end
//Movie.m
#implementation Movie
#synthesize m_num, m_name;
#end
I then have another class called Map where I am implementing functions to play with my "Movies". One of the function is called insert, and it inserts an object of class movie into an array where all movies should be stored. The code compiles but my "m_array" does not seem to keep a record of what I add to it. Here is the code:
//Map.h
#import "Movie.h"
#interface Map : NSObject
{
#private
int m_count;
NSMutableArray *m_array;
}
#property int m_count;
#property(nonatomic, retain) NSMutableArray *m_array;
-(bool) contain: (NSString *) name;
-(bool) insert: (NSString *) name: (int) chap;
#end
//Map.m
#implementation Map
#synthesize m_count, m_array;
//Constructor
-(id) init{
if (self = [super init]){
m_count = 0;
}
return self;
}
-(bool) contain: (NSString *) name{
bool b = false;
for (int i = 0; i < m_count; i++) {
Movie *m = [[Movie alloc]init];
m = [m_array objectAtIndex:i];
NSLog(#"%# came out in %i", m.m_name, m.m_num);
if (m.m_name == name) {
b = true;
}
}
return b;
}
-(bool) insert:(NSString *) name: (int) chap{
Movie *m1 = [[Movie alloc]init];
m1.m_name = name;
m1.m_num = chap;
[m_array addObject:m1];
NSLog(#"Here is the object %#",[m_array objectAtIndex:m_count]);
m_count++;
return true;
}
#end
-(bool) upgrade:(NSString *)name :(int)chap{
if(![self contain:name])
return false;
for (int i = 0; i < m_count; i++){
Movie *m = [[Movie alloc]init];
m = [m_array objectAtIndex:i];
if(m.m_name == name)
m.m_num = chap;
}
return true;
}
Here's my main:
//main.m
#import "Map.h"
int main (int argc, const char * argv[])
{
#autoreleasepool
{
Map *m = [[Map alloc]init];
[m insert:#"James Bond" :2001];
if (![m contain:#"James Bond"]) {
NSLog(#"It does not work");
}
}
return 0;
}
Here's the console output:
2012-02-27 14:20:04.923 myMap[3926:707] Here is the object (null)
2012-02-27 14:20:05.036 myMap[3926:707] (null) came out in 0
2012-02-27 14:20:05.037 myMap[3926:707] It does not work
It looks like you forgot to create the array:
- (id)init
{
self = [super init]
if (nil != self) {
m_count = 0;
m_array = [NSMutableArray new]; << here
}
return self;
}
Without creating it, it's just nil.