定义一个类来存储信息

时间:2012-02-23 05:03:05

标签: objective-c ios

我从联系人那里获得了用户的信息,我想存储这些信息,所以我定义了一个新类: 我的新类UserInfo.h文件

#import <Foundation/Foundation.h>

@interface UserInfo : NSObject
{
    NSString *firstName;
    NSString *lastName;
    NSMutableArray *emailArray;
    UIImage *image;
}
@property(nonatomic,retain)NSString *firstName;
@property(nonatomic,retain)NSString *lastName;
@property(nonatomic,retain)NSMutableArray *emailArray;
@property(nonatomic,retain)UIImage *image;
@end

我的UserInfo.m文件

#import "UserInfo.h"

@implementation UserInfo
@synthesize firstName;
@synthesize lastName;
@synthesize emailArray;
@synthesize image;

@end

在我的视图控制器文件中,我创建了一个NSMutableArray来存储新的类对象

NSMutableArray *m_AllUserArray;

我的.m文件,同伴代码在我的loadView函数中:

m_AllUserArray = [[NSMutableArray alloc]init];


    for (int i = 0; i<CFArrayGetCount(nameArray); i++) {
        ABRecordRef person = CFArrayGetValueAtIndex(nameArray, i);
        UserInfo *userinfo = [[UserInfo alloc]init];
        //----------get name
        userinfo.firstName = (NSString*)ABRecordCopyValue(person,kABPersonFirstNameProperty);
        userinfo.lastName = (NSString*)ABRecordCopyValue(person, kABPersonLastNameProperty);

        //----------get email
        ABMultiValueRef mails = (ABMultiValueRef)ABRecordCopyValue(person, kABPersonEmailProperty);
        for (int j = 0; j < ABMultiValueGetCount(mails); j ++) {
            NSString* emailContent = (NSString*)ABMultiValueCopyValueAtIndex(mails, j);
            if (emailContent != nil) {

                                [userinfo.emailArray addObject:emailContent];
            }
        }

        //-----------------get contact image
        NSData *imageData = (NSData*)ABPersonCopyImageData(person);
        //if there are no images in contact,will create default image instead
        UIImage *img;
        if (imageData == nil) {
            img = [UIImage imageNamed:@"friend_on_small_normal.png"];
        } else {
            img = [UIImage imageWithData:imageData];
        }
        userinfo.image = img;
        //-------------------------------------
        [m_AllUserArray addObject:userinfo];


NSLog(@"userinfo.firstName:%@",userinfo.firstName);
        NSLog(@"userinfo.lastName:%@",userinfo.lastName);
        NSLog(@"userinfo.emailArray:%@",userinfo.emailArray);
        [userinfo release];
    }

但是当我使用数组时,它为空,为什么?

for (int i=0; i<[m_AllUserArray count]; i++) {
            NSLog(@"%@",(UserInfo*)[m_AllUserArray objectAtIndex:i]);
            NSLog(@"lastName:%@",[(UserInfo*)[m_AllUserArray objectAtIndex:i] lastName]);
            NSLog(@"emailArray:%@",[[m_AllUserArray objectAtIndex:i] emailArray]);
            NSLog(@"email1:%@",[[[m_AllUserArray objectAtIndex:i] emailArray] objectAtIndex:0]);
            NSLog(@"email2:%@",[[[m_AllUserArray objectAtIndex:i] emailArray] objectAtIndex:1]);

        }

我的日志:

1970-01-11 10:57:08.-740 M[3291:307] <UserInfo: 0x2cbdd0>
1970-01-11 10:57:16.-585 M[3291:307] lastName:(null)
1970-01-11 10:57:23.-599 M[3291:307] emailArray:(null)
1970-01-11 11:52:00.-176 M[3291:307] email1:(null)
1970-01-11 11:52:04.-565 M[3291:307] email2:(null)

PS: 我已经解决了这个问题,谢谢~~~~~

1 个答案:

答案 0 :(得分:0)

我猜您在emailArray课程中讨论UserInfo

您尚未分配该阵列。在UserInfo class init方法中初始化该数组。否则,在向其添加元素之前创建一个新数组并分配给它(emailArray)。