iOS Referencing NSMutableArray Within an Object - Deciphering Syntax

General Tech Learning Aids/Tools 2 years ago

0 2 0 0 0 tuteeHUB earn credit +10 pts

5 Star Rating 1 Rating

Posted on 16 Aug 2022, this text provides information on Learning Aids/Tools related to General Tech. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.

Take Quiz To Earn Credits!

Turn Your Knowledge into Earnings.

tuteehub_quiz

Answers (2)

Post Answer
profilepic.png
manpreet Tuteehub forum best answer Best Answer 2 years 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
profilepic.png
manpreet 2 years 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"];

0 views   0 shares

No matter what stage you're at in your education or career, TuteeHub will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.