Categories

See More
Popular Forum

MBA (4887) B.Tech (1769) Engineering (1486) Class 12 (1030) Study Abroad (1004) Computer Science and Engineering (988) Business Management Studies (865) BBA (846) Diploma (746) CAT (651) B.Com (648) B.Sc (643) JEE Mains (618) Mechanical Engineering (574) Exam (525) India (462) Career (452) All Time Q&A (439) Mass Communication (427) BCA (417) Science (384) Computers & IT (Non-Engg) (383) Medicine & Health Sciences (381) Hotel Management (373) Civil Engineering (353) MCA (349) Tuteehub Top Questions (348) Distance (340) Colleges in India (334)
See More

iOS Referencing NSMutableArray Within an Object - Deciphering Syntax

General Tech Learning Aids/Tools
Max. 2000 characters
Replies

usr_profile.png
Liza Sain

User

( 6 months ago )

 

xCode iOS not registering:

currentCard = [myBook getCard:cardIndex];

Having some difficulty calling a get method (name), for a string, within an Object(addressCard) at a particular index(int index), within an NSMutableArray(book), within an object(addressBook).

Basically an iOS viewController has its own instance of an addressBook that holds addressCards within an NSMutableArray it instantiates. However in the viewLoad of the viewController, the AddressBook however doesn't get the card.

Is there some particular form for syntax in this particular case?

Fairly new to iOS, because its an app there are several files, but the main issue is directly related to addressBook.m's addCard and getCard Methods. Project is based on a lesson in learning Objective-C by Kochan if anyone is familiar with it (or can recommend a better source for learning iOS).

Method within AddressBook.m

//Add a method for getting the current card in view
-(AddressCard*) getCard: (int) index{
    AddressCard *cardC = [book objectAtIndex:index];
    return cardC;    
}


ViewController.m

#import "ViewController.h"


@interface ViewController ()

@end

@implementation ViewController
{
    BOOL isNew;
    int cardIndex;
    AddressBook *myBook;
    AddressCard *currentCard;
    BOOL cardChanged;
}


- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.


    //Initialize the Address Book
    NSString *presetName1 = @"Dave Strider";
    NSString *presetName2 = @"Rose Tyler";
    NSString *presetName3 = @"Sherlock Holmes";

    NSString *presetEmail1 = @"TurntechGodhead@skaia.net";
    NSString *presetEmail2 = @"DoctorsBetterHalf@tardis.Who";
    NSString *presetEmail3 = @"SHolmes@221Baker.st";


    AddressCard *card1 = [[AddressCard alloc]init];
    AddressCard *card2 = [[AddressCard alloc]init];
    AddressCard *card3 = [[AddressCard alloc]init];

    [card1 setName: presetName1 andEmail:presetEmail1];
    [card2 setName: presetName2 andEmail:presetEmail2];
    [card3 setName: presetName3 andEmail:presetEmail3];

    [myBook addCard:card1];
    [myBook addCard:card2];
    [myBook addCard:card3];



    //set the addybook index to zero
    cardIndex = 0;

    //set card to current card
    //***** ISSUE IS HERE *****
    currentCard = [myBook getCard:cardIndex];




    //set field values ***** currentCard gains no variables. 
    [_nameField setText:[currentCard name]];
    [_emailField setText:[currentCard email]];

    isNew = false;
    cardChanged = false;

  //*****  TESTING SYNTAX *****
  // This method however works perfectly, the card, and its string are accepted ****
  //   [_nameField setText:[c
									

usr_profile.png
Karlie Kloss

User

( 6 months ago )

Is that the entire source for ViewController? If so, I don't see where you created the address book instance.

In general, if you send a message to something and nothing happens, you should check to see that "something" isn't nil. In your java days, you would have seen an NPE. In this language, nothing happens. 'myBook' is probably nil here.

Something like this in viewDidLoad should do:

myBook = [[AddressBook alloc] initWithName:@"Some Name"];

what's your interest