我正在尝试“傻瓜的目标C”一书中的一些例子。我尝试使用下面的代码检索元素但是徒劳无功。所有都被视为NSMutableArray中的对象。但我不知道如何使用对象检索元素。
#import <Foundation/Foundation.h>
#import "BudObj.h"
#import "Transaction.h"
int main(int argc, char *argv[]) {
Budget* europeBudget=[Budget new];
NSMutableArray *transactions=[[NSMutableArray alloc] initWithCapacity:10];
[europeBudget createBudget:1000.00 withExchangeRate:1.2500];
Transaction* aTransaction;
aTransaction = [Transaction new];
for(int n=1;n<2;n++){
aTransaction = [[Transaction alloc] init];
[aTransaction createTransaction:n*100 ofType:cash];
[transactions addObject:aTransaction];
[aTransaction release];
}
int n=1;
while (n<3) {
aTransaction = [[Transaction alloc]init];
[aTransaction createTransaction:n*100 ofType:credit];
[transactions addObject:aTransaction];
[aTransaction release];
n++;
}
do{
aTransaction = [[Transaction alloc]init];
[aTransaction createTransaction:n*100 ofType:cash];
[transactions addObject:aTransaction];
[aTransaction release];
n++;
}while (n<=3);
NSLog(@"\nNumber of elements in an array:%i",[transactions count]);
int c;
c=[transactions count];
NSLog(@"\nThe Elements are:\n");
for(int i=0;i<c;i++){
NSLog(@"%@",[transactions objectAtIndex:i]);
}
for(Transaction *aaTransaction in transactions){
switch ([aTransaction returnType]) {
case cash:
[europeBudget spendDollars:[aaTransaction returnAmount]];
break;
case credit:
[europeBudget changeForeignCurrency:[aaTransaction returnAmount]];
break;
default:
break;
}
}
[transactions release];
[europeBudget release];
return 0;
}
#import <Foundation/Foundation.h>
@interface
Budget : NSObject {
float exchangeRate;
double budget;
double exchangeTransaction;
}
- (void) createBudget: (double) aBudget withExchangeRate: (float) anExchangeRate;
- (void) spendDollars: (double) dollars;
- (void) changeForeignCurrency: (double) foreignCurrency;
@end
#import <Foundation/Foundation.h>
#import "BudObj.h"
#import "Transaction.h"
@implementation Budget
- (void) createBudget: (double) aBudget withExchangeRate: (float) anExchangeRate{
budget = aBudget;
exchangeRate = anExchangeRate;
}
- (void) spendDollars:(double)dollars{
budget = budget - dollars;
NSLog(@"Converting %0.2f US Dollars into Foreign Currency leaves $%0.2f",dollars,budget);
}
- (void) changeForeignCurrency:(double)foreignCurrency{
exchangeTransaction = foreignCurrency * exchangeRate;
budget = budget - exchangeTransaction;
NSLog(@"Charging %0.2f in Foreign Currency leaves $%0.2f",foreignCurrency,budget);
}
@end
#import <Cocoa/Cocoa.h>
typedef enum{cash,credit} transactionType;
@interface Transaction : NSObject {
transactionType type;
double amount;
}
-(void)createTransaction:(double)theAmount ofType:(transactionType)theType;
-(double)returnAmount;
-(transactionType)returnType;
@end
#import "Transaction.h"
@implementation Transaction
-(void)createTransaction:(double)theAmount ofType:(transactionType)theType{
type=theType;
amount=theAmount;
}
-(double)returnAmount{
return amount;
}
-(transactionType)returnType{
return type;
}
@end
The Elements are:
2011-04-15 18:12:11.039 BudObj.m[2180:a0f] <Transaction: 0x10010c950> //Could not retreive the data from the array it's showing up some address
2011-04-15 18:12:11.039 BudObj.m[2180:a0f] <Transaction: 0x100104fe0> //
2011-04-15 18:12:11.040 BudObj.m[2180:a0f] <Transaction: 0x100106c60> //
2011-04-15 18:12:11.040 BudObj.m[2180:a0f] <Transaction: 0x100106d00> //
2011-04-15 18:12:11.041 BudObj.m[2180:a0f] Converting 100.00 US Dollars into Foreign Currency leaves $900.00
2011-04-15 18:12:11.041 BudObj.m[2180:a0f] Converting 100.00 US Dollars into Foreign Currency leaves $800.00
2011-04-15 18:12:11.041 BudObj.m[2180:a0f] Converting 200.00 US Dollars into Foreign Currency leaves $600.00
2011-04-15 18:12:11.042 BudObj.m[2180:a0f] Converting 300.00 US Dollars into Foreign Currency leaves $300.00
答案 0 :(得分:1)
for (Transaction* transaction in transactions) {
//do stuff here, or just print the object with something like the code below
NSLog(@"Transaction: %@", transaction);
}
当然,当你有这样的代码时:
[aTransaction createTransaction:n*100 ofType:credit];
[transactions addObject:aTransaction];
...实际上并没有将新创建的事务存储在数组中。您只是多次存储变量aTransaction
。你可能会有更好的运气:
Transaction* nextTransaction = [aTransaction createTransaction:n*100 ofType:credit];
[transactions addObject:nextTransaction];
修改强>
你可能会有更多的运气:
#import <Foundation/Foundation.h>
#import "BudObj.h"
#import "Transaction.h"
int main(int argc, char *argv[]) {
Budget* europeBudget=[[Budget alloc] init];
NSMutableArray *transactions=[[NSMutableArray alloc] initWithCapacity:10];
[europeBudget createBudget:1000.00 withExchangeRate:1.2500];
Transaction* aTransaction = nil;
for(int n=1;n<2;n++){
//this adds 1 transaction to the array
aTransaction = [[[Transaction alloc] init] autorelease];
[aTransaction createTransaction:n*100 ofType:cash];
[transactions addObject:aTransaction];
}
int n=1;
while (n<3) {
//this adds 2 transactions to the array
aTransaction = [[[Transaction alloc] init] autorelease];
[aTransaction createTransaction:n*100 ofType:credit];
[transactions addObject:aTransaction];
n++;
}
do{
//this adds 1 transaction to the array
aTransaction = [[[Transaction alloc] init] autorelease];
[aTransaction createTransaction:n*100 ofType:cash];
[transactions addObject:aTransaction];
n++;
}while (n<=3);
//there should be 4 elements in the array now
NSLog(@"\nNumber of elements in an array:%i",[transactions count]);
int c;
c=[transactions count];
NSLog(@"\nThe Elements are:\n");
for(int i=0;i<c;i++){
Transaction* trans = [transactions objectAtIndex:i];
NSLog(@"Transaction %d: %@; type=%d, amount=%f", i, trans, [trans returnType], [trans returnAmount]);
}
for(Transaction *aaTransaction in transactions){
switch ([aTransaction returnType]) {
case cash:
[europeBudget spendDollars:[aaTransaction returnAmount]];
break;
case credit:
[europeBudget changeForeignCurrency:[aaTransaction returnAmount]];
break;
default:
break;
}
}
[transactions release];
[europeBudget release];
return 0;
}
您在输出中看到的地址并不意味着程序无法在阵列中找到Transaction
。事实上,它恰恰相反。地址是Transaction
实例的内存地址。它作为地址打印的原因是因为这是打印NSObject
实例时的默认行为。您的类没有覆盖此默认行为,因此您在打印时获取内存地址。
如果要覆盖此默认行为,可以执行以下操作:
#import "Transaction.h"
@implementation Transaction
//override the 'description' method to change how your object prints
-(NSString*)description {
NSString* friendlyType = theType == cash ? @"cash" : @"credit";
return [NSString stringWithFormat:@"Transaction: type=%@, amount=%f", friendlyType, amount];
}
-(void)createTransaction:(double)theAmount ofType:(transactionType)theType{
type=theType;
amount=theAmount;
}
-(double)returnAmount{
return amount;
}
-(transactionType)returnType{
return type;
}
@end
答案 1 :(得分:0)
您遇到的问题是您在i
中将对象输出为整数(NSLog
)。相反,您应该使用@
。
另一个问题是Objective-C中的对象是引用类型。这意味着当您将对象分配给另一个对象时,请说:
Transaction* transA = [[Transaction alloc] init];
Transaction* transB = [[Transaction alloc] init];
transB = transA;
前两行创建两个单独的对象。但第三行将transB
指定为引用到transA
。也就是说,它们是相同的对象。此外,由于transB
不再指向原始对象,因此会泄漏内存。
虽然我不能100%确定对象创建的工作原理,但与[aTransaction createTransaction:n*100 ofType:cash];
类似的行会将值分配给同一个对象(aTransaction
)。这意味着,数组中包含的所有对象很可能都是指向同一对象的指针。而且,如果他们没有返回自动释放的对象,这些可能会泄漏内存。
在NSArray
或NSMutableArray
(或可能是任何集合类型)中记录元素的简单方法是执行以下操作:
NSArray* someArray = [NSArray array];
// Add items here.
NSLog(@"SomeArray: %@", someArray);
输出将自动为您格式化。
答案 2 :(得分:0)
您已经在代码的最后4行中从对象中获取对象。
使用[transactions objectAtIndex:i]
时会返回数组中的i'th
transaction
对象。