如何从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
    
已邀请:
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;
}
您在输出中看到的地址并不意味着程序无法在数组中找到ѭ11。实际上,这是完全相反的意思。该地址是您的“ 11”实例内存中的地址。它被打印为地址的原因是因为这是打印
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
    
您已经在代码的最后4行中将对象从数组中移出了。 当使用
[transactions objectAtIndex:i]
时,将返回数组中的
i\'th
transaction
对象。     
您遇到的问题是您要在as19ѭ中将对象输出为整数(
i
)。相反,您应该使用
@
。 另一个问题是,Objective-C中的对象是引用类型。这意味着当您将一个对象分配给另一个对象时,请说:
Transaction* transA = [[Transaction alloc] init];
Transaction* transB = [[Transaction alloc] init];

transB = transA;
前两行创建两个单独的对象。但是第三行将“ 22”指定为对“ 23”的引用。也就是说,它们是同一对象。另外,由于
transB
不再指向原始对象,因此会泄漏内存。 尽管我不是100%不确定对象的创建方式,但是类似于
[aTransaction createTransaction:n*100 ofType:cash];
的行将值分配给同一对象(
aTransaction
)。这意味着,数组中包含的所有对象很可能都是指向同一对象的指针。而且,如果它们不返回自动释放的对象,则可能会泄漏内存。 在
NSArray
NSMutableArray
(或可能是任何集合类型)中记录元素的简单方法是:
NSArray* someArray = [NSArray array];
// Add items here.
NSLog(@\"SomeArray: %@\", someArray);
输出将自动为您格式化。     

要回复问题请先登录注册