Xcode中的iOS不登記:的iOS引用的NSMutableArray中的對象 - 解讀語法
currentCard = [myBook getCard:cardIndex];
在一個特定的指數有一些困難,調用get方法(名稱),用於字符串, 對象(addressCard)內(int index),在一個對象(addressBook)內的 NSMutableArray(book)中。
基本上,一個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 = @"[email protected]";
NSString *presetEmail2 = @"DoctorsBetterHal[email protected]";
NSString *presetEmail3 = @"[email protected]";
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
這裏是其餘的類,如果他們會提供額外的幫助理解這個nubby問題。
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
- 再次,沒有特別的代碼生成一個錯誤消息。更重要的是,頂部指定的代碼行對代碼沒有任何影響。
什麼是你看到的錯誤? – 2013-02-20 11:13:37
約定是不使用「get」作爲前綴getters。取而代之的是一個名爲'getCard:'的方法。以get開頭的方法預計會通過引用返回值。遵循約定不僅使其他人能夠輕鬆理解您的代碼,而且也是編譯器和分析器使用的提示。 – zaph 2013-02-20 11:15:53
我應該指定,它不是一個錯誤,它只是,我使用的語法不更新currentCard - viewController.m的((void)viewDidLoad方法 – user1815714 2013-02-20 11:16:29