如何从NSMutableArray检索和查看元素
|
我正在尝试从“ Objective C for Dummies”一书中举一些例子。我试图用下面的代码来检索元素,但是徒劳。所有这些都被视为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;
}
BudObj.h
#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
BudObj.m
#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
Transaction.h
#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
Transaction.m
#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
没有找到相关结果
已邀请:
3 个回复
坝胺绣敝
当然,当您有这样的代码时:
...您实际上并没有在阵列中存储新创建的事务。您只是多次存储变量
。您可能会遇到类似以下的情况:
编辑: 您可能会有更多的运气:
您在输出中看到的地址并不意味着程序无法在数组中找到ѭ11。实际上,这是完全相反的意思。该地址是您的“ 11”实例内存中的地址。它被打印为地址的原因是因为这是打印
实例时的默认行为。您的类不会覆盖此默认行为,因此在打印时会获得内存地址。 如果要覆盖此默认行为,可以执行以下操作:
结乳
时,将返回数组中的
对象。
才脊烽馈低
)。相反,您应该使用
。 另一个问题是,Objective-C中的对象是引用类型。这意味着当您将一个对象分配给另一个对象时,请说:
前两行创建两个单独的对象。但是第三行将“ 22”指定为对“ 23”的引用。也就是说,它们是同一对象。另外,由于
不再指向原始对象,因此会泄漏内存。 尽管我不是100%不确定对象的创建方式,但是类似于
的行将值分配给同一对象(
)。这意味着,数组中包含的所有对象很可能都是指向同一对象的指针。而且,如果它们不返回自动释放的对象,则可能会泄漏内存。 在
或
(或可能是任何集合类型)中记录元素的简单方法是:
输出将自动为您格式化。