0

xCode iOS 未注册:

currentCard = [myBook getCard:cardIndex];

在对象(addressCard)中的特定索引(int index),NSMutableArray(book)中,对象(addressBook)中,为字符串调用get方法(名称)有些困难。

基本上,iOS viewController 有自己的 addressBook 实例,它在它实例化的 NSMutableArray 中保存 addressCards。然而在 viewController 的 viewLoad 中,AddressBook 却没有拿到卡片。

在这种特殊情况下,语法是否有某种特殊形式?

对 iOS 来说相当新,因为它的一个应用程序有几个文件,但主要问题与 addressBook.m 的 addCard 和 getCard 方法直接相关。该项目基于 Kochan 的学习 Objective-C 的课程,如果有人熟悉它(或者可以推荐更好的学习 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:[card1 name]];

   // [_nameField setText:presetName1];
   // [_emailField setText:presetEmail1];
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}





- (IBAction)btnNext_touch:(id)sender {
//check bools



//go to next card, check for last.
    int i = [myBook entries];
    if (i == cardIndex)
    {cardIndex = 0;}
    else {
        cardIndex++;
    }



//reset current bools
    isNew = false;
    cardChanged = false;


    //set card to current card
    currentCard = [myBook getCard:cardIndex];

    //set field values
    [_nameField setText:[currentCard name]];
    [_emailField setText:[currentCard email]];

}

- (IBAction)nameFieldChanged:(id)sender {
    cardChanged = true;

    //    if (isNew != true)
    //  {

    //    [_nameField setText:[];
    //}   else{

    //}
}

- (IBAction)emailFieldChanged:(id)sender {
    cardChanged = true;
}


@end

如果他们对理解这个小问题有额外的帮助,这里是其余的课程。

ViewController.h
//
//  ViewController.h
//  Lozano007-p4
//
//  Created by antonio a lozano ii on 2/19/13.
//  Copyright (c) 2013 antonio a lozano ii. All rights reserved.
//

#import <UIKit/UIKit.h>
#import "AddressBook.h"


@interface ViewController : UIViewController


@property (strong, nonatomic) IBOutlet UITextField *nameField;
@property (strong, nonatomic) IBOutlet UITextField *emailField;


@property (strong, nonatomic) IBOutlet UIButton *btnNext;


- (IBAction)btnNext_touch:(id)sender;

- (IBAction)nameFieldChanged:(id)sender;
- (IBAction)emailFieldChanged:(id)sender;


@end



AddressCard.h

//
//  AddressCard.h
//  Lozano007-p4
//
//  Created by antonio a lozano ii on 2/19/13.
//  Copyright (c) 2013 antonio a lozano ii. All rights reserved.
//

#import <Foundation/Foundation.h>
@interface AddressCard : NSObject <NSCopying, NSCoding>

@property (nonatomic, copy) NSString *name, *email;
-(void) setName: (NSString *) theName andEmail: (NSString *) theEmail;
-(void) assignName: (NSString *) theName andEmail: (NSString *) theEmail;
-(NSComparisonResult) compareNames: (id) element;
-(void) print;
@end


AddressCard.m
//
//  AddressCard.m
//  Lozano007-p4
//
//  Created by antonio a lozano ii on 2/19/13.
//  Copyright (c) 2013 antonio a lozano ii. All rights reserved.
//

#import "AddressCard.h"

@implementation AddressCard
@synthesize name, email;

-(void) setName: (NSString *) theName andEmail: (NSString *) theEmail
{
    self.name = theName;
    self.email = theEmail;
}

// Compare the two names from the specified address cards
-(NSComparisonResult) compareNames: (id) element
{
    return [name compare: [element name]];
}

-(void) print
{
    NSLog (@"====================================");
    NSLog (@"|                                  |");
    NSLog (@"| %-31s |", [name UTF8String]);
    NSLog (@"| %-31s |", [email UTF8String]);
    NSLog (@"|                                  |");
    NSLog (@"|                                  |");
    NSLog (@"|                                  |");
    NSLog (@"|       O                O         |");
    NSLog (@"====================================");
}

-(id) copyWithZone: (NSZone *) zone
{
id newCard = [[[self class] allocWithZone: zone]init];
              [newCard assignName: name andEmail: email];
              return newCard;
}

              -(void) assignName: (NSString *) theName andEmail: (NSString *) theEmail
{
                  name = theName;
                  email = theEmail;
}
              -(void) encodeWithCoder: (NSCoder *) encoder
{
                  [encoder encodeObject: name forKey: @"AddressCardName"];
                  [encoder encodeObject: email forKey: @"AddressCardEmail"];
}
              -(id) initWithCoder: (NSCoder *) decoder
{
    name = [decoder decodeObjectForKey: @"AddressCardName"];
    email = [decoder decodeObjectForKey: @"AddressCardEmail"];
    return self;
}


@end


AddressBook.h

//
//  AddressBook.h
//  Lozano007-p4
//
//  Created by antonio a lozano ii on 2/19/13.
//  Copyright (c) 2013 antonio a lozano ii. All rights reserved.
//

#import <Foundation/Foundation.h> 
#import "AddressCard.h"
@interface AddressBook: NSObject <NSCopying, NSCoding>

//{

 //Add a mutable array for the address cards
  //  NSMutableArray *cardArray;

//}


@property (nonatomic, copy) NSString *bookName;
@property (nonatomic, strong) NSMutableArray *book;
-(id) initWithName: (NSString *) name;
-(void) sort;
-(void) addCard: (AddressCard *) theCard;
-(void) sort2;
-(void) removeCard: (AddressCard *) theCard;
-(int) entries;
-(void) list;
-(AddressCard *) lookup: (NSString *) theName;

//Add a getMethod for the Current Display'd card.
-(AddressCard*) getCard: (int) index;
@end


AddressBook.m
//
//  AddressBook.m
//  Lozano007-p4
//
//  Created by antonio a lozano ii on 2/19/13.
//  Copyright (c) 2013 antonio a lozano ii. All rights reserved.
//

#import "AddressBook.h"

@implementation AddressBook

@synthesize book, bookName;

// set up the AddressBook's name and an empty book
-(id) initWithName: (NSString *) name
{
    self = [super init];

    if (self) {
        bookName = [NSString stringWithString: name];
        book = [NSMutableArray array];

    }

    return self;
}

-(id) init
{
    return [self initWithName: @"Unnamed Book"];
            }


// Write our own book setter to create a mutable copy
-(void) setBook: (NSArray *) theBook
{
 book = [theBook mutableCopy];
}


-(void) sort
{
[book sortUsingSelector: @selector(compareNames:)];
}




// Alternate sort using blocks
-(void) sort2
{
[book sortUsingComparator:
    ^(id obj1, id obj2) {
                    return [[obj1 name] compare: [obj2 name]];}];
}


-(void) addCard: (AddressCard *) theCard
{
    [book addObject: theCard];
}



-(void) removeCard: (AddressCard *) theCard
{
    [book removeObjectIdenticalTo: theCard];
}


-(int) entries
{
    return [book count];
}


-(void) list
{
    NSLog (@"======== Contents of: %@ =========", bookName);

    for ( AddressCard *theCard in book )
        NSLog (@"%-20s %-32s", [theCard.name UTF8String],
               [theCard.email UTF8String]);

    NSLog (@"==================================================");
}

// lookup address card by name — assumes an exact match



-(AddressCard *) lookup: (NSString *) theName
{
    for ( AddressCard *nextCard in book )
        if ( [[nextCard name] caseInsensitiveCompare: theName]
            == NSOrderedSame )
            return nextCard;

    return nil;
}


-(void) encodeWithCoder: (NSCoder *) encoder
{
    [encoder encodeObject:bookName forKey: @"AddressBookBookName"];
    [encoder encodeObject:book forKey: @"AddressBookBook"];
}



-(id) initWithCoder: (NSCoder *) decoder
{
    bookName = [decoder decodeObjectForKey: @"AddressBookBookName"];
    book = [decoder decodeObjectForKey: @"AddressBookBook"];

    return self;
}


// Method for NSCopying protocol



-(id) copyWithZone: (NSZone *) zone
    {
id newBook = [[[self class] allocWithZone: zone] init];

[newBook setBookName: bookName];

// The following will do a shallow copy of the address book
    [newBook setBook: book];

        return newBook;
    }


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

}
@end

-- 再一次,代码并没有专门生成错误消息。更重要的是,顶部指定的代码行对代码没有任何影响。

4

1 回答 1

0

这是 ViewController 的全部来源吗?如果是这样,我看不到您在哪里创建了通讯簿实例。

通常,如果您向某事发送消息但没有任何反应,您应该检查“某事”是否为 nil。在你的 Java 时代,你会看到一个 NPE。在这种语言中,什么都不会发生。'myBook' 在这里可能为零。

像这样的事情viewDidLoad应该做:

myBook = [[AddressBook alloc] initWithName:@"Some Name"];
于 2013-02-20T11:26:51.417 回答