情報を格納するクラスを定義し、私は情報を保存したいので、私は新しいクラスを定義します。 私の新しいクラス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ファイル、fellowingコードは私の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:私は、問題を解決した 、~~~~~
ここで 'm_AllUserArray = [[NSMutableArray alloc] init];'? 'init'で? –
複数の配列があるので、記述してください。 – Ilanchezhian
[m_AllUserArray addObject:userinfo.firstname];を使用してください。 Forループ内に [m_AllUserArray addObject:userinfo]の代わりに使用します。 私はそれが動作すると確信しています。 – Kamarshad