iOS在对象中引用NSMutableArray - 解密语法

时间:2013-02-20 11:04:52

标签: iphone xcode nsmutablearray addressbook

xCode iOS未注册:

currentCard = [myBook getCard:cardIndex];

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

基本上,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

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

1 个答案:

答案 0 :(得分:0)

这是ViewController的全部来源吗?如果是这样,我看不到您创建地址簿实例的位置。

一般情况下,如果您发送消息并且没有任何反应,则应检查“某事”是否为零。在你的Java时代,你会看到一个NPE。在这种语言中,没有任何反应。 'myBook'可能在这里没有。

viewDidLoad中应该这样做:

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